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