root / hci / trunk / eneraptor-web-app / web-app / js / flot / examples / tracking.html @ 69
History | View | Annotate | Download (3.32 KB)
1 | 11 | alexbesir | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|---|---|---|
2 | <html>
|
||
3 | <head>
|
||
4 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
||
5 | <title>Flot Examples</title> |
||
6 | <link href="layout.css" rel="stylesheet" type="text/css"></link> |
||
7 | <!--[if IE]><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 | </head>
|
||
12 | <body>
|
||
13 | <h1>Flot Examples</h1> |
||
14 | |||
15 | <div id="placeholder" style="width:600px;height:300px"></div> |
||
16 | |||
17 | <p>You can add crosshairs that'll track the mouse position, either
|
||
18 | on both axes or as here on only one.</p>
|
||
19 | |||
20 | <p>If you combine it with listening on hover events, you can use
|
||
21 | it to track the intersection on the curves by interpolating |
||
22 | the data points (look at the legend).</p>
|
||
23 | |||
24 | <p id="hoverdata"></p> |
||
25 | |||
26 | <script id="source" language="javascript" type="text/javascript"> |
||
27 | var plot;
|
||
28 | $(function () {
|
||
29 | var sin = [], cos = [];
|
||
30 | for (var i = 0; i < 14; i += 0.1) {
|
||
31 | sin.push([i, Math.sin(i)]);
|
||
32 | cos.push([i, Math.cos(i)]);
|
||
33 | }
|
||
34 | |||
35 | plot = $.plot($("#placeholder"),
|
||
36 | [ { data: sin, label: "sin(x) = -0.00"},
|
||
37 | { data: cos, label: "cos(x) = -0.00" } ], {
|
||
38 | series: {
|
||
39 | lines: { show: true }
|
||
40 | },
|
||
41 | crosshair: { mode: "x" },
|
||
42 | grid: { hoverable: true, autoHighlight: false },
|
||
43 | yaxis: { min: -1.2, max: 1.2 }
|
||
44 | });
|
||
45 | var legends = $("#placeholder .legendLabel");
|
||
46 | legends.each(function () {
|
||
47 | // fix the widths so they don't jump around
|
||
48 | $(this).css('width', $(this).width());
|
||
49 | });
|
||
50 | |||
51 | var updateLegendTimeout = null;
|
||
52 | var latestPosition = null;
|
||
53 | |||
54 | function updateLegend() {
|
||
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 | var i, j, dataset = plot.getData();
|
||
65 | for (i = 0; i < dataset.length; ++i) {
|
||
66 | var series = dataset[i];
|
||
67 | |||
68 | // find the nearest points, x-wise
|
||
69 | for (j = 0; j < series.data.length; ++j)
|
||
70 | if (series.data[j][0] > pos.x)
|
||
71 | break;
|
||
72 | |||
73 | // now interpolate
|
||
74 | var y, p1 = series.data[j - 1], p2 = series.data[j];
|
||
75 | if (p1 == null)
|
||
76 | y = p2[1];
|
||
77 | else if (p2 == null)
|
||
78 | y = p1[1];
|
||
79 | else
|
||
80 | y = p1[1] + (p2[1] - p1[1]) * (pos.x - p1[0]) / (p2[0] - p1[0]);
|
||
81 | |||
82 | legends.eq(i).text(series.label.replace(/=.*/, "= " + y.toFixed(2)));
|
||
83 | }
|
||
84 | }
|
||
85 | |||
86 | $("#placeholder").bind("plothover", function (event, pos, item) {
|
||
87 | latestPosition = pos;
|
||
88 | if (!updateLegendTimeout)
|
||
89 | updateLegendTimeout = setTimeout(updateLegend, 50);
|
||
90 | });
|
||
91 | });
|
||
92 | </script>
|
||
93 | |||
94 | </body>
|
||
95 | </html> |