| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 5 |
<title>Flot Examples: Tracking</title> |
| 6 |
<link href="../examples.css" rel="stylesheet" type="text/css"> |
| 7 |
<script language="javascript" type="text/javascript" src="../../source/jquery.js"></script> |
| 8 |
<script language="javascript" type="text/javascript" src="../../source/jquery.canvaswrapper.js"></script> |
| 9 |
<script language="javascript" type="text/javascript" src="../../source/jquery.colorhelpers.js"></script> |
| 10 |
<script language="javascript" type="text/javascript" src="../../source/jquery.flot.js"></script> |
| 11 |
<script language="javascript" type="text/javascript" src="../../source/jquery.flot.saturated.js"></script> |
| 12 |
<script language="javascript" type="text/javascript" src="../../source/jquery.flot.browser.js"></script> |
| 13 |
<script language="javascript" type="text/javascript" src="../../source/jquery.flot.drawSeries.js"></script> |
| 14 |
<script language="javascript" type="text/javascript" src="../../source/jquery.flot.uiConstants.js"></script> |
| 15 |
<script language="javascript" type="text/javascript" src="../../source/jquery.flot.navigate.js"></script> |
| 16 |
<script language="javascript" type="text/javascript" src="../../source/jquery.flot.touchNavigate.js"></script> |
| 17 |
<script language="javascript" type="text/javascript" src="../../source/jquery.flot.hover.js"></script> |
| 18 |
<script language="javascript" type="text/javascript" src="../../source/jquery.flot.touch.js"></script> |
| 19 |
<script language="javascript" type="text/javascript" src="../../source/jquery.flot.legend.js"></script> |
| 20 |
<script language="javascript" type="text/javascript" src="../../source/jquery.flot.crosshair.js"></script> |
| 21 |
<script type="text/javascript"> |
| 22 |
|
| 23 |
$(function() { |
| 24 |
|
| 25 |
var sin = [], cos = []; |
| 26 |
for (var i = 0; i < 14; i += 0.1) { |
| 27 |
sin.push([i, Math.sin(i)]); |
| 28 |
cos.push([i, Math.cos(i)]); |
| 29 |
} |
| 30 |
|
| 31 |
plot = $.plot("#placeholder", [ |
| 32 |
{ data: sin, label: "sin(x) = -0.00"}, |
| 33 |
{ data: cos, label: "cos(x) = -0.00" } |
| 34 |
], { |
| 35 |
legend: { |
| 36 |
show: true |
| 37 |
}, |
| 38 |
series: { |
| 39 |
lines: { |
| 40 |
show: true, |
| 41 |
lineWidth: 2 |
| 42 |
} |
| 43 |
}, |
| 44 |
crosshair: { |
| 45 |
mode: "xy" |
| 46 |
}, |
| 47 |
grid: { |
| 48 |
hoverable: true, |
| 49 |
clickable: true, |
| 50 |
autoHighlight: false |
| 51 |
}, |
| 52 |
yaxis: { |
| 53 |
min: -1.2, |
| 54 |
max: 1.2 |
| 55 |
} |
| 56 |
}); |
| 57 |
|
| 58 |
var legends = $("#placeholder .legendLayer text tspan"); |
| 59 |
|
| 60 |
legends.each(function () { |
| 61 |
// fix the widths so they don't jump around |
| 62 |
$(this).css('width', $(this).width()); |
| 63 |
}); |
| 64 |
|
| 65 |
var updateLegendTimeout = null; |
| 66 |
var latestPosition = null; |
| 67 |
|
| 68 |
function updateLegend() { |
| 69 |
|
| 70 |
updateLegendTimeout = null; |
| 71 |
var pos = latestPosition; |
| 72 |
var axes = plot.getAxes(); |
| 73 |
|
| 74 |
if (pos.x < axes.xaxis.min || pos.x > axes.xaxis.max || |
| 75 |
pos.y < axes.yaxis.min || pos.y > axes.yaxis.max) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
var i, j, dataset = plot.getData(); |
| 80 |
|
| 81 |
for (i = 0; i < dataset.length; ++i) { |
| 82 |
var series = dataset[i]; |
| 83 |
// Find the nearest points, x-wise |
| 84 |
for (j = 0; j < series.data.length; ++j) { |
| 85 |
if (series.data[j][0] > pos.x) { |
| 86 |
break; |
| 87 |
} |
| 88 |
} |
| 89 |
// Now Interpolate |
| 90 |
var y, |
| 91 |
p1 = series.data[j - 1], |
| 92 |
p2 = series.data[j]; |
| 93 |
if (p1 == null) { |
| 94 |
y = p2[1]; |
| 95 |
} else if (p2 == null) { |
| 96 |
y = p1[1]; |
| 97 |
} else { |
| 98 |
y = p1[1] + (p2[1] - p1[1]) * (pos.x - p1[0]) / (p2[0] - p1[0]); |
| 99 |
} |
| 100 |
legends.eq(i).text(series.label.replace(/=.*/, "= " + y.toFixed(2))); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
$("#placeholder").bind("plothover", function (event, pos, item) { |
| 105 |
latestPosition = pos; |
| 106 |
if (!updateLegendTimeout) { |
| 107 |
updateLegendTimeout = setTimeout(updateLegend, 50); |
| 108 |
} |
| 109 |
}).bind("plotclick", function (event, pos, item) { |
| 110 |
plot.lockCrosshair(pos); |
| 111 |
});; |
| 112 |
|
| 113 |
// Add the Flot version string to the footer |
| 114 |
|
| 115 |
$("#footer").prepend("Flot " + $.plot.version + " – "); |
| 116 |
}); |
| 117 |
|
| 118 |
</script> |
| 119 |
</head> |
| 120 |
<body> |
| 121 |
|
| 122 |
<div id="header"> |
| 123 |
<h2>Tracking</h2> |
| 124 |
</div> |
| 125 |
|
| 126 |
<div id="content"> |
| 127 |
|
| 128 |
<div class="demo-container"> |
| 129 |
<div id="placeholder" class="demo-placeholder"></div> |
| 130 |
</div> |
| 131 |
|
| 132 |
<p>You can add crosshairs that'll track the mouse position, either on both axes or as here on only one.</p> |
| 133 |
|
| 134 |
<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> |
| 135 |
|
| 136 |
<p id="hoverdata"></p> |
| 137 |
|
| 138 |
</div> |
| 139 |
|
| 140 |
<div id="footer"> |
| 141 |
Copyright © 2007 - 2014 IOLA and Ole Laursen |
| 142 |
</div> |
| 143 |
|
| 144 |
</body> |
| 145 |
</html> |