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