Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed lines drawn between above intersection #1226

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 50 additions & 39 deletions jquery.flot.threshold.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ Licensed under the MIT license.


The plugin supports these options: The plugin supports these options:


series: { series: {
threshold: { threshold: {
below: number below: number,
color: colorspec above: mumber,
} color: colorspec
} }
}


It can also be applied to a single series, like this: It can also be applied to a single series, like this:


$.plot( $("#placeholder"), [{ $.plot( $("#placeholder"), [{
data: [ ... ], data: [ ... ],
threshold: { ... } threshold: { ... }
}]) }])


An array can be passed for multiple thresholding, like this: An array can be passed for multiple thresholding, like this:


threshold: [{ threshold: [{
below: number1 below: number1,
color: color1 color: color1
},{ },{
below: number2 above: number2,
color: color2 color: color2
}] }]


These multiple threshold objects can be passed in any order since they are These multiple threshold objects can be passed in any order since they are
sorted by the processing function. sorted by the processing function.
Expand All @@ -46,9 +47,9 @@ You may need to check for this in hover events.
var options = { var options = {
series: { threshold: null } // or { below: number, color: color spec} series: { threshold: null } // or { below: number, color: color spec}
}; };

function init(plot) { function init(plot) {
function thresholdData(plot, s, datapoints, below, color) { function thresholdData(plot, s, datapoints, below, above, color) {
var ps = datapoints.pointsize, i, x, y, p, prevp, var ps = datapoints.pointsize, i, x, y, p, prevp,
thresholded = $.extend({}, s); // note: shallow copy thresholded = $.extend({}, s); // note: shallow copy


Expand All @@ -58,7 +59,7 @@ You may need to check for this in hover events.
thresholded.threshold = null; thresholded.threshold = null;
thresholded.originSeries = s; thresholded.originSeries = s;
thresholded.data = []; thresholded.data = [];

var origpoints = datapoints.points, var origpoints = datapoints.points,
addCrossingPoints = s.lines.show; addCrossingPoints = s.lines.show;


Expand All @@ -71,25 +72,35 @@ You may need to check for this in hover events.
y = origpoints[i + 1]; y = origpoints[i + 1];


prevp = p; prevp = p;
if (y < below)
if (y < below){
p = threspoints; p = threspoints;
else limit = below;
modifier = 1;
}
else if(y > above){
p = threspoints;
limit = above;
modifier = -1;
}
else{
p = newpoints; p = newpoints;
}


if (addCrossingPoints && prevp != p && x != null if (addCrossingPoints && prevp !== p && x !== null
&& i > 0 && origpoints[i - ps] != null) { && i > 0 && origpoints[i - ps] !== null) {
var interx = x + (below - y) * (x - origpoints[i - ps]) / (y - origpoints[i - ps + 1]); var interx = x + (limit - y) * (x - origpoints[i - ps]) / (y - origpoints[i - ps + 1]);
prevp.push(interx); prevp.push(interx);
prevp.push(below); prevp.push(limit);
for (m = 2; m < ps; ++m) for (m = 2; m < ps; ++m)
prevp.push(origpoints[i + m]); prevp.push(origpoints[i + m]);

p.push(null); // start new segment p.push(null); // start new segment
p.push(null); p.push(null);
for (m = 2; m < ps; ++m) for (m = 2; m < ps; ++m)
p.push(origpoints[i + m]); p.push(origpoints[i + m]);
p.push(interx); p.push(interx);
p.push(below); p.push(limit);
for (m = 2; m < ps; ++m) for (m = 2; m < ps; ++m)
p.push(origpoints[i + m]); p.push(origpoints[i + m]);
} }
Expand All @@ -100,43 +111,43 @@ You may need to check for this in hover events.
p.push(origpoints[i + m]); p.push(origpoints[i + m]);
} }


datapoints.points = newpoints; // datapoints.points = newpoints;
thresholded.datapoints.points = threspoints; thresholded.datapoints.points = threspoints;

if (thresholded.datapoints.points.length > 0) { if (thresholded.datapoints.points.length > 0) {
var origIndex = $.inArray(s, plot.getData()); var origIndex = $.inArray(s, plot.getData());
// Insert newly-generated series right after original one (to prevent it from becoming top-most) // Insert newly-generated series right after original one (to prevent it from becoming top-most)
plot.getData().splice(origIndex + 1, 0, thresholded); plot.getData().splice(origIndex + 1, 0, thresholded);
} }

// FIXME: there are probably some edge cases left in bars // FIXME: there are probably some edge cases left in bars
} }

function processThresholds(plot, s, datapoints) { function processThresholds(plot, s, datapoints) {
if (!s.threshold) if (!s.threshold)
return; return;

if (s.threshold instanceof Array) { if (s.threshold instanceof Array) {
s.threshold.sort(function(a, b) { s.threshold.sort(function(a, b) {
return a.below - b.below; return a.below - b.below;
}); });

$(s.threshold).each(function(i, th) { $(s.threshold).each(function(i, th) {
thresholdData(plot, s, datapoints, th.below, th.color); thresholdData(plot, s, datapoints, th.below, th.above, th.color);
}); });
} }
else { else {
thresholdData(plot, s, datapoints, s.threshold.below, s.threshold.color); thresholdData(plot, s, datapoints, s.threshold.below, s.threshold.above, s.threshold.color);
} }
} }

plot.hooks.processDatapoints.push(processThresholds); plot.hooks.processDatapoints.push(processThresholds);
} }

$.plot.plugins.push({ $.plot.plugins.push({
init: init, init: init,
options: options, options: options,
name: 'threshold', name: 'threshold',
version: '1.2' version: '1.3'
}); });
})(jQuery); })(jQuery);