Skip to content

Commit

Permalink
Major fix to line tracking - now uses plot's grid.mouseActiveRadius o…
Browse files Browse the repository at this point in the history
…ption for the threshold and is based of pixel distance instead of data. NOTE: This may break your production code, because the lines option changed from an object with track and threshold to just a boolean.
  • Loading branch information
Roundaround committed Aug 6, 2014
1 parent 3b3138c commit 17fc8ad
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
30 changes: 14 additions & 16 deletions js/jquery.flot.tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* authors: Krzysztof Urbas @krzysu [myviews.pl],Evan Steinkerchner @Roundaround
* website: https://github.com/krzysu/flot.tooltip
*
* build on 2014-08-04
* build on 2014-08-06
* released under MIT License, 2012
*/
(function ($) {
Expand All @@ -33,10 +33,7 @@
y: 20
},
defaultTheme: true,
lines: {
track: false,
threshold: 0.05
},
lines: false,

// callbacks
onHover: function (flotItem, $tooltipEl) {},
Expand Down Expand Up @@ -140,9 +137,11 @@

if (item) {
plot.showTooltip(item, pos);
} else if (that.plotOptions.series.lines.show && that.tooltipOptions.lines.track === true) {
} else if (that.plotOptions.series.lines.show && that.tooltipOptions.lines === true) {
var maxDistance = that.plotOptions.grid.mouseActiveRadius;

var closestTrace = {
distance: -1
distance: maxDistance + 1
};

$.each(plot.getData(), function (i, series) {
Expand All @@ -166,9 +165,10 @@
var pointPrev = { x: series.data[xBeforeIndex][0], y: series.data[xBeforeIndex][1] },
pointNext = { x: series.data[xAfterIndex][0], y: series.data[xAfterIndex][1] };

var distToLine = dotLineLength(pos.x, pos.y, pointPrev.x, pointPrev.y, pointNext.x, pointNext.y, false);
var distToLine = dotLineLength(series.xaxis.p2c(pos.x), series.yaxis.p2c(pos.y), series.xaxis.p2c(pointPrev.x),
series.yaxis.p2c(pointPrev.y), series.xaxis.p2c(pointNext.x), series.yaxis.p2c(pointNext.y), false);

if (distToLine < that.tooltipOptions.lines.threshold) {
if (distToLine < closestTrace.distance) {

var closestIndex = lineDistance(pointPrev.x, pointPrev.y, pos.x, pos.y) <
lineDistance(pos.x, pos.y, pointNext.x, pointNext.y) ? xBeforeIndex : xAfterIndex;
Expand All @@ -188,16 +188,14 @@
seriesIndex: i
};

if (closestTrace.distance === -1 || distToLine < closestTrace.distance) {
closestTrace = {
distance: distToLine,
item: item
};
}
closestTrace = {
distance: distToLine,
item: item
};
}
});

if (closestTrace.distance !== -1)
if (closestTrace.distance < maxDistance + 1)
plot.showTooltip(closestTrace.item, pos);
else
plot.hideTooltip();
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.flot.tooltip.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 13 additions & 15 deletions js/jquery.flot.tooltip.source.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
y: 20
},
defaultTheme: true,
lines: {
track: false,
threshold: 0.05
},
lines: false,

// callbacks
onHover: function (flotItem, $tooltipEl) {},
Expand Down Expand Up @@ -129,9 +126,11 @@

if (item) {
plot.showTooltip(item, pos);
} else if (that.plotOptions.series.lines.show && that.tooltipOptions.lines.track === true) {
} else if (that.plotOptions.series.lines.show && that.tooltipOptions.lines === true) {
var maxDistance = that.plotOptions.grid.mouseActiveRadius;

var closestTrace = {
distance: -1
distance: maxDistance + 1
};

$.each(plot.getData(), function (i, series) {
Expand All @@ -155,9 +154,10 @@
var pointPrev = { x: series.data[xBeforeIndex][0], y: series.data[xBeforeIndex][1] },
pointNext = { x: series.data[xAfterIndex][0], y: series.data[xAfterIndex][1] };

var distToLine = dotLineLength(pos.x, pos.y, pointPrev.x, pointPrev.y, pointNext.x, pointNext.y, false);
var distToLine = dotLineLength(series.xaxis.p2c(pos.x), series.yaxis.p2c(pos.y), series.xaxis.p2c(pointPrev.x),
series.yaxis.p2c(pointPrev.y), series.xaxis.p2c(pointNext.x), series.yaxis.p2c(pointNext.y), false);

if (distToLine < that.tooltipOptions.lines.threshold) {
if (distToLine < closestTrace.distance) {

var closestIndex = lineDistance(pointPrev.x, pointPrev.y, pos.x, pos.y) <
lineDistance(pos.x, pos.y, pointNext.x, pointNext.y) ? xBeforeIndex : xAfterIndex;
Expand All @@ -177,16 +177,14 @@
seriesIndex: i
};

if (closestTrace.distance === -1 || distToLine < closestTrace.distance) {
closestTrace = {
distance: distToLine,
item: item
};
}
closestTrace = {
distance: distToLine,
item: item
};
}
});

if (closestTrace.distance !== -1)
if (closestTrace.distance < maxDistance + 1)
plot.showTooltip(closestTrace.item, pos);
else
plot.hideTooltip();
Expand Down

0 comments on commit 17fc8ad

Please sign in to comment.