Merge pull request #761 from justinsg/master
[dygraphs.git] / tests / annotation.html
CommitLineData
54425b14 1<!DOCTYPE html>
ce49c2fa
DV
2<html>
3 <head>
4 <title>demo</title>
ec7ce8a2
DV
5 <!--
6 <script type="text/javascript" src="dygraph-combined.js"></script>
7 -->
7e5ddc94
DV
8 <!--
9 For production (minified) code, use:
10 <script type="text/javascript" src="dygraph-combined.js"></script>
11 -->
fbd6834a 12 <script type="text/javascript" src="../dist/dygraph.js"></script>
7e5ddc94 13
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
af6e4ad5
DV
34 annotations = [];
35 var graph_initialized = false;
36
33030f33 37 g = new Dygraph(
3bf2fa91 38 document.getElementById("g_div"),
ce49c2fa
DV
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++) {
e6d53148
DV
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";
ce49c2fa
DV
49 }
50 return r;
51 },
52 {
53 rollPeriod: 1,
5c528fa2 54 showRoller: true,
ce49c2fa 55 width: 480,
5c528fa2 56 height: 320,
af6e4ad5
DV
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
5c528fa2
DV
65 var ann = g.annotations();
66 var html = "";
67 for (var i = 0; i < ann.length; i++) {
e6d53148
DV
68 var name = nameAnnotation(ann[i]);
69 html += "<span id='" + name + "'>"
135d9962
DV
70 html += name + ": " + (ann[i].shortText || '(icon)')
71 html += " -> " + ann[i].text + "</span><br/>";
5c528fa2
DV
72 }
73 document.getElementById("list").innerHTML = html;
3bf2fa91 74 }
ce49c2fa
DV
75 }
76 );
5c528fa2
DV
77
78 var last_ann = 0;
5c528fa2
DV
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",
ce5e8d36 91 icon: 'dollar.png',
33030f33
DV
92 width: 18,
93 height: 23,
ce5e8d36 94 tickHeight: 4,
5c528fa2 95 text: 'Another one',
ab5e5c75
DV
96 cssClass: 'annotation',
97 clickHandler: function() {
98 document.getElementById("events").innerHTML += "special handler<br/>";
99 }
5c528fa2 100 } );
9bb94ee7
DV
101 annotations.push( {
102 series: 'parabola',
103 x: '20061012',
104 shortText: 'P',
105 text: 'Parabola Annotation at same x-coord'
106 } );
af6e4ad5
DV
107
108 if (graph_initialized) {
109 g.setAnnotations(annotations);
110 }
5c528fa2
DV
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,
9a40897e
DV
119 text: 'Line ' + x,
120 tickHeight: 10
5c528fa2
DV
121 } );
122 last_ann = x;
123 g.setAnnotations(annotations);
124 }
ab5e5c75 125
d14b9eed
DV
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
e6d53148 143 var saveBg = '';
2ad87eaa 144 var num = 0;
ab5e5c75
DV
145 g.updateOptions( {
146 annotationClickHandler: function(ann, point, dg, event) {
70aa9072
DV
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) {
e6d53148
DV
153 document.getElementById(nameAnnotation(ann)).style.fontWeight = 'bold';
154 saveBg = ann.div.style.backgroundColor;
155 ann.div.style.backgroundColor = '#ddd';
70aa9072
DV
156 },
157 annotationMouseOutHandler: function(ann, point, dg, event) {
e6d53148
DV
158 document.getElementById(nameAnnotation(ann)).style.fontWeight = 'normal';
159 ann.div.style.backgroundColor = saveBg;
2ad87eaa
DV
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++;
ab5e5c75
DV
178 }
179 });
ce49c2fa
DV
180 </script>
181</body>
182</html>