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