pandorafms/pandora_console/include/graphs/flot/jquery.flot.threshold.js

154 lines
5.1 KiB
JavaScript
Raw Normal View History

2018-02-16 13:41:31 +01:00
/* Flot plugin for thresholding data.
Copyright (c) 2007-2014 IOLA and Ole Laursen.
Licensed under the MIT license.
The plugin supports these options:
2018-05-10 08:43:09 +02:00
series: {
threshold: {
below: number,
above: mumber,
color: colorspec
}
}
2018-02-16 13:41:31 +01:00
It can also be applied to a single series, like this:
2018-05-10 08:43:09 +02:00
$.plot( $("#placeholder"), [{
data: [ ... ],
threshold: { ... }
}])
2018-02-16 13:41:31 +01:00
An array can be passed for multiple thresholding, like this:
2018-05-10 08:43:09 +02:00
threshold: [{
below: number1,
color: color1
},{
above: number2,
color: color2
}]
2018-02-16 13:41:31 +01:00
These multiple threshold objects can be passed in any order since they are
sorted by the processing function.
The data points below "below" are drawn with the specified color. This makes
it easy to mark points below 0, e.g. for budget data.
Internally, the plugin works by splitting the data into two series, above and
below the threshold. The extra series below the threshold will have its label
cleared and the special "originSeries" attribute set to the original series.
You may need to check for this in hover events.
*/
2018-05-10 08:43:09 +02:00
(function($) {
var options = {
series: { threshold: null } // or { below: number, color: color spec}
};
2018-05-10 08:43:09 +02:00
function init(plot) {
2018-05-10 08:43:09 +02:00
function thresholdData(plot, s, datapoints, below, above, color) {
var origpoints = datapoints.points,
ps = datapoints.pointsize,
addCrossingPoints = s.lines.show,
thresholded = $.extend({}, s), // note: shallow copy
threspoints = [],
newpoints = [],
prevp, i, x, y, p, m;
2018-02-16 13:41:31 +01:00
thresholded.datapoints = { points: [], pointsize: ps, format: datapoints.format };
thresholded.label = null;
thresholded.color = color;
thresholded.threshold = null;
thresholded.originSeries = s;
thresholded.data = [];
for (i = 0; i < origpoints.length; i += ps) {
2018-02-16 13:41:31 +01:00
x = origpoints[i];
y = origpoints[i + 1];
prevp = p;
2018-05-10 08:43:09 +02:00
if (y < below || y > above) {
p = threspoints;
2018-05-10 08:43:09 +02:00
} else {
p = newpoints;
2018-05-10 08:43:09 +02:00
}
2018-05-10 08:43:09 +02:00
if (addCrossingPoints && prevp !== p && x != null && i > 0 && origpoints[i - ps] != null) {
2018-02-16 13:41:31 +01:00
var interx = x + (below - y) * (x - origpoints[i - ps]) / (y - origpoints[i - ps + 1]);
prevp.push(interx);
prevp.push(below);
2018-05-10 08:43:09 +02:00
for (m = 2; m < ps; ++m) {
prevp.push(origpoints[i + m]);
2018-05-10 08:43:09 +02:00
}
p.push(null); // start new segment
p.push(null);
2018-05-10 08:43:09 +02:00
for (m = 2; m < ps; ++m) {
p.push(origpoints[i + m]);
2018-05-10 08:43:09 +02:00
}
p.push(interx);
p.push(below);
2018-05-10 08:43:09 +02:00
for (m = 2; m < ps; ++m) {
p.push(origpoints[i + m]);
2018-05-10 08:43:09 +02:00
}
}
p.push(x);
p.push(y);
2018-05-10 08:43:09 +02:00
for (m = 2; m < ps; ++m) {
2018-02-16 13:41:31 +01:00
p.push(origpoints[i + m]);
2018-05-10 08:43:09 +02:00
}
}
datapoints.points = newpoints;
thresholded.datapoints.points = threspoints;
2018-05-10 08:43:09 +02:00
2018-02-16 13:41:31 +01:00
if (thresholded.datapoints.points.length > 0) {
var origIndex = $.inArray(s, plot.getData());
// Insert newly-generated series right after original one (to prevent it from becoming top-most)
plot.getData().splice(origIndex + 1, 0, thresholded);
}
2018-05-10 08:43:09 +02:00
// FIXME: there are probably some edge cases left in bars
}
2018-05-10 08:43:09 +02:00
2018-02-16 13:41:31 +01:00
function processThresholds(plot, s, datapoints) {
2018-05-10 08:43:09 +02:00
if (!s.threshold) {
return;
2018-05-10 08:43:09 +02:00
}
2018-02-16 13:41:31 +01:00
if (s.threshold instanceof Array) {
s.threshold.sort(function(a, b) {
return a.below - b.below;
});
2018-05-10 08:43:09 +02:00
$(s.threshold).each(function(i, th) {
2018-05-10 08:43:09 +02:00
thresholdData(plot, s, datapoints, th.below, th.above, th.color);
});
2018-05-10 08:43:09 +02:00
} else {
thresholdData(plot, s, datapoints, s.threshold.below, s.threshold.above, s.threshold.color);
}
}
2018-05-10 08:43:09 +02:00
2018-02-16 13:41:31 +01:00
plot.hooks.processDatapoints.push(processThresholds);
2018-05-10 08:43:09 +02:00
function processThresholdsLegend(ctx, canvas, s) {
if (!s.threshold) {
return;
}
var color = s.threshold.color ? s.threshold.color : "black";
$(".legendLabel").each(function() {
if ($(this).text() === s.label)
{
var legend = $(this).prev().find("div > div");
legend.css("border-right-color", color);
legend.css("border-bottom-color", color);
}
});
}
plot.hooks.drawSeries.push(processThresholdsLegend);
}
2018-05-10 08:43:09 +02:00
$.plot.plugins.push({
init: init,
options: options,
2018-05-10 08:43:09 +02:00
name: "threshold",
version: "1.3"
});
2018-05-10 08:43:09 +02:00
})(jQuery);