index.html 5.4 KB

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