From: Dan Vanderkam Date: Tue, 14 Sep 2010 01:12:45 +0000 (-0700) Subject: click to add annotations X-Git-Tag: v1.0.0~662 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=2ad87eaa004e722037fba951f4fe418d09c209cc;p=dygraphs.git click to add annotations --- 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++; } });