-
Notifications
You must be signed in to change notification settings - Fork 1
/
chartist-plugin-slicedonutmargin.js
59 lines (49 loc) · 1.96 KB
/
chartist-plugin-slicedonutmargin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Chartist.js plugin to add margins between slices of a donut.
*
*/
/* global Chartist */
(function(window, document, Chartist) {
'use strict';
var defaultOptions = {
sliceMargin: 4
};
Chartist.plugins = Chartist.plugins || {};
Chartist.plugins.ctSliceDonutMargin = function(options) {
options = Chartist.extend({}, defaultOptions, options);
return function ctSliceDonutMargin(chart) {
if(chart instanceof Chartist.Pie) {
chart.on('draw', function(data) {
if(data.type === 'slice') {
if (Math.abs(data.startAngle - data.endAngle) < options.sliceMargin)
{
return;
}
var start = Chartist.polarToCartesian(data.center.x, data.center.y, data.radius, data.startAngle + options.sliceMargin/2),
end = Chartist.polarToCartesian(data.center.x, data.center.y, data.radius, data.endAngle - options.sliceMargin/2),
arcSweep = data.endAngle - data.startAngle <= 180 ? '0' : '1',
d = [
// Start at the end point from the cartesian coordinates
'M', end.x, end.y,
// Draw arc
'A', data.radius, data.radius, 0, arcSweep, 0, start.x, start.y
];
// Create the SVG path
var path = new Chartist.Svg('path', {
d: d.join(' ')
}, data.element._node.className.baseVal);
// Adding the pie series value to the path
path.attr({
'value': data.value
}, Chartist.xmlNs.uri);
// If this is a donut, we add the stroke-width as style attribute
path.attr({
'style': 'stroke-width: ' + data.element._node.style.strokeWidth
});
data.element.replace(path);
}
});
}
};
};
}(window, document, Chartist));