New approach to fix NaN handling in highlighting
[dygraphs.git] / gallery / link-interaction.js
1 Gallery.register(
2 'link-interaction',
3 {
4 name: 'Link Interaction',
5 setup: function(parent) {
6 parent.innerHTML = [
7 "<div id='div_g'></div>",
8 "<b>Zoom:</b>",
9 "<a href='#' id='hour'>hour</a> ",
10 "<a href='#' id='day'>day</a> ",
11 "<a href='#' id='week'>week</a> ",
12 "<a href='#' id='month'>month</a> ",
13 "<a href='#' id='full'>full</a> ",
14 "<b>Pan:</b> ",
15 "<a href='#' id='left'>left</a> ",
16 "<a href='#' id='right'>right</a> "].join("\n");
17 },
18 run: function() {
19 var r = [ ];
20 var base_time = Date.parse("2008/07/01");
21 var num = 24 * 0.25 * 365;
22 for (var i = 0; i < num; i++) {
23 r.push([ new Date(base_time + i * 3600 * 1000),
24 i + 50 * (i % 60), // line
25 i * (num - i) * 4.0 / num // parabola
26 ]);
27 }
28 var orig_range = [ r[0][0].valueOf(), r[r.length - 1][0].valueOf() ];
29 g = new Dygraph(
30 document.getElementById("div_g"),
31 r, {
32 rollPeriod: 7,
33 animatedZooms: true,
34 // errorBars: true,
35 width: 600,
36 height: 300,
37 labels: ["Date", "a", "b"]
38 }
39 );
40
41 var desired_range = null;
42 function approach_range() {
43 if (!desired_range) return;
44 // go halfway there
45 var range = g.xAxisRange();
46 if (Math.abs(desired_range[0] - range[0]) < 60 &&
47 Math.abs(desired_range[1] - range[1]) < 60) {
48 g.updateOptions({dateWindow: desired_range});
49 // (do not set another timeout.)
50 } else {
51 var new_range;
52 new_range = [0.5 * (desired_range[0] + range[0]),
53 0.5 * (desired_range[1] + range[1])];
54 g.updateOptions({dateWindow: new_range});
55 animate();
56 }
57 }
58 function animate() {
59 setTimeout(approach_range, 50);
60 }
61
62 var zoom = function(res) {
63 var w = g.xAxisRange();
64 desired_range = [ w[0], w[0] + res * 1000 ];
65 animate();
66 }
67
68 var reset = function() {
69 desired_range = orig_range;
70 animate();
71 }
72
73 var pan = function(dir) {
74 var w = g.xAxisRange();
75 var scale = w[1] - w[0];
76 var amount = scale * 0.25 * dir;
77 desired_range = [ w[0] + amount, w[1] + amount ];
78 animate();
79 }
80
81 document.getElementById('hour').onclick = function() { zoom(3600); };
82 document.getElementById('day').onclick = function() { zoom(86400); };
83 document.getElementById('week').onclick = function() { zoom(604800); };
84 document.getElementById('month').onclick = function() { zoom(30 * 86400); };
85 document.getElementById('full').onclick = function() { reset(); };
86 document.getElementById('left').onclick = function() { pan(-1); };
87 document.getElementById('right').onclick = function() { pan(+1); };
88 }
89 });