3 * Copyright 2012 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
7 Dygraph
.Plugins
.Annotations
= (function() {
10 Current bits of jankiness:
11 - Uses dygraph.layout_ to get the parsed annotations.
12 - Uses dygraph.plotter_.area
14 It would be nice if the plugin didn't require so much special support inside
15 the core dygraphs classes, but annotations involve quite a bit of parsing and
18 TODO(danvk): cache DOM elements.
22 var annotations
= function() {
23 this.annotations_
= [];
26 annotations
.prototype.toString
= function() {
27 return "Annotations Plugin";
30 annotations
.prototype.activate
= function(g
) {
32 clearChart
: this.clearChart
,
33 didDrawChart
: this.didDrawChart
37 annotations
.prototype.detachLabels
= function() {
38 for (var i
= 0; i
< this.annotations_
.length
; i
++) {
39 var a
= this.annotations_
[i
];
40 if (a
.parentNode
) a
.parentNode
.removeChild(a
);
41 this.annotations_
[i
] = null;
43 this.annotations_
= [];
46 annotations
.prototype.clearChart
= function(e
) {
50 annotations
.prototype.didDrawChart
= function(e
) {
53 // Early out in the (common) case of zero annotations.
54 var points
= g
.layout_
.annotated_points
;
55 if (!points
|| points
.length
== 0) return;
57 var containerDiv
= e
.canvas
.parentNode
;
58 var annotationStyle
= {
59 "position": "absolute",
60 "fontSize": g
.getOption('axisLabelFontSize') + "px",
65 var bindEvt
= function(eventName
, classEventName
, pt
) {
66 return function(annotation_event
) {
67 var a
= pt
.annotation
;
68 if (a
.hasOwnProperty(eventName
)) {
69 a
[eventName
](a
, pt
, g
, annotation_event
);
70 } else if (g
.getOption(classEventName
)) {
71 g
.getOption(classEventName
)(a
, pt
, g
, annotation_event
);
76 // Add the annotations one-by-one.
77 var area
= e
.dygraph
.plotter_
.area
;
78 for (var i
= 0; i
< points
.length
; i
++) {
80 if (p
.canvasx
< area
.x
|| p
.canvasx
> area
.x
+ area
.w
||
81 p
.canvasy
< area
.y
|| p
.canvasy
> area
.y
+ area
.h
) {
87 if (a
.hasOwnProperty("tickHeight")) {
88 tick_height
= a
.tickHeight
;
91 var div
= document
.createElement("div");
92 for (var name
in annotationStyle
) {
93 if (annotationStyle
.hasOwnProperty(name
)) {
94 div
.style
[name
] = annotationStyle
[name
];
97 if (!a
.hasOwnProperty('icon')) {
98 div
.className
= "dygraphDefaultAnnotation";
100 if (a
.hasOwnProperty('cssClass')) {
101 div
.className
+= " " + a
.cssClass
;
104 var width
= a
.hasOwnProperty('width') ? a
.width
: 16;
105 var height
= a
.hasOwnProperty('height') ? a
.height
: 16;
106 if (a
.hasOwnProperty('icon')) {
107 var img
= document
.createElement("img");
111 div
.appendChild(img
);
112 } else if (p
.annotation
.hasOwnProperty('shortText')) {
113 div
.appendChild(document
.createTextNode(p
.annotation
.shortText
));
115 div
.style
.left
= (p
.canvasx
- width
/ 2) + "px";
116 if (a
.attachAtBottom
) {
117 div
.style
.top
= (area
.h
- height
- tick_height
) + "px";
119 div
.style
.top
= (p
.canvasy
- height
- tick_height
) + "px";
121 div
.style
.width
= width
+ "px";
122 div
.style
.height
= height
+ "px";
123 div
.title
= p
.annotation
.text
;
124 div
.style
.color
= g
.colorsMap_
[p
.name
];
125 div
.style
.borderColor
= g
.colorsMap_
[p
.name
];
128 g
.addEvent(div
, 'click',
129 bindEvt('clickHandler', 'annotationClickHandler', p
, this));
130 g
.addEvent(div
, 'mouseover',
131 bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p
, this));
132 g
.addEvent(div
, 'mouseout',
133 bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p
, this));
134 g
.addEvent(div
, 'dblclick',
135 bindEvt('dblClickHandler', 'annotationDblClickHandler', p
, this));
137 containerDiv
.appendChild(div
);
138 this.annotations_
.push(div
);
140 var ctx
= e
.drawingContext
;
142 ctx
.strokeStyle
= g
.colorsMap_
[p
.name
];
144 if (!a
.attachAtBottom
) {
145 ctx
.moveTo(p
.canvasx
, p
.canvasy
);
146 ctx
.lineTo(p
.canvasx
, p
.canvasy
- 2 - tick_height
);
148 ctx
.moveTo(p
.canvasx
, area
.h
);
149 ctx
.lineTo(p
.canvasx
, area
.h
- 2 - tick_height
);
157 annotations
.prototype.destroy
= function() {