From 2ad87eaa004e722037fba951f4fe418d09c209cc Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Mon, 13 Sep 2010 18:12:45 -0700 Subject: [PATCH] click to add annotations --- dygraph.js | 4 ++-- tests/annotation.html | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/dygraph.js b/dygraph.js index 8c9f2df..4d2d3d0 100644 --- a/dygraph.js +++ b/dygraph.js @@ -804,8 +804,8 @@ Dygraph.prototype.createDragInterface_ = function() { var regionHeight = Math.abs(dragEndY - dragStartY); if (regionWidth < 2 && regionHeight < 2 && self.lastx_ != undefined) { + // TODO(danvk): pass along more info about the points, e.g. 'x' if (self.attr_('clickCallback') != null) { - // TODO(danvk): pass along more info about the points. self.attr_('clickCallback')(event, self.lastx_, self.selPoints_); } if (self.attr_('pointClickCallback')) { @@ -823,7 +823,7 @@ Dygraph.prototype.createDragInterface_ = function() { } // Allow any click within two pixels of the dot. - var radius = this.attr_('highlightCircleSize') + 2; + var radius = self.attr_('highlightCircleSize') + 2; if (closestDistance <= 5 * 5) { self.attr_('pointClickCallback')(event, self.selPoints_[closestIdx]); } diff --git a/tests/annotation.html b/tests/annotation.html index b87c291..60062bc 100644 --- a/tests/annotation.html +++ b/tests/annotation.html @@ -100,6 +100,7 @@ } var saveBg = ''; + var num = 0; g.updateOptions( { annotationClickHandler: function(ann, point, dg, event) { eventDiv.innerHTML += "click: " + nameAnnotation(ann) + "
"; @@ -115,6 +116,24 @@ annotationMouseOutHandler: function(ann, point, dg, event) { document.getElementById(nameAnnotation(ann)).style.fontWeight = 'normal'; ann.div.style.backgroundColor = saveBg; + }, + + pointClickCallback: function(event, p) { + // Check if the point is already annotated. + if (p.annotation) return; + + // If not, add one. + var ann = { + series: p.name, + xval: p.xval, + shortText: num, + text: "Annotation #" + num + }; + var anns = g.annotations(); + anns.push(ann); + g.setAnnotations(anns); + + num++; } }); -- 2.7.4