1 /*global Gallery,Dygraph,data */
6 title
: 'Dynamic Annotations Demo',
7 setup
: function(parent
) {
9 "<p>Click any point to add an annotation to it or click 'Add Annotation'.</p>",
10 "<button id='add'>Add Annotation></button>",
11 "<button id='bottom'>Shove to bottom</button>",
12 "<div id='list'></div>",
13 "<div id='g_div'></div>",
14 "<div id='events'></div>" ].join("\n");
18 var eventDiv
= document
.getElementById("events");
19 function nameAnnotation(ann
) {
20 return "(" + ann
.series
+ ", " + ann
.x
+ ")";
24 document
.getElementById("g_div"),
26 var zp
= function(x
) { if (x
< 10) return "0"+x
; else return x
; };
27 var r
= "date,parabola,line,another line,sine wave\n";
28 for (var i
=1; i
<=31; i
++) {
29 r
+= "200610" + zp(i
);
30 r
+= "," + 10*(i
*(31-i
));
32 r
+= "," + 10*(250 - 8*i
);
33 r
+= "," + 10*(125 + 125 * Math
.sin(0.3*i
));
43 drawCallback
: function(g
) {
44 var ann
= g
.annotations();
46 for (var i
= 0; i
< ann
.length
; i
++) {
47 var name
= nameAnnotation(ann
[i
]);
48 html
+= "<span id='" + name
+ "'>";
49 html
+= name
+ ": " + (ann
[i
].shortText
|| '(icon)');
50 html
+= " -> " + ann
[i
].text
+ "</span><br/>";
52 document
.getElementById("list").innerHTML
= html
;
59 for (var x
= 10; x
< 15; x
+= 2) {
64 text
: 'Stock Market Crash ' + x
69 series
: 'another line',
71 icon
: 'images/dollar.png',
76 cssClass
: 'annotation',
77 clickHandler
: function() {
78 eventDiv
.innerHTML
+= "special handler<br/>";
81 g
.setAnnotations(annotations
);
83 document
.getElementById('add').onclick
= function() {
93 g
.setAnnotations(annotations
);
96 var bottom
= document
.getElementById('bottom');
98 bottom
.onclick
= function() {
99 var to_bottom
= bottom
.textContent
== 'Shove to bottom';
101 var anns
= g
.annotations();
102 for (var i
= 0; i
< anns
.length
; i
++) {
103 anns
[i
].attachAtBottom
= to_bottom
;
105 g
.setAnnotations(anns
);
108 bottom
.textContent
= 'Lift back up';
110 bottom
.textContent
= 'Shove to bottom';
117 annotationClickHandler
: function(ann
, point
, dg
, event
) {
118 eventDiv
.innerHTML
+= "click: " + nameAnnotation(ann
) + "<br/>";
120 annotationDblClickHandler
: function(ann
, point
, dg
, event
) {
121 eventDiv
.innerHTML
+= "dblclick: " + nameAnnotation(ann
) + "<br/>";
123 annotationMouseOverHandler
: function(ann
, point
, dg
, event
) {
124 document
.getElementById(nameAnnotation(ann
)).style
.fontWeight
= 'bold';
125 saveBg
= ann
.div
.style
.backgroundColor
;
126 ann
.div
.style
.backgroundColor
= '#ddd';
128 annotationMouseOutHandler
: function(ann
, point
, dg
, event
) {
129 document
.getElementById(nameAnnotation(ann
)).style
.fontWeight
= 'normal';
130 ann
.div
.style
.backgroundColor
= saveBg
;
133 pointClickCallback
: function(event
, p
) {
134 // Check if the point is already annotated.
135 if (p
.annotation
) return;
142 text
: "Annotation #" + num
144 var anns
= g
.annotations();
146 g
.setAnnotations(anns
);