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