Commit 7daabbb3 authored by olau@iola.dk's avatar olau@iola.dk

Integrated patch by Mark Byers for detecting bar hovering

git-svn-id: https://flot.googlecode.com/svn/trunk@88 1e0a6537-2640-0410-bfb7-f154510ff394
parent 05f5c62e
...@@ -549,9 +549,6 @@ specified, "dataIndex" will be 1, "series" is a normalized series ...@@ -549,9 +549,6 @@ specified, "dataIndex" will be 1, "series" is a normalized series
object with among other things the "Foo" label in series.label and the object with among other things the "Foo" label in series.label and the
color in series.color, and "seriesIndex" is 0. color in series.color, and "seriesIndex" is 0.
Note that the detection of nearby points is limited to points (bars
aren't working) at the moment.
If you use the above events to update some other information and want If you use the above events to update some other information and want
to clear out that info in case the mouse goes away, you'll probably to clear out that info in case the mouse goes away, you'll probably
also need to listen to "mouseout" events on the placeholder div. also need to listen to "mouseout" events on the placeholder div.
......
...@@ -9,8 +9,8 @@ compatibility hooks are in place). ...@@ -9,8 +9,8 @@ compatibility hooks are in place).
Interactivity: added a new "plothover" event and this and the Interactivity: added a new "plothover" event and this and the
"plotclick" event now returns the closest data item (based on patch by "plotclick" event now returns the closest data item (based on patch by
/david). See the revamped "interacting with the data" example for /david, patch by Mark Byers for bar support). See the revamped
some hints on what you can do. "interacting with the data" example for some hints on what you can do.
Highlighting: you can now highlight points and points are Highlighting: you can now highlight points and points are
autohighlighted when you hover over them (if hovering is turned on). autohighlighted when you hover over them (if hovering is turned on).
......
...@@ -1484,41 +1484,64 @@ ...@@ -1484,41 +1484,64 @@
// Returns the data item the mouse is over, or null if none is found // Returns the data item the mouse is over, or null if none is found
function findNearbyItem(mouseX, mouseY) { function findNearbyItem(mouseX, mouseY) {
var maxDistance = options.grid.mouseActiveRadius; var maxDistance = options.grid.mouseActiveRadius,
var lowestDistance = maxDistance * maxDistance + 1, lowestDistance = maxDistance * maxDistance + 1,
item = null; item = null, foundPoint = false;
function result(i, j) {
return { datapoint: series[i].data[j],
dataIndex: j,
series: series[i],
seriesIndex: i }
}
for (var i = 0; i < series.length; ++i) { for (var i = 0; i < series.length; ++i) {
var data = series[i].data, var data = series[i].data,
axisx = series[i].xaxis, axisx = series[i].xaxis,
axisy = series[i].yaxis; axisy = series[i].yaxis,
// precompute some stuff to make the loop faster // precompute some stuff to make the loop faster
var mx = axisx.c2p(mouseX), my = axisy.c2p(mouseY), mx = axisx.c2p(mouseX),
my = axisy.c2p(mouseY),
maxx = maxDistance / axisx.scale, maxx = maxDistance / axisx.scale,
maxy = maxDistance / axisy.scale; maxy = maxDistance / axisy.scale,
checkbar = series[i].bars.show,
checkpoint = !(series[i].bars.show && !(series[i].lines.show || series[i].points.show)),
barLeft = series[i].bars.align == "left" ? 0 : -series[i].bars.barWidth/2,
barRight = barLeft + series[i].bars.barWidth;
for (var j = 0; j < data.length; ++j) { for (var j = 0; j < data.length; ++j) {
if (data[j] == null) if (data[j] == null)
continue; continue;
// first check whether we're too far away
var x = data[j][0], y = data[j][1]; var x = data[j][0], y = data[j][1];
if (x - mx > maxx || x - mx < -maxx)
continue; if (checkbar) {
if (y - my > maxy || y - my < -maxy) // For a bar graph, the cursor must be inside the bar
// and no other point can be nearby
if (!foundPoint && mx >= x + barLeft &&
mx <= x + barRight && my <= y)
item = result(i, j);
}
if (checkpoint) {
// For points and lines, the cursor must be within a
// certain distance to the data point
// check bounding box first
if ((x - mx > maxx || x - mx < -maxx) ||
(y - my > maxy || y - my < -maxy))
continue; continue;
// We have to calculate distances in pixels, not in // We have to calculate distances in pixels, not in
// data units, because the scale of the axes may be different // data units, because the scale of the axes may be different
var dx = Math.abs(axisx.p2c(x) - mouseX), var dx = Math.abs(axisx.p2c(x) - mouseX),
dy = Math.abs(axisy.p2c(y) - mouseY); dy = Math.abs(axisy.p2c(y) - mouseY),
var dist = dx * dx + dy * dy; dist = dx * dx + dy * dy;
if (dist < lowestDistance) { if (dist < lowestDistance) {
lowestDistance = dist; lowestDistance = dist;
item = { datapoint: data[j], foundPoint = true;
dataIndex: j, item = result(i, j);
series: series[i], }
seriesIndex: i };
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment