3 * Copyright 2012 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
7 /*global Dygraph:false */
9 Dygraph
.Plugins
.Annotations
= (function() {
14 Current bits of jankiness:
15 - Uses dygraph.layout_ to get the parsed annotations.
16 - Uses dygraph.plotter_.area
18 It would be nice if the plugin didn't require so much special support inside
19 the core dygraphs classes, but annotations involve quite a bit of parsing and
22 TODO(danvk): cache DOM elements.
26 var annotations
= function() {
27 this.annotations_
= [];
30 annotations
.prototype.toString
= function() {
31 return "Annotations Plugin";
34 annotations
.prototype.activate
= function(g
) {
36 clearChart
: this.clearChart
,
37 didDrawChart
: this.didDrawChart
41 annotations
.prototype.detachLabels
= function() {
42 for (var i
= 0; i
< this.annotations_
.length
; i
++) {
43 var a
= this.annotations_
[i
];
44 if (a
.parentNode
) a
.parentNode
.removeChild(a
);
45 this.annotations_
[i
] = null;
47 this.annotations_
= [];
50 annotations
.prototype.clearChart
= function(e
) {
54 annotations
.prototype.didDrawChart
= function(e
) {
57 // Early out in the (common) case of zero annotations.
58 var points
= g
.layout_
.annotated_points
;
59 if (!points
|| points
.length
=== 0) return;
61 var containerDiv
= e
.canvas
.parentNode
;
62 var annotationStyle
= {
63 "position": "absolute",
64 "fontSize": g
.getOption('axisLabelFontSize') + "px",
69 var bindEvt
= function(eventName
, classEventName
, pt
) {
70 return function(annotation_event
) {
71 var a
= pt
.annotation
;
72 if (a
.hasOwnProperty(eventName
)) {
73 a
[eventName
](a
, pt
, g
, annotation_event
);
74 } else if (g
.getOption(classEventName
)) {
75 g
.getOption(classEventName
)(a
, pt
, g
, annotation_event
);
80 // Add the annotations one-by-one.
81 var area
= e
.dygraph
.plotter_
.area
;
83 // x-coord to sum of previous annotation's heights (used for stacking).
84 var xToUsedHeight
= {};
86 for (var i
= 0; i
< points
.length
; i
++) {
88 if (p
.canvasx
< area
.x
|| p
.canvasx
> area
.x
+ area
.w
||
89 p
.canvasy
< area
.y
|| p
.canvasy
> area
.y
+ area
.h
) {
95 if (a
.hasOwnProperty("tickHeight")) {
96 tick_height
= a
.tickHeight
;
99 var div
= document
.createElement("div");
100 for (var name
in annotationStyle
) {
101 if (annotationStyle
.hasOwnProperty(name
)) {
102 div
.style
[name
] = annotationStyle
[name
];
105 if (!a
.hasOwnProperty('icon')) {
106 div
.className
= "dygraphDefaultAnnotation";
108 if (a
.hasOwnProperty('cssClass')) {
109 div
.className
+= " " + a
.cssClass
;
112 var width
= a
.hasOwnProperty('width') ? a
.width
: 16;
113 var height
= a
.hasOwnProperty('height') ? a
.height
: 16;
114 if (a
.hasOwnProperty('icon')) {
115 var img
= document
.createElement("img");
119 div
.appendChild(img
);
120 } else if (p
.annotation
.hasOwnProperty('shortText')) {
121 div
.appendChild(document
.createTextNode(p
.annotation
.shortText
));
123 var left
= p
.canvasx
- width
/ 2;
124 div
.style
.left
= left
+ "px";
126 if (a
.attachAtBottom
) {
127 var y
= (area
.y
+ area
.h
- height
- tick_height
);
128 if (xToUsedHeight
[left
]) {
129 y
-= xToUsedHeight
[left
];
131 xToUsedHeight
[left
] = 0;
133 xToUsedHeight
[left
] += (tick_height
+ height
);
136 divTop
= p
.canvasy
- height
- tick_height
;
138 div
.style
.top
= divTop
+ "px";
139 div
.style
.width
= width
+ "px";
140 div
.style
.height
= height
+ "px";
141 div
.title
= p
.annotation
.text
;
142 div
.style
.color
= g
.colorsMap_
[p
.name
];
143 div
.style
.borderColor
= g
.colorsMap_
[p
.name
];
146 g
.addEvent(div
, 'click',
147 bindEvt('clickHandler', 'annotationClickHandler', p
, this));
148 g
.addEvent(div
, 'mouseover',
149 bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p
, this));
150 g
.addEvent(div
, 'mouseout',
151 bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p
, this));
152 g
.addEvent(div
, 'dblclick',
153 bindEvt('dblClickHandler', 'annotationDblClickHandler', p
, this));
155 containerDiv
.appendChild(div
);
156 this.annotations_
.push(div
);
158 var ctx
= e
.drawingContext
;
160 ctx
.strokeStyle
= g
.colorsMap_
[p
.name
];
162 if (!a
.attachAtBottom
) {
163 ctx
.moveTo(p
.canvasx
, p
.canvasy
);
164 ctx
.lineTo(p
.canvasx
, p
.canvasy
- 2 - tick_height
);
166 var y
= divTop
+ height
;
167 ctx
.moveTo(p
.canvasx
, y
);
168 ctx
.lineTo(p
.canvasx
, y
+ tick_height
);
176 annotations
.prototype.destroy
= function() {