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