index.html 3.1 KB

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