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