gviz mostly working
[dygraphs.git] / dygraph-canvas.js
index 1947ccd..13e5fe6 100644 (file)
@@ -30,14 +30,13 @@ DygraphLayout.prototype.addDataset = function(setname, set_xy) {
   this.datasets[setname] = set_xy;
 };
 
-// TODO(danvk): CONTRACT remove
 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) {
+    if (!ann[i].xval && !ann[i].x) {
       this.dygraph_.error("Annotations must have an 'x' property");
       return;
     }
@@ -49,7 +48,7 @@ DygraphLayout.prototype.setAnnotations = function(ann) {
       return;
     }
     Dygraph.update(a, ann[i]);
-    a.xval = parse(a.x);
+    if (!a.xval) a.xval = parse(a.x);
     this.annotations.push(a);
   }
 };
@@ -520,6 +519,10 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() {
   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")) {
@@ -557,6 +560,7 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() {
     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));