-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.flot.pie_polar.js
75 lines (60 loc) · 2.28 KB
/
jquery.flot.pie_polar.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(function ($) {
function init(plot) {
var maxValue = null,
processed = false;
plot.hooks.processOptions.push(function(plot, options) {
if (options.series.pie.show && options.series.pie.polar) {
// simple check if required pie plugin loaded
// TODO grep $.plot.plugins for sure
if (!$.isNumeric(options.series.pie.radius)) {
alert('pie plugin required to be enabled first');
}
}
});
plot.hooks.processDatapoints.push(function(plot, series, data, datapoints) {
var options = plot.getOptions();
if (options.series.pie.show && options.series.pie.polar) {
processDatapoints(plot, series, data, datapoints);
}
});
function processDatapoints(plot, series, datapoints) {
if (!processed) {
processed = true;
options = plot.getOptions();
plot.setData(processData(plot.getData()));
}
}
function processData(slices) {
// calculate maximum radius
var maxRadius = Math.min(plot.getPlaceholder().width(), plot.getPlaceholder().height() / options.series.pie.tilt) / 2;
// TODO : get maxRadius from pie plugin to avoid issue with labels
if (options.series.pie.label.show && !options.series.pie.innerRadius) maxRadius *= 0.75; // dirty fix
var i;
// calculate maximum value
for (i = 0; i < slices.length; i++) {
maxValue = Math.max(maxValue, slices[i].data[0][1]);
}
// convert(invert) angle & radius
for (i = 0; i < slices.length; i++) {
slices[i].angle = Math.PI * 2 / slices.length;
slices[i].radius = slices[i].data[0][1]/maxValue * maxRadius;
}
return slices;
}
}
// define specific options and their default values
var options = {
series: {
pie: {
show: false,
polar: false
}
}
};
$.plot.plugins.push({
init: init,
options: options,
name: 'pie_polar',
version: '0.1'
});
})(jQuery);