add tick marks
[dygraphs.git] / dygraph-canvas.js
index 13c8061..3932e71 100644 (file)
@@ -19,6 +19,7 @@ DygraphLayout = function(dygraph, options) {
   this.options = {};  // TODO(danvk): remove, use attr_ instead.
   Dygraph.update(this.options, options ? options : {});
   this.datasets = new Array();
+  this.annotations = new Array()
 };
 
 DygraphLayout.prototype.attr_ = function(name) {
@@ -30,23 +31,20 @@ DygraphLayout.prototype.addDataset = function(setname, set_xy) {
 };
 
 // TODO(danvk): CONTRACT remove
-DygraphLayout.prototype.addAnnotation = function() {
-  // Add an annotation to one series.
-  this.annotations = [];
-  for (var x = 10; x < 30; x += 2) {
-    this.annotations.push( {
-      series: 'sine wave',
-      xval: this.attr_('xValueParser')("200610" + x),
-      shortText: x,
-      text: 'Stock Market Crash ' + x
-    } );
+DygraphLayout.prototype.setAnnotations = function(ann) {
+  // The Dygraph object's annotations aren't parsed. We parse them here and
+  // save a copy.
+  var parse = this.attr_('xValueParser');
+  for (var i = 0; i < ann.length; i++) {
+    var a = {};
+    if (!ann[i].x) {
+      this.dygraph_.error("Annotations must have an 'x' property");
+      return;
+    }
+    Dygraph.update(a, ann[i]);
+    a.xval = parse(a.x);
+    this.annotations.push(a);
   }
-  this.annotations.push( {
-    series: 'another line',
-    xval: this.attr_('xValueParser')("20061013"),
-    shortText: 'X',
-    text: 'Another one'
-  } );
 };
 
 DygraphLayout.prototype.evaluate = function() {
@@ -499,29 +497,64 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() {
     "zIndex": 10,
     "width": "20px",
     "overflow": "hidden",
-    "border": "1px solid black",
-    "background-color": "white",
-    "text-align": "center"
   };
 
+  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];
+    var tick_height = 5;
+    if (p.annotation.hasOwnProperty("tickHeight")) {
+      tick_height = p.annotation.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;
+    }
     div.appendChild(document.createTextNode(p.annotation.shortText));
     div.style.left = (p.canvasx - 10) + "px";
-    div.style.top = p.canvasy + "px";
+    div.style.top = (p.canvasy - 20 - tick_height) + "px";
     div.title = p.annotation.text;
     div.style.color = this.colors[p.name];
     div.style.borderColor = this.colors[p.name];
+
+    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();
   }
 };