From c1d4516c4bc51916178f5d8d6bf0dc5432ba2828 Mon Sep 17 00:00:00 2001 From: Ole Laursen Date: Fri, 3 Jun 2011 17:03:44 +0200 Subject: [PATCH] Add support for multiple thresholds, patch by Arnaud Bellec --- NEWS.txt | 3 +++ jquery.flot.threshold.js | 47 +++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/NEWS.txt b/NEWS.txt index 586b258..d29b76f 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -20,6 +20,9 @@ Changes: - Support for setting the interval between redraws of the overlay canvas with redrawOverlayInterval (suggested in issue 185). +- Support for multiple thresholds in thresholds plugin (patch by + Arnaud Bellec, issue 523). + Bug fixes - Fix problem with null values and pie plugin (patch by gcruxifix, diff --git a/jquery.flot.threshold.js b/jquery.flot.threshold.js index e180139..ea04ab6 100644 --- a/jquery.flot.threshold.js +++ b/jquery.flot.threshold.js @@ -12,6 +12,19 @@ Flot plugin for thresholding data. Controlled through the option or in a specific series $.plot($("#placeholder"), [{ data: [ ... ], threshold: { ... }}]) + +An array can be passed for multiple thresholding + + threshold: [{ + below: number1 + color: color1 + },{ + below: number2 + color: color2 + }] + +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. @@ -29,22 +42,18 @@ events. }; function init(plot) { - function thresholdData(plot, s, datapoints) { - if (!s.threshold) - return; - + function thresholdData(plot, s, datapoints, below, color) { var ps = datapoints.pointsize, i, x, y, p, prevp, thresholded = $.extend({}, s); // note: shallow copy thresholded.datapoints = { points: [], pointsize: ps }; thresholded.label = null; - thresholded.color = s.threshold.color; + thresholded.color = color; thresholded.threshold = null; thresholded.originSeries = s; thresholded.data = []; - - var below = s.threshold.below, - origpoints = datapoints.points, + + var origpoints = datapoints.points, addCrossingPoints = s.lines.show; threspoints = []; @@ -93,13 +102,31 @@ events. // FIXME: there are probably some edge cases left in bars } - plot.hooks.processDatapoints.push(thresholdData); + function processThresholds(plot, s, datapoints) { + if (!s.threshold) + return; + + if (s.threshold instanceof Array) { + s.threshold.sort(function(a, b) { + return a.below - b.below; + }); + + $(s.threshold).each(function(i, th) { + thresholdData(plot, s, datapoints, th.below, th.color); + }); + } + else { + thresholdData(plot, s, datapoints, s.threshold.below, s.threshold.color); + } + } + + plot.hooks.processDatapoints.push(processThresholds); } $.plot.plugins.push({ init: init, options: options, name: 'threshold', - version: '1.1' + version: '1.2' }); })(jQuery);