From: Dan Vanderkam Date: Sat, 11 Sep 2010 05:36:52 +0000 (-0700) Subject: event works X-Git-Tag: v1.0.0~674 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=ab5e5c7548f975820f6b469886d15ff5bc1b8477;p=dygraphs.git event works --- diff --git a/dygraph-canvas.js b/dygraph-canvas.js index 4f5bd6c..66888ee 100644 --- a/dygraph-canvas.js +++ b/dygraph-canvas.js @@ -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); } diff --git a/tests/annotation.html b/tests/annotation.html index 100334b..7d61736 100644 --- a/tests/annotation.html +++ b/tests/annotation.html @@ -66,7 +66,10 @@ x: "20061013", shortText: 'X', text: 'Another one', - cssClass: 'annotation' + cssClass: 'annotation', + clickHandler: function() { + document.getElementById("events").innerHTML += "special handler
"; + } } ); g.setAnnotations(annotations); @@ -82,8 +85,15 @@ last_ann = x; g.setAnnotations(annotations); } + + g.updateOptions( { + annotationClickHandler: function(ann, point, dg, event) { + document.getElementById("events").innerHTML += "click: (" + ann.series + ", " + ann.x + ")
"; + } + }); +