attach divs to points; respect clipping area
[dygraphs.git] / dygraph-canvas.js
index 4f5bd6c..b71044a 100644 (file)
@@ -41,6 +41,13 @@ DygraphLayout.prototype.setAnnotations = function(ann) {
       this.dygraph_.error("Annotations must have an 'x' property");
       return;
     }
+    if (ann[i].icon &&
+        !(ann[i].hasOwnProperty('iconWidth') &&
+          ann[i].hasOwnProperty('iconHeight'))) {
+      this.dygraph_.error("Must set iconWidth and iconHeight when setting " +
+                          "annotation.icon property");
+      return;
+    }
     Dygraph.update(a, ann[i]);
     a.xval = parse(a.x);
     this.annotations.push(a);
@@ -495,32 +502,86 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() {
     "position": "absolute",
     "fontSize": this.options.axisLabelFontSize + "px",
     "zIndex": 10,
-    "width": "20px",
     "overflow": "hidden",
   };
 
+  var bindEvt = function(eventName, classEventName, p, self) {
+    return function(e) {
+      var a = p.annotation;
+      if (a.hasOwnProperty(eventName)) {
+        a[eventName](a, p, self.dygraph_, e);
+      } else if (self.dygraph_.attr_(classEventName)) {
+        self.dygraph_.attr_(classEventName)(a, p, self.dygraph_,e );
+      }
+    };
+  }
+
   // Get a list of point with annotations.
   var points = this.layout.annotated_points;
   for (var i = 0; i < points.length; i++) {
     var p = points[i];
+    if (p.canvasx < this.area.x || p.canvasx > this.area.x + this.area.w) {
+      continue;
+    }
+
+    var a = p.annotation;
+    var tick_height = 6;
+    if (a.hasOwnProperty("tickHeight")) {
+      tick_height = a.tickHeight;
+    }
+
     var div = document.createElement("div");
     for (var name in annotationStyle) {
       if (annotationStyle.hasOwnProperty(name)) {
         div.style[name] = annotationStyle[name];
       }
     }
-    div.className = "dygraphDefaultAnnotation";
-    if (p.annotation.hasOwnProperty('cssClass')) {
-      div.className += " " + p.annotation.cssClass;
+    if (!a.hasOwnProperty('icon')) {
+      div.className = "dygraphDefaultAnnotation";
+    }
+    if (a.hasOwnProperty('cssClass')) {
+      div.className += " " + a.cssClass;
+    }
+
+    var width = a.hasOwnProperty('height') ? a.height : 20;
+    var height = a.hasOwnProperty('width') ? a.width : 16;
+    if (a.hasOwnProperty('icon')) {
+      var img = document.createElement("img");
+      img.src = a.icon;
+      img.width = width = a.iconWidth;
+      img.height = height = a.iconHeight;
+      div.appendChild(img);
+    } else if (p.annotation.hasOwnProperty('shortText')) {
+      div.appendChild(document.createTextNode(p.annotation.shortText));
     }
-    div.appendChild(document.createTextNode(p.annotation.shortText));
-    div.style.left = (p.canvasx - 10) + "px";
-    div.style.top = p.canvasy + "px";
+    div.style.left = (p.canvasx - width / 2) + "px";
+    div.style.top = (p.canvasy - height - tick_height) + "px";
+    div.style.width = width + "px";
+    div.style.height = height + "px";
     div.title = p.annotation.text;
     div.style.color = this.colors[p.name];
     div.style.borderColor = this.colors[p.name];
+    a.div = div;
+
+    Dygraph.addEvent(div, 'click',
+        bindEvt('clickHandler', 'annotationClickHandler', p, this));
+    Dygraph.addEvent(div, 'mouseover',
+        bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p, this));
+    Dygraph.addEvent(div, 'mouseout',
+        bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p, this));
+    Dygraph.addEvent(div, 'dblclick',
+        bindEvt('dblClickHandler', 'annotationDblClickHandler', p, this));
+
     this.container.appendChild(div);
     this.annotations.push(div);
+
+    var ctx = this.element.getContext("2d");
+    ctx.strokeStyle = this.colors[p.name];
+    ctx.beginPath();
+    ctx.moveTo(p.canvasx, p.canvasy);
+    ctx.lineTo(p.canvasx, p.canvasy - 2 - tick_height);
+    ctx.closePath();
+    ctx.stroke();
   }
 };