Commit | Line | Data |
---|---|---|
54425b14 | 1 | <!DOCTYPE html> |
ce49c2fa DV |
2 | <html> |
3 | <head> | |
4 | <title>demo</title> | |
5 | <!--[if IE]> | |
a2b2c3a1 | 6 | <script type="text/javascript" src="../excanvas.js"></script> |
ce49c2fa | 7 | <![endif]--> |
ec7ce8a2 DV |
8 | <!-- |
9 | <script type="text/javascript" src="dygraph-combined.js"></script> | |
10 | --> | |
ce49c2fa DV |
11 | <script type="text/javascript" src="../strftime/strftime-min.js"></script> |
12 | <script type="text/javascript" src="../rgbcolor/rgbcolor.js"></script> | |
13 | <script type="text/javascript" src="../dygraph-canvas.js"></script> | |
14 | <script type="text/javascript" src="../dygraph.js"></script> | |
5c528fa2 DV |
15 | <style type="text/css"> |
16 | .annotation { | |
5c528fa2 DV |
17 | } |
18 | </style> | |
ce49c2fa DV |
19 | </head> |
20 | <body> | |
d14b9eed DV |
21 | <h3>Annotations Demo</h3> |
22 | <p>Click any point to add an annotation to it or click "Add Annotation".</p> | |
70aa9072 | 23 | <input type="button" value="Add Annotation" onclick="add()" /> |
d14b9eed | 24 | <input type="button" value="Shove to bottom" onclick="bottom(this)" /> |
70aa9072 | 25 | <div id="events"> </div> |
3bf2fa91 | 26 | <div style="position:absolute; left:200px; top: 200px;" id="g_div"></div> |
e6d53148 | 27 | <div style="position:absolute; left:700px; top: 200px;" id="list"></div> |
ce49c2fa DV |
28 | |
29 | <script type="text/javascript"> | |
e6d53148 DV |
30 | var eventDiv = document.getElementById("events"); |
31 | function nameAnnotation(ann) { | |
32 | return "(" + ann.series + ", " + ann.x + ")"; | |
33 | } | |
34 | ||
33030f33 | 35 | g = new Dygraph( |
3bf2fa91 | 36 | document.getElementById("g_div"), |
ce49c2fa DV |
37 | function() { |
38 | var zp = function(x) { if (x < 10) return "0"+x; else return x; }; | |
39 | var r = "date,parabola,line,another line,sine wave\n"; | |
40 | for (var i=1; i<=31; i++) { | |
e6d53148 DV |
41 | r += "200610" + zp(i); |
42 | r += "," + 10*(i*(31-i)); | |
43 | r += "," + 10*(8*i); | |
44 | r += "," + 10*(250 - 8*i); | |
45 | r += "," + 10*(125 + 125 * Math.sin(0.3*i)); | |
46 | r += "\n"; | |
ce49c2fa DV |
47 | } |
48 | return r; | |
49 | }, | |
50 | { | |
51 | rollPeriod: 1, | |
5c528fa2 | 52 | showRoller: true, |
ce49c2fa | 53 | width: 480, |
5c528fa2 DV |
54 | height: 320, |
55 | drawCallback: function(g) { | |
56 | var ann = g.annotations(); | |
57 | var html = ""; | |
58 | for (var i = 0; i < ann.length; i++) { | |
e6d53148 DV |
59 | var name = nameAnnotation(ann[i]); |
60 | html += "<span id='" + name + "'>" | |
61 | html += name + ": " + ann[i].shortText + " -> "; | |
62 | html += ann[i].text + "</span><br/>"; | |
5c528fa2 DV |
63 | } |
64 | document.getElementById("list").innerHTML = html; | |
3bf2fa91 | 65 | } |
ce49c2fa DV |
66 | } |
67 | ); | |
5c528fa2 DV |
68 | |
69 | var last_ann = 0; | |
70 | annotations = []; | |
71 | for (var x = 10; x < 15; x += 2) { | |
72 | annotations.push( { | |
73 | series: 'sine wave', | |
74 | x: "200610" + x, | |
75 | shortText: x, | |
76 | text: 'Stock Market Crash ' + x | |
77 | } ); | |
78 | last_ann = x; | |
79 | } | |
80 | annotations.push( { | |
81 | series: 'another line', | |
82 | x: "20061013", | |
ce5e8d36 | 83 | icon: 'dollar.png', |
33030f33 DV |
84 | width: 18, |
85 | height: 23, | |
ce5e8d36 | 86 | tickHeight: 4, |
5c528fa2 | 87 | text: 'Another one', |
ab5e5c75 DV |
88 | cssClass: 'annotation', |
89 | clickHandler: function() { | |
90 | document.getElementById("events").innerHTML += "special handler<br/>"; | |
91 | } | |
5c528fa2 DV |
92 | } ); |
93 | g.setAnnotations(annotations); | |
94 | ||
95 | function add() { | |
96 | var x = last_ann + 2; | |
97 | var annnotations = g.annotations(); | |
98 | annotations.push( { | |
99 | series: 'line', | |
100 | x: "200610" + x, | |
101 | shortText: x, | |
9a40897e DV |
102 | text: 'Line ' + x, |
103 | tickHeight: 10 | |
5c528fa2 DV |
104 | } ); |
105 | last_ann = x; | |
106 | g.setAnnotations(annotations); | |
107 | } | |
ab5e5c75 | 108 | |
d14b9eed DV |
109 | function bottom(el) { |
110 | var to_bottom = true; | |
111 | if (el.value != 'Shove to bottom') to_bottom = false; | |
112 | ||
113 | var anns = g.annotations(); | |
114 | for (var i = 0; i < anns.length; i++) { | |
115 | anns[i].attachAtBottom = to_bottom; | |
116 | } | |
117 | g.setAnnotations(anns); | |
118 | ||
119 | if (to_bottom) { | |
120 | el.value = 'Lift back up'; | |
121 | } else { | |
122 | el.value = 'Shove to bottom'; | |
123 | } | |
124 | } | |
125 | ||
e6d53148 | 126 | var saveBg = ''; |
2ad87eaa | 127 | var num = 0; |
ab5e5c75 DV |
128 | g.updateOptions( { |
129 | annotationClickHandler: function(ann, point, dg, event) { | |
70aa9072 DV |
130 | eventDiv.innerHTML += "click: " + nameAnnotation(ann) + "<br/>"; |
131 | }, | |
132 | annotationDblClickHandler: function(ann, point, dg, event) { | |
133 | eventDiv.innerHTML += "dblclick: " + nameAnnotation(ann) + "<br/>"; | |
134 | }, | |
135 | annotationMouseOverHandler: function(ann, point, dg, event) { | |
e6d53148 DV |
136 | document.getElementById(nameAnnotation(ann)).style.fontWeight = 'bold'; |
137 | saveBg = ann.div.style.backgroundColor; | |
138 | ann.div.style.backgroundColor = '#ddd'; | |
70aa9072 DV |
139 | }, |
140 | annotationMouseOutHandler: function(ann, point, dg, event) { | |
e6d53148 DV |
141 | document.getElementById(nameAnnotation(ann)).style.fontWeight = 'normal'; |
142 | ann.div.style.backgroundColor = saveBg; | |
2ad87eaa DV |
143 | }, |
144 | ||
145 | pointClickCallback: function(event, p) { | |
146 | // Check if the point is already annotated. | |
147 | if (p.annotation) return; | |
148 | ||
149 | // If not, add one. | |
150 | var ann = { | |
151 | series: p.name, | |
152 | xval: p.xval, | |
153 | shortText: num, | |
154 | text: "Annotation #" + num | |
155 | }; | |
156 | var anns = g.annotations(); | |
157 | anns.push(ann); | |
158 | g.setAnnotations(anns); | |
159 | ||
160 | num++; | |
ab5e5c75 DV |
161 | } |
162 | }); | |
ce49c2fa DV |
163 | </script> |
164 | </body> | |
165 | </html> |