3 * Copyright 2012 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
7 Dygraph
.Plugins
.Annotations
= (function() {
12 Current bits of jankiness:
13 - Uses dygraph.layout_ to get the parsed annotations.
14 - Uses dygraph.plotter_.area
16 It would be nice if the plugin didn't require so much special support inside
17 the core dygraphs classes, but annotations involve quite a bit of parsing and
20 TODO(danvk): cache DOM elements.
24 var annotations
= function() {
25 this.annotations_
= [];
28 annotations
.prototype.toString
= function() {
29 return "Annotations Plugin";
32 annotations
.prototype.activate
= function(g
) {
34 clearChart
: this.clearChart
,
35 didDrawChart
: this.didDrawChart
39 annotations
.prototype.detachLabels
= function() {
40 for (var i
= 0; i
< this.annotations_
.length
; i
++) {
41 var a
= this.annotations_
[i
];
42 if (a
.parentNode
) a
.parentNode
.removeChild(a
);
43 this.annotations_
[i
] = null;
45 this.annotations_
= [];
48 annotations
.prototype.clearChart
= function(e
) {
52 annotations
.prototype.didDrawChart
= function(e
) {
55 // Early out in the (common) case of zero annotations.
56 var points
= g
.layout_
.annotated_points
;
57 if (!points
|| points
.length
=== 0) return;
59 var containerDiv
= e
.canvas
.parentNode
;
60 var annotationStyle
= {
61 "position": "absolute",
62 "fontSize": g
.getOption('axisLabelFontSize') + "px",
67 var bindEvt
= function(eventName
, classEventName
, pt
) {
68 return function(annotation_event
) {
69 var a
= pt
.annotation
;
70 if (a
.hasOwnProperty(eventName
)) {
71 a
[eventName
](a
, pt
, g
, annotation_event
);
72 } else if (g
.getOption(classEventName
)) {
73 g
.getOption(classEventName
)(a
, pt
, g
, annotation_event
);
78 // Add the annotations one-by-one.
79 var area
= e
.dygraph
.plotter_
.area
;
80 for (var i
= 0; i
< points
.length
; i
++) {
82 if (p
.canvasx
< area
.x
|| p
.canvasx
> area
.x
+ area
.w
||
83 p
.canvasy
< area
.y
|| p
.canvasy
> area
.y
+ area
.h
) {
89 if (a
.hasOwnProperty("tickHeight")) {
90 tick_height
= a
.tickHeight
;
93 var div
= document
.createElement("div");
94 for (var name
in annotationStyle
) {
95 if (annotationStyle
.hasOwnProperty(name
)) {
96 div
.style
[name
] = annotationStyle
[name
];
99 if (!a
.hasOwnProperty('icon')) {
100 div
.className
= "dygraphDefaultAnnotation";
102 if (a
.hasOwnProperty('cssClass')) {
103 div
.className
+= " " + a
.cssClass
;
106 var width
= a
.hasOwnProperty('width') ? a
.width
: 16;
107 var height
= a
.hasOwnProperty('height') ? a
.height
: 16;
108 if (a
.hasOwnProperty('icon')) {
109 var img
= document
.createElement("img");
113 div
.appendChild(img
);
114 } else if (p
.annotation
.hasOwnProperty('shortText')) {
115 div
.appendChild(document
.createTextNode(p
.annotation
.shortText
));
117 div
.style
.left
= (p
.canvasx
- width
/ 2) + "px";
118 if (a
.attachAtBottom
) {
119 div
.style
.top
= (area
.h
- height
- tick_height
) + "px";
121 div
.style
.top
= (p
.canvasy
- height
- tick_height
) + "px";
123 div
.style
.width
= width
+ "px";
124 div
.style
.height
= height
+ "px";
125 div
.title
= p
.annotation
.text
;
126 div
.style
.color
= g
.colorsMap_
[p
.name
];
127 div
.style
.borderColor
= g
.colorsMap_
[p
.name
];
130 g
.addEvent(div
, 'click',
131 bindEvt('clickHandler', 'annotationClickHandler', p
, this));
132 g
.addEvent(div
, 'mouseover',
133 bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p
, this));
134 g
.addEvent(div
, 'mouseout',
135 bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p
, this));
136 g
.addEvent(div
, 'dblclick',
137 bindEvt('dblClickHandler', 'annotationDblClickHandler', p
, this));
139 containerDiv
.appendChild(div
);
140 this.annotations_
.push(div
);
142 var ctx
= e
.drawingContext
;
144 ctx
.strokeStyle
= g
.colorsMap_
[p
.name
];
146 if (!a
.attachAtBottom
) {
147 ctx
.moveTo(p
.canvasx
, p
.canvasy
);
148 ctx
.lineTo(p
.canvasx
, p
.canvasy
- 2 - tick_height
);
150 ctx
.moveTo(p
.canvasx
, area
.h
);
151 ctx
.lineTo(p
.canvasx
, area
.h
- 2 - tick_height
);
159 annotations
.prototype.destroy
= function() {