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