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