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