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