Fix pointClickCallback and resizing coordinate weirdness when inside a
authorRobert Konigsberg <konigsberg@google.com>
Mon, 27 Jun 2011 20:18:47 +0000 (16:18 -0400)
committerRobert Konigsberg <konigsberg@google.com>
Mon, 27 Jun 2011 20:18:47 +0000 (16:18 -0400)
scrolling div element.

dygraph-utils.js
dygraph.js

index b5521c4..27196d8 100644 (file)
@@ -173,35 +173,57 @@ Dygraph.hsvToRGB = function (hue, saturation, value) {
 // http://blog.firetree.net/2005/07/04/javascript-find-position/
 // http://www.quirksmode.org/js/findpos.html
 
-/** @private */
+/**
+ * Find the x-coordinate of the supplied object relative to the left side
+ * of the page.
+ * @private
+ */
 Dygraph.findPosX = function(obj) {
   var curleft = 0;
-  if(obj.offsetParent)
-    while(1)
-    {
-      curleft += obj.offsetLeft;
-      if(!obj.offsetParent)
+  if(obj.offsetParent) {
+    var copyObj = obj;
+    while(1) {
+      curleft += copyObj.offsetLeft;
+      if(!copyObj.offsetParent) {
         break;
-      obj = obj.offsetParent;
+      }
+      copyObj = copyObj.offsetParent;
     }
-  else if(obj.x)
+  } else if(obj.x) {
     curleft += obj.x;
+  }
+  // This handles the case where the object is inside a scrolled div.
+  while(obj && obj != document.body) {
+    curleft -= obj.scrollLeft;
+    obj = obj.parentNode;
+  }
   return curleft;
 };
 
-/** @private */
+/**
+ * Find the y-coordinate of the supplied object relative to the top of the
+ * page.
+ * @private
+ */
 Dygraph.findPosY = function(obj) {
   var curtop = 0;
-  if(obj.offsetParent)
-    while(1)
-    {
-      curtop += obj.offsetTop;
-      if(!obj.offsetParent)
+  if(obj.offsetParent) {
+    var copyObj = obj;
+    while(1) {
+      curtop += copyObj.offsetTop;
+      if(!copyObj.offsetParent) {
         break;
-      obj = obj.offsetParent;
+      }
+      copyObj = copyObj.offsetParent;
     }
-  else if(obj.y)
+  } else if(obj.y) {
     curtop += obj.y;
+  }
+  // This handles the case where the object is inside a scrolled div.
+  while(obj && obj != document.body) {
+    curtop -= obj.scrollTop;
+    obj = obj.parentNode;
+  }
   return curtop;
 };
 
index 83d4e8c..2619d63 100644 (file)
@@ -866,13 +866,13 @@ Dygraph.prototype.createDragInterface_ = function() {
     isZooming: false,
     isPanning: false,  // is this drag part of a pan?
     is2DPan: false,    // if so, is that pan 1- or 2-dimensional?
-    dragStartX: null,
-    dragStartY: null,
-    dragEndX: null,
-    dragEndY: null,
+    dragStartX: null, // pixel coordinates
+    dragStartY: null, // pixel coordinates
+    dragEndX: null, // pixel coordinates
+    dragEndY: null, // pixel coordinates
     dragDirection: null,
-    prevEndX: null,
-    prevEndY: null,
+    prevEndX: null, // pixel coordinates
+    prevEndY: null, // pixel coordinates
     prevDragDirection: null,
 
     // The value on the left side of the graph when a pan operation starts.
@@ -887,7 +887,8 @@ Dygraph.prototype.createDragInterface_ = function() {
     // panning operation.
     dateRange: null,
 
-    // Utility function to convert page-wide coordinates to canvas coords
+    // Top-left corner of the canvas, in DOM coords
+    // TODO(konigsberg): Rename topLeftCanvasX, topLeftCanvasY.
     px: 0,
     py: 0,