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