attach divs to points; respect clipping area
[dygraphs.git] / tests / annotation.html
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>
11 <style type="text/css">
12 .annotation {
13 }
14 </style>
15 </head>
16 <body>
17 <input type="button" value="Add Annotation" onclick="add()" />
18 <div id="events"> </div>
19 <div style="position:absolute; left:200px; top: 200px;" id="g"></div>
20 <div style="position:absolute; left:700px; top: 200px;" id="list"></div>
21
22 <script type="text/javascript">
23 var eventDiv = document.getElementById("events");
24 function nameAnnotation(ann) {
25 return "(" + ann.series + ", " + ann.x + ")";
26 }
27
28 g = new DateGraph(
29 document.getElementById("g"),
30 function() {
31 var zp = function(x) { if (x < 10) return "0"+x; else return x; };
32 var r = "date,parabola,line,another line,sine wave\n";
33 for (var i=1; i<=31; i++) {
34 r += "200610" + zp(i);
35 r += "," + 10*(i*(31-i));
36 r += "," + 10*(8*i);
37 r += "," + 10*(250 - 8*i);
38 r += "," + 10*(125 + 125 * Math.sin(0.3*i));
39 r += "\n";
40 }
41 return r;
42 },
43 {
44 rollPeriod: 1,
45 showRoller: true,
46 width: 480,
47 height: 320,
48 drawCallback: function(g) {
49 var ann = g.annotations();
50 var html = "";
51 for (var i = 0; i < ann.length; i++) {
52 var name = nameAnnotation(ann[i]);
53 html += "<span id='" + name + "'>"
54 html += name + ": " + ann[i].shortText + " -> ";
55 html += ann[i].text + "</span><br/>";
56 }
57 document.getElementById("list").innerHTML = html;
58 },
59 }
60 );
61
62 var last_ann = 0;
63 annotations = [];
64 for (var x = 10; x < 15; x += 2) {
65 annotations.push( {
66 series: 'sine wave',
67 x: "200610" + x,
68 shortText: x,
69 text: 'Stock Market Crash ' + x
70 } );
71 last_ann = x;
72 }
73 annotations.push( {
74 series: 'another line',
75 x: "20061013",
76 icon: 'dollar.png',
77 iconWidth: 18,
78 iconHeight: 23,
79 tickHeight: 4,
80 text: 'Another one',
81 cssClass: 'annotation',
82 clickHandler: function() {
83 document.getElementById("events").innerHTML += "special handler<br/>";
84 }
85 } );
86 g.setAnnotations(annotations);
87
88 function add() {
89 var x = last_ann + 2;
90 var annnotations = g.annotations();
91 annotations.push( {
92 series: 'line',
93 x: "200610" + x,
94 shortText: x,
95 text: 'Line ' + x,
96 tickHeight: 10
97 } );
98 last_ann = x;
99 g.setAnnotations(annotations);
100 }
101
102 var saveBg = '';
103 g.updateOptions( {
104 annotationClickHandler: function(ann, point, dg, event) {
105 eventDiv.innerHTML += "click: " + nameAnnotation(ann) + "<br/>";
106 },
107 annotationDblClickHandler: function(ann, point, dg, event) {
108 eventDiv.innerHTML += "dblclick: " + nameAnnotation(ann) + "<br/>";
109 },
110 annotationMouseOverHandler: function(ann, point, dg, event) {
111 document.getElementById(nameAnnotation(ann)).style.fontWeight = 'bold';
112 saveBg = ann.div.style.backgroundColor;
113 ann.div.style.backgroundColor = '#ddd';
114 },
115 annotationMouseOutHandler: function(ann, point, dg, event) {
116 document.getElementById(nameAnnotation(ann)).style.fontWeight = 'normal';
117 ann.div.style.backgroundColor = saveBg;
118 }
119 });
120 </script>
121 </body>
122 </html>