4 <link rel=
"stylesheet" href=
"../dist/dygraph.css">
6 <script type=
"text/javascript" src=
"../dist/dygraph.js"></script>
8 <style type=
"text/css">
14 <h3>Annotations Demo
</h3>
15 <p>Click any point to add an annotation to it or click
"Add Annotation".
</p>
16 <input type=
"button" value=
"Add Annotation" onclick=
"add()" />
17 <input type=
"button" value=
"Shove to bottom" onclick=
"bottom(this)" />
18 <div id=
"events"> </div>
19 <div style=
"position:absolute; left:200px; top: 200px;" id=
"g_div"></div>
20 <div style=
"position:absolute; left:700px; top: 200px;" id=
"list"></div>
22 <script type=
"text/javascript">
23 var eventDiv = document.getElementById(
"events");
24 function nameAnnotation(ann) {
25 return
"(" + ann.series +
", " + ann.x +
")";
29 var graph_initialized = false;
32 document.getElementById(
"g_div"),
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++) {
37 r +=
"200610" + zp(i);
38 r +=
"," +
10*(i*(
31-i));
40 r +=
"," +
10*(
250 -
8*i);
41 r +=
"," +
10*(
125 +
125 * Math.sin(
0.3*i));
51 drawCallback: function(g, is_initial) {
53 graph_initialized = true;
54 if (annotations.length
> 0) {
55 g.setAnnotations(annotations);
59 var ann = g.annotations();
61 for (var i =
0; i < ann.length; i++) {
62 var name = nameAnnotation(ann[i]);
63 html +=
"<span id='" + name +
"'>"
64 html += name +
": " + (ann[i].shortText || '(icon)')
65 html +=
" -> " + ann[i].text +
"</span><br/>";
67 document.getElementById(
"list").innerHTML = html;
73 for (var x =
10; x <
15; x +=
2) {
78 text: 'Stock Market Crash ' + x
83 series: 'another line',
89 tickColor: 'indianred',
92 cssClass: 'annotation',
93 clickHandler: function() {
94 document.getElementById(
"events").innerHTML +=
"special handler<br/>";
101 text: 'Parabola Annotation at same x-coord'
104 if (graph_initialized) {
105 g.setAnnotations(annotations);
109 var x = last_ann +
2;
110 var annnotations = g.annotations();
119 g.setAnnotations(annotations);
122 function bottom(el) {
123 var to_bottom = true;
124 if (el.value != 'Shove to bottom') to_bottom = false;
126 var anns = g.annotations();
127 for (var i =
0; i < anns.length; i++) {
128 anns[i].attachAtBottom = to_bottom;
130 g.setAnnotations(anns);
133 el.value = 'Lift back up';
135 el.value = 'Shove to bottom';
142 annotationClickHandler: function(ann, point, dg, event) {
143 eventDiv.innerHTML +=
"click: " + nameAnnotation(ann) +
"<br/>";
145 annotationDblClickHandler: function(ann, point, dg, event) {
146 eventDiv.innerHTML +=
"dblclick: " + nameAnnotation(ann) +
"<br/>";
148 annotationMouseOverHandler: function(ann, point, dg, event) {
149 document.getElementById(nameAnnotation(ann)).style.fontWeight = 'bold';
150 saveBg = ann.div.style.backgroundColor;
151 ann.div.style.backgroundColor = '#ddd';
153 annotationMouseOutHandler: function(ann, point, dg, event) {
154 document.getElementById(nameAnnotation(ann)).style.fontWeight = 'normal';
155 ann.div.style.backgroundColor = saveBg;
158 pointClickCallback: function(event, p) {
159 // Check if the point is already annotated.
160 if (p.annotation) return;
167 text:
"Annotation #" + num
169 var anns = g.annotations();
171 g.setAnnotations(anns);