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