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