2016-04-24 14:26:11 +00:00
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" >
< title > Flot Examples: Tracking< / title >
< link href = "../examples.css" rel = "stylesheet" type = "text/css" >
2019-10-23 10:34:21 +00:00
<!-- [if lte IE 8]><script language="javascript" type="text/javascript" src="../../excanvas.min.js"></script><![endif] -->
< script language = "javascript" type = "text/javascript" src = "../../jquery.js" > < / script >
< script language = "javascript" type = "text/javascript" src = "../../jquery.flot.js" > < / script >
< script language = "javascript" type = "text/javascript" src = "../../jquery.flot.crosshair.js" > < / script >
2016-04-24 14:26:11 +00:00
< script type = "text/javascript" >
$(function() {
var sin = [], cos = [];
for (var i = 0; i < 14 ; i + = 0 . 1 ) {
sin.push([i, Math.sin(i)]);
cos.push([i, Math.cos(i)]);
}
plot = $.plot("#placeholder", [
{ data: sin, label: "sin(x) = -0.00"},
{ data: cos, label: "cos(x) = -0.00" }
], {
series: {
lines: {
2019-10-23 10:34:21 +00:00
show: true
2016-04-24 14:26:11 +00:00
}
},
crosshair: {
2019-10-23 10:34:21 +00:00
mode: "x"
2016-04-24 14:26:11 +00:00
},
grid: {
hoverable: true,
autoHighlight: false
},
yaxis: {
min: -1.2,
max: 1.2
}
});
2019-10-23 10:34:21 +00:00
var legends = $("#placeholder .legendLabel");
2016-04-24 14:26:11 +00:00
legends.each(function () {
// fix the widths so they don't jump around
$(this).css('width', $(this).width());
});
var updateLegendTimeout = null;
var latestPosition = null;
function updateLegend() {
updateLegendTimeout = null;
2019-10-23 10:34:21 +00:00
2016-04-24 14:26:11 +00:00
var pos = latestPosition;
2019-04-08 16:55:26 +00:00
2019-10-23 10:34:21 +00:00
var axes = plot.getAxes();
2016-04-24 14:26:11 +00:00
if (pos.x < axes.xaxis.min | | pos . x > axes.xaxis.max ||
pos.y < axes.yaxis.min | | pos . y > axes.yaxis.max) {
return;
}
var i, j, dataset = plot.getData();
2019-04-08 16:55:26 +00:00
for (i = 0; i < dataset.length ; + + i ) {
2019-10-23 10:34:21 +00:00
2016-04-24 14:26:11 +00:00
var series = dataset[i];
2019-10-23 10:34:21 +00:00
2016-04-24 14:26:11 +00:00
// Find the nearest points, x-wise
2019-10-23 10:34:21 +00:00
2016-04-24 14:26:11 +00:00
for (j = 0; j < series.data.length ; + + j ) {
if (series.data[j][0] > pos.x) {
break;
}
}
2019-10-23 10:34:21 +00:00
2016-04-24 14:26:11 +00:00
// Now Interpolate
2019-10-23 10:34:21 +00:00
2016-04-24 14:26:11 +00:00
var y,
p1 = series.data[j - 1],
p2 = series.data[j];
2019-10-23 10:34:21 +00:00
2016-04-24 14:26:11 +00:00
if (p1 == null) {
y = p2[1];
} else if (p2 == null) {
y = p1[1];
} else {
y = p1[1] + (p2[1] - p1[1]) * (pos.x - p1[0]) / (p2[0] - p1[0]);
}
2019-10-23 10:34:21 +00:00
2016-04-24 14:26:11 +00:00
legends.eq(i).text(series.label.replace(/=.*/, "= " + y.toFixed(2)));
}
}
2019-10-23 10:34:21 +00:00
$("#placeholder").bind("plothover", function (event, pos, item) {
2016-04-24 14:26:11 +00:00
latestPosition = pos;
if (!updateLegendTimeout) {
updateLegendTimeout = setTimeout(updateLegend, 50);
}
2019-10-23 10:34:21 +00:00
});
2016-04-24 14:26:11 +00:00
// Add the Flot version string to the footer
$("#footer").prepend("Flot " + $.plot.version + " – ");
});
< / script >
< / head >
< body >
< div id = "header" >
< h2 > Tracking< / h2 >
< / div >
< div id = "content" >
< div class = "demo-container" >
< div id = "placeholder" class = "demo-placeholder" > < / div >
< / div >
< p > You can add crosshairs that'll track the mouse position, either on both axes or as here on only one.< / p >
< p > If you combine it with listening on hover events, you can use it to track the intersection on the curves by interpolating the data points (look at the legend).< / p >
< p id = "hoverdata" > < / p >
< / div >
< div id = "footer" >
Copyright © 2007 - 2014 IOLA and Ole Laursen
< / div >
< / body >
< / html >