| 3284 |
vikas |
1 |
/*
|
|
|
2 |
Flot plugin for thresholding data. Controlled through the option
|
|
|
3 |
"threshold" in either the global series options
|
|
|
4 |
|
|
|
5 |
series: {
|
|
|
6 |
threshold: {
|
|
|
7 |
below: number
|
|
|
8 |
color: colorspec
|
|
|
9 |
}
|
|
|
10 |
}
|
|
|
11 |
|
|
|
12 |
or in a specific series
|
|
|
13 |
|
|
|
14 |
$.plot($("#placeholder"), [{ data: [ ... ], threshold: { ... }}])
|
|
|
15 |
|
|
|
16 |
The data points below "below" are drawn with the specified color. This
|
|
|
17 |
makes it easy to mark points below 0, e.g. for budget data.
|
|
|
18 |
|
|
|
19 |
Internally, the plugin works by splitting the data into two series,
|
|
|
20 |
above and below the threshold. The extra series below the threshold
|
|
|
21 |
will have its label cleared and the special "originSeries" attribute
|
|
|
22 |
set to the original series. You may need to check for this in hover
|
|
|
23 |
events.
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
(function ($) {
|
|
|
27 |
var options = {
|
|
|
28 |
series: { threshold: null } // or { below: number, color: color spec}
|
|
|
29 |
};
|
|
|
30 |
|
|
|
31 |
function init(plot) {
|
|
|
32 |
function thresholdData(plot, s, datapoints) {
|
|
|
33 |
if (!s.threshold)
|
|
|
34 |
return;
|
|
|
35 |
|
|
|
36 |
var ps = datapoints.pointsize, i, x, y, p, prevp,
|
|
|
37 |
thresholded = $.extend({}, s); // note: shallow copy
|
|
|
38 |
|
|
|
39 |
thresholded.datapoints = { points: [], pointsize: ps };
|
|
|
40 |
thresholded.label = null;
|
|
|
41 |
thresholded.color = s.threshold.color;
|
|
|
42 |
thresholded.threshold = null;
|
|
|
43 |
thresholded.originSeries = s;
|
|
|
44 |
thresholded.data = [];
|
|
|
45 |
|
|
|
46 |
var below = s.threshold.below,
|
|
|
47 |
origpoints = datapoints.points,
|
|
|
48 |
addCrossingPoints = s.lines.show;
|
|
|
49 |
|
|
|
50 |
threspoints = [];
|
|
|
51 |
newpoints = [];
|
|
|
52 |
|
|
|
53 |
for (i = 0; i < origpoints.length; i += ps) {
|
|
|
54 |
x = origpoints[i]
|
|
|
55 |
y = origpoints[i + 1];
|
|
|
56 |
|
|
|
57 |
prevp = p;
|
|
|
58 |
if (y < below)
|
|
|
59 |
p = threspoints;
|
|
|
60 |
else
|
|
|
61 |
p = newpoints;
|
|
|
62 |
|
|
|
63 |
if (addCrossingPoints && prevp != p && x != null
|
|
|
64 |
&& i > 0 && origpoints[i - ps] != null) {
|
|
|
65 |
var interx = (x - origpoints[i - ps]) / (y - origpoints[i - ps + 1]) * (below - y) + x;
|
|
|
66 |
prevp.push(interx);
|
|
|
67 |
prevp.push(below);
|
|
|
68 |
for (m = 2; m < ps; ++m)
|
|
|
69 |
prevp.push(origpoints[i + m]);
|
|
|
70 |
|
|
|
71 |
p.push(null); // start new segment
|
|
|
72 |
p.push(null);
|
|
|
73 |
for (m = 2; m < ps; ++m)
|
|
|
74 |
p.push(origpoints[i + m]);
|
|
|
75 |
p.push(interx);
|
|
|
76 |
p.push(below);
|
|
|
77 |
for (m = 2; m < ps; ++m)
|
|
|
78 |
p.push(origpoints[i + m]);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
p.push(x);
|
|
|
82 |
p.push(y);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
datapoints.points = newpoints;
|
|
|
86 |
thresholded.datapoints.points = threspoints;
|
|
|
87 |
|
|
|
88 |
if (thresholded.datapoints.points.length > 0)
|
|
|
89 |
plot.getData().push(thresholded);
|
|
|
90 |
|
|
|
91 |
// FIXME: there are probably some edge cases left in bars
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
plot.hooks.processDatapoints.push(thresholdData);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
$.plot.plugins.push({
|
|
|
98 |
init: init,
|
|
|
99 |
options: options,
|
|
|
100 |
name: 'threshold',
|
|
|
101 |
version: '1.0'
|
|
|
102 |
});
|
|
|
103 |
})(jQuery);
|