1 /* Flot plugin for thresholding data.
3 Copyright (c) 2007-2013 IOLA and Ole Laursen.
4 Licensed under the MIT license.
6 The plugin supports these options:
15 It can also be applied to a single series, like this:
17 $.plot( $("#placeholder"), [{
22 An array can be passed for multiple thresholding, like this:
32 These multiple threshold objects can be passed in any order since they are
33 sorted by the processing function.
35 The data points below "below" are drawn with the specified color. This makes
36 it easy to mark points below 0, e.g. for budget data.
38 Internally, the plugin works by splitting the data into two series, above and
39 below the threshold. The extra series below the threshold will have its label
40 cleared and the special "originSeries" attribute set to the original series.
41 You may need to check for this in hover events.
47 series: { threshold: null } // or { below: number, color: color spec}
51 function thresholdData(plot, s, datapoints, below, color) {
52 var ps = datapoints.pointsize, i, x, y, p, prevp,
53 thresholded = $.extend({}, s); // note: shallow copy
55 thresholded.datapoints = { points: [], pointsize: ps, format: datapoints.format };
56 thresholded.label = null;
57 thresholded.color = color;
58 thresholded.threshold = null;
59 thresholded.originSeries = s;
60 thresholded.data = [];
62 var origpoints = datapoints.points,
63 addCrossingPoints = s.lines.show;
69 for (i = 0; i < origpoints.length; i += ps) {
71 y = origpoints[i + 1];
79 if (addCrossingPoints && prevp != p && x != null
80 && i > 0 && origpoints[i - ps] != null) {
81 var interx = x + (below - y) * (x - origpoints[i - ps]) / (y - origpoints[i - ps + 1]);
84 for (m = 2; m < ps; ++m)
85 prevp.push(origpoints[i + m]);
87 p.push(null); // start new segment
89 for (m = 2; m < ps; ++m)
90 p.push(origpoints[i + m]);
93 for (m = 2; m < ps; ++m)
94 p.push(origpoints[i + m]);
99 for (m = 2; m < ps; ++m)
100 p.push(origpoints[i + m]);
103 datapoints.points = newpoints;
104 thresholded.datapoints.points = threspoints;
106 if (thresholded.datapoints.points.length > 0) {
107 var origIndex = $.inArray(s, plot.getData());
108 // Insert newly-generated series right after original one (to prevent it from becoming top-most)
109 plot.getData().splice(origIndex + 1, 0, thresholded);
112 // FIXME: there are probably some edge cases left in bars
115 function processThresholds(plot, s, datapoints) {
119 if (s.threshold instanceof Array) {
120 s.threshold.sort(function(a, b) {
121 return a.below - b.below;
124 $(s.threshold).each(function(i, th) {
125 thresholdData(plot, s, datapoints, th.below, th.color);
129 thresholdData(plot, s, datapoints, s.threshold.below, s.threshold.color);
133 plot.hooks.processDatapoints.push(processThresholds);
136 $.plot.plugins.push({