- var self = this;
-
- // Tracks whether the mouse is down right now
- var isZooming = false;
- var isPanning = false; // is this drag part of a pan?
- var is2DPan = false; // if so, is that pan 1- or 2-dimensional?
- var dragStartX = null;
- var dragStartY = null;
- var dragEndX = null;
- var dragEndY = null;
- var dragDirection = null;
- var prevEndX = null;
- var prevEndY = null;
- var prevDragDirection = null;
-
- // TODO(danvk): update this comment
- // draggingDate and draggingValue represent the [date,value] point on the
- // graph at which the mouse was pressed. As the mouse moves while panning,
- // the viewport must pan so that the mouse position points to
- // [draggingDate, draggingValue]
- var draggingDate = null;
-
- // TODO(danvk): update this comment
- // The range in second/value units that the viewport encompasses during a
- // panning operation.
- var dateRange = null;
-
- // Utility function to convert page-wide coordinates to canvas coords
- var px = 0;
- var py = 0;
- var getX = function(e) { return Dygraph.pageX(e) - px };
- 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) {
- if (isZooming) {
- dragEndX = getX(event);
- dragEndY = getY(event);
-
- var xDelta = Math.abs(dragStartX - dragEndX);
- var yDelta = Math.abs(dragStartY - dragEndY);
-
- // drag direction threshold for y axis is twice as large as x axis
- dragDirection = (xDelta < yDelta / 2) ? Dygraph.VERTICAL : Dygraph.HORIZONTAL;
-
- self.drawZoomRect_(dragDirection, dragStartX, dragEndX, dragStartY, dragEndY,
- prevDragDirection, prevEndX, prevEndY);
-
- prevEndX = dragEndX;
- prevEndY = dragEndY;
- prevDragDirection = dragDirection;
- } else if (isPanning) {
- dragEndX = getX(event);
- dragEndY = getY(event);
-
- // TODO(danvk): update this comment
- // Want to have it so that:
- // 1. draggingDate appears at dragEndX, draggingValue appears at dragEndY.
- // 2. daterange = (dateWindow_[1] - dateWindow_[0]) is unaltered.
- // 3. draggingValue appears at dragEndY.
- // 4. valueRange is unaltered.
-
- var minDate = draggingDate - (dragEndX / self.width_) * dateRange;
- var maxDate = minDate + dateRange;
- self.dateWindow_ = [minDate, maxDate];
-
-
- // y-axis scaling is automatic unless this is a full 2D pan.
- if (is2DPan) {
- // Adjust each axis appropriately.
- var y_frac = dragEndY / self.height_;
- for (var i = 0; i < self.axes_.length; i++) {
- var axis = self.axes_[i];
- var maxValue = axis.draggingValue + y_frac * axis.dragValueRange;
- var minValue = maxValue - axis.dragValueRange;
- axis.valueWindow = [ minValue, maxValue ];
- }
+ var context = {
+ // Tracks whether the mouse is down right now
+ 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,
+ dragDirection: null,
+ prevEndX: null,
+ prevEndY: null,
+ prevDragDirection: null,
+
+ // The value on the left side of the graph when a pan operation starts.
+ initialLeftmostDate: null,
+
+ // The number of units each pixel spans. (This won't be valid for log
+ // scales)
+ xUnitsPerPixel: null,
+
+ // TODO(danvk): update this comment
+ // The range in second/value units that the viewport encompasses during a
+ // panning operation.
+ dateRange: null,
+
+ // Utility function to convert page-wide coordinates to canvas coords
+ px: 0,
+ py: 0,
+
+ // Values for use with panEdgeFraction, which limit how far outside the
+ // graph's data boundaries it can be panned.
+ boundedDates: null, // [minDate, maxDate]
+ boundedValues: null, // [[minValue, maxValue] ...]
+
+ initializeMouseDown: function(event, g, context) {
+ // prevents mouse drags from selecting page text.
+ if (event.preventDefault) {
+ event.preventDefault(); // Firefox, Chrome, etc.
+ } else {
+ event.returnValue = false; // IE
+ event.cancelBubble = true;