X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph-interaction-model.js;h=c57ad2556bb14f96393c1aea83677a267a2c0f5c;hb=01681f66b26293c86564ed28259cb3093dee098b;hp=32809f57d6b106997ce4523646f73ebc3882c0e7;hpb=0290d079ee1d4558b652877c81621884f425fc3a;p=dygraphs.git diff --git a/dygraph-interaction-model.js b/dygraph-interaction-model.js index 32809f5..c57ad25 100644 --- a/dygraph-interaction-model.js +++ b/dygraph-interaction-model.js @@ -1,12 +1,18 @@ -// Copyright 2011 Robert Konigsberg (konigsberg@google.com) -// All Rights Reserved. +/** + * @license + * Copyright 2011 Robert Konigsberg (konigsberg@google.com) + * MIT-licensed (http://opensource.org/licenses/MIT) + */ -/** +/** * @fileoverview The default interaction model for Dygraphs. This is kept out * of dygraph.js for better navigability. * @author Robert Konigsberg (konigsberg@google.com) */ +/*jshint globalstrict: true */ +/*global Dygraph:false */ +"use strict"; /** * A collection of functions to facilitate build custom interaction models. @@ -28,6 +34,7 @@ Dygraph.Interaction = {}; * dragStartX/dragStartY/etc. properties). This function modifies the context. */ Dygraph.Interaction.startPan = function(event, g, context) { + var i, axis; context.isPanning = true; var xRange = g.xAxisRange(); context.dateRange = xRange[1] - xRange[0]; @@ -48,8 +55,8 @@ Dygraph.Interaction.startPan = function(event, g, context) { var boundedValues = []; var maxYPixelsToDraw = g.height_ * g.attr_("panEdgeFraction"); - for (var i = 0; i < g.axes_.length; i++) { - var axis = g.axes_[i]; + for (i = 0; i < g.axes_.length; i++) { + axis = g.axes_[i]; var yExtremes = axis.extremeRange; var boundedTopY = g.toDomYCoord(yExtremes[0], i) + maxYPixelsToDraw; @@ -65,20 +72,26 @@ Dygraph.Interaction.startPan = function(event, g, context) { // Record the range of each y-axis at the start of the drag. // If any axis has a valueRange or valueWindow, then we want a 2D pan. + // We can't store data directly in g.axes_, because it does not belong to us + // and could change out from under us during a pan (say if there's a data + // update). context.is2DPan = false; - for (var i = 0; i < g.axes_.length; i++) { - var axis = g.axes_[i]; + context.axes = []; + for (i = 0; i < g.axes_.length; i++) { + axis = g.axes_[i]; + var axis_data = {}; var yRange = g.yAxisRange(i); // TODO(konigsberg): These values should be in |context|. // In log scale, initialTopValue, dragValueRange and unitsPerPixel are log scale. if (axis.logscale) { - axis.initialTopValue = Dygraph.log10(yRange[1]); - axis.dragValueRange = Dygraph.log10(yRange[1]) - Dygraph.log10(yRange[0]); + axis_data.initialTopValue = Dygraph.log10(yRange[1]); + axis_data.dragValueRange = Dygraph.log10(yRange[1]) - Dygraph.log10(yRange[0]); } else { - axis.initialTopValue = yRange[1]; - axis.dragValueRange = yRange[1] - yRange[0]; + axis_data.initialTopValue = yRange[1]; + axis_data.dragValueRange = yRange[1] - yRange[0]; } - axis.unitsPerPixel = axis.dragValueRange / (g.plotter_.area.h - 1); + axis_data.unitsPerPixel = axis_data.dragValueRange / (g.plotter_.area.h - 1); + context.axes.push(axis_data); // While calculating axes, set 2dpan. if (axis.valueWindow || axis.valueRange) context.is2DPan = true; @@ -123,23 +136,24 @@ Dygraph.Interaction.movePan = function(event, g, context) { // Adjust each axis appropriately. for (var i = 0; i < g.axes_.length; i++) { var axis = g.axes_[i]; + var axis_data = context.axes[i]; var pixelsDragged = context.dragEndY - context.dragStartY; - var unitsDragged = pixelsDragged * axis.unitsPerPixel; - + var unitsDragged = pixelsDragged * axis_data.unitsPerPixel; + var boundedValue = context.boundedValues ? context.boundedValues[i] : null; // In log scale, maxValue and minValue are the logs of those values. - var maxValue = axis.initialTopValue + unitsDragged; + var maxValue = axis_data.initialTopValue + unitsDragged; if (boundedValue) { maxValue = Math.min(maxValue, boundedValue[1]); } - var minValue = maxValue - axis.dragValueRange; + var minValue = maxValue - axis_data.dragValueRange; if (boundedValue) { if (minValue < boundedValue[0]) { // Adjust maxValue, and recompute minValue. maxValue = maxValue - (minValue - boundedValue[0]); - minValue = maxValue - axis.dragValueRange; + minValue = maxValue - axis_data.dragValueRange; } } if (axis.logscale) { @@ -175,12 +189,10 @@ Dygraph.Interaction.endPan = function(event, g, context) { var regionHeight = Math.abs(context.dragEndY - context.dragStartY); if (regionWidth < 2 && regionHeight < 2 && - g.lastx_ != undefined && g.lastx_ != -1) { + g.lastx_ !== undefined && g.lastx_ != -1) { Dygraph.Interaction.treatMouseOpAsClick(g, event, context); } - // TODO(konigsberg): Clear the context data from the axis. - // (replace with "context = {}" ?) // TODO(konigsberg): mouseup should just delete the // context object, and mousedown should create a new one. context.isPanning = false; @@ -190,6 +202,7 @@ Dygraph.Interaction.endPan = function(event, g, context) { context.valueRange = null; context.boundedDates = null; context.boundedValues = null; + context.axes = null; }; /** @@ -263,7 +276,8 @@ Dygraph.Interaction.treatMouseOpAsClick = function(g, event, context) { var p = g.selPoints_[i]; var distance = Math.pow(p.canvasx - context.dragEndX, 2) + Math.pow(p.canvasy - context.dragEndY, 2); - if (closestIdx == -1 || distance < closestDistance) { + if (!isNaN(distance) && + (closestIdx == -1 || distance < closestDistance)) { closestDistance = distance; closestIdx = i; } @@ -308,7 +322,7 @@ Dygraph.Interaction.endZoom = function(event, g, context) { var regionHeight = Math.abs(context.dragEndY - context.dragStartY); if (regionWidth < 2 && regionHeight < 2 && - g.lastx_ != undefined && g.lastx_ != -1) { + g.lastx_ !== undefined && g.lastx_ != -1) { Dygraph.Interaction.treatMouseOpAsClick(g, event, context); } @@ -319,7 +333,7 @@ Dygraph.Interaction.endZoom = function(event, g, context) { g.doZoomY_(Math.min(context.dragStartY, context.dragEndY), Math.max(context.dragStartY, context.dragEndY)); } else { - g.canvas_ctx_.clearRect(0, 0, g.canvas_.width, g.canvas_.height); + g.clearZoomRect_(); } context.dragStartX = null; context.dragStartY = null; @@ -405,8 +419,26 @@ Dygraph.Interaction.nonInteractiveModel_ = { var regionHeight = Math.abs(context.dragEndY - context.dragStartY); if (regionWidth < 2 && regionHeight < 2 && - g.lastx_ != undefined && g.lastx_ != -1) { + g.lastx_ !== undefined && g.lastx_ != -1) { Dygraph.Interaction.treatMouseOpAsClick(g, event, context); } } }; + +// Default interaction model when using the range selector. +Dygraph.Interaction.dragIsPanInteractionModel = { + mousedown: function(event, g, context) { + context.initializeMouseDown(event, g, context); + Dygraph.startPan(event, g, context); + }, + mousemove: function(event, g, context) { + if (context.isPanning) { + Dygraph.movePan(event, g, context); + } + }, + mouseup: function(event, g, context) { + if (context.isPanning) { + Dygraph.endPan(event, g, context); + } + } +};