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