event works
authorDan Vanderkam <danvdk@gmail.com>
Sat, 11 Sep 2010 05:36:52 +0000 (22:36 -0700)
committerDan Vanderkam <danvdk@gmail.com>
Sat, 11 Sep 2010 05:36:52 +0000 (22:36 -0700)
dygraph-canvas.js
tests/annotation.html

index 4f5bd6c..66888ee 100644 (file)
@@ -499,6 +499,17 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() {
     "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++) {
@@ -519,6 +530,16 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() {
     div.title = p.annotation.text;
     div.style.color = this.colors[p.name];
     div.style.borderColor = this.colors[p.name];
+
+    var self = this;
+    Dygraph.addEvent(div, 'click', function(p, self) { return function(e) {
+      if (p.annotation.hasOwnProperty('clickHandler')) {
+        p.annotation.clickHandler(p.annotation, p, self.dygraph_, e);
+      } else if (self.dygraph_.attr_('annotationClickHandler')) {
+        self.dygraph_.attr_('annotationClickHandler')(p.annotation, p, self.dygraph_, e);
+      } }; }(p, self)
+    );
+
     this.container.appendChild(div);
     this.annotations.push(div);
   }
index 100334b..7d61736 100644 (file)
         x: "20061013",
         shortText: 'X',
         text: 'Another one',
-        cssClass: 'annotation'
+        cssClass: 'annotation',
+        clickHandler: function() {
+          document.getElementById("events").innerHTML += "special handler<br/>";
+        }
       } );
       g.setAnnotations(annotations);
 
         last_ann = x;
         g.setAnnotations(annotations);
       }
+
+      g.updateOptions( {
+        annotationClickHandler: function(ann, point, dg, event) {
+          document.getElementById("events").innerHTML += "click: (" + ann.series + ", " + ann.x + ")<br/>";
+        }
+      });
     </script>
 
     <input type="button" value="Add Annotation" onclick="add()" />
+    <div id="events"> </div>
 </body>
 </html>