From: Dan Vanderkam Date: Tue, 14 Sep 2010 01:02:04 +0000 (-0700) Subject: add pointClickCallback X-Git-Tag: v1.0.0~663 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=1e1bf7dfc2628a153f6b6b8c9b2b6b6d74390e6c;p=dygraphs.git add pointClickCallback --- diff --git a/dygraph.js b/dygraph.js index 4d086f7..8c9f2df 100644 --- a/dygraph.js +++ b/dygraph.js @@ -727,7 +727,7 @@ Dygraph.prototype.createDragInterface_ = function() { var px = 0; var py = 0; var getX = function(e) { return Dygraph.pageX(e) - px }; - var getY = function(e) { return Dygraph.pageX(e) - py }; + var getY = function(e) { return Dygraph.pageY(e) - py }; // Draw zoom rectangles when the mouse is down and the user moves around Dygraph.addEvent(this.mouseEventElement_, 'mousemove', function(event) { @@ -803,11 +803,31 @@ Dygraph.prototype.createDragInterface_ = function() { var regionWidth = Math.abs(dragEndX - dragStartX); var regionHeight = Math.abs(dragEndY - dragStartY); - if (regionWidth < 2 && regionHeight < 2 && - self.attr_('clickCallback') != null && - self.lastx_ != undefined) { - // TODO(danvk): pass along more info about the points. - self.attr_('clickCallback')(event, self.lastx_, self.selPoints_); + if (regionWidth < 2 && regionHeight < 2 && self.lastx_ != undefined) { + 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')) { + // check if the click was on a particular point. + var closestIdx = -1; + var closestDistance = 0; + for (var i = 0; i < self.selPoints_.length; i++) { + var p = self.selPoints_[i]; + var distance = Math.pow(p.canvasx - dragEndX, 2) + + Math.pow(p.canvasy - dragEndY, 2); + if (closestIdx == -1 || distance < closestDistance) { + closestDistance = distance; + closestIdx = i; + } + } + + // Allow any click within two pixels of the dot. + var radius = this.attr_('highlightCircleSize') + 2; + if (closestDistance <= 5 * 5) { + self.attr_('pointClickCallback')(event, self.selPoints_[closestIdx]); + } + } } if (regionWidth >= 10) { diff --git a/tests/callback.html b/tests/callback.html index 5a08409..64e9777 100644 --- a/tests/callback.html +++ b/tests/callback.html @@ -76,6 +76,10 @@ s.innerHTML += "Click " + pts_info(e,x,pts) + "
"; }, + pointClickCallback: function(e, p) { + s.innerHTML += "Point Click " + p.name + ": " + p.x + "
"; + }, + zoomCallback: function(minX, maxX) { s.innerHTML += "Zoom [" + minX + ", " + maxX + "]
"; },