| 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2011 Robert Konigsberg (konigsberg@google.com) |
| 4 | * MIT-licensed (http://opensource.org/licenses/MIT) |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @fileoverview The default interaction model for Dygraphs. This is kept out |
| 9 | * of dygraph.js for better navigability. |
| 10 | * @author Robert Konigsberg (konigsberg@google.com) |
| 11 | */ |
| 12 | |
| 13 | /*jshint globalstrict: true */ |
| 14 | /*global Dygraph:false */ |
| 15 | "use strict"; |
| 16 | |
| 17 | /** |
| 18 | * A collection of functions to facilitate build custom interaction models. |
| 19 | * @class |
| 20 | */ |
| 21 | Dygraph.Interaction = {}; |
| 22 | |
| 23 | /** |
| 24 | * Called in response to an interaction model operation that |
| 25 | * should start the default panning behavior. |
| 26 | * |
| 27 | * It's used in the default callback for "mousedown" operations. |
| 28 | * Custom interaction model builders can use it to provide the default |
| 29 | * panning behavior. |
| 30 | * |
| 31 | * @param {Event} event the event object which led to the startPan call. |
| 32 | * @param {Dygraph} g The dygraph on which to act. |
| 33 | * @param {Object} context The dragging context object (with |
| 34 | * dragStartX/dragStartY/etc. properties). This function modifies the |
| 35 | * context. |
| 36 | */ |
| 37 | Dygraph.Interaction.startPan = function(event, g, context) { |
| 38 | var i, axis; |
| 39 | context.isPanning = true; |
| 40 | var xRange = g.xAxisRange(); |
| 41 | context.dateRange = xRange[1] - xRange[0]; |
| 42 | context.initialLeftmostDate = xRange[0]; |
| 43 | context.xUnitsPerPixel = context.dateRange / (g.plotter_.area.w - 1); |
| 44 | |
| 45 | if (g.attr_("panEdgeFraction")) { |
| 46 | var maxXPixelsToDraw = g.width_ * g.attr_("panEdgeFraction"); |
| 47 | var xExtremes = g.xAxisExtremes(); // I REALLY WANT TO CALL THIS xTremes! |
| 48 | |
| 49 | var boundedLeftX = g.toDomXCoord(xExtremes[0]) - maxXPixelsToDraw; |
| 50 | var boundedRightX = g.toDomXCoord(xExtremes[1]) + maxXPixelsToDraw; |
| 51 | |
| 52 | var boundedLeftDate = g.toDataXCoord(boundedLeftX); |
| 53 | var boundedRightDate = g.toDataXCoord(boundedRightX); |
| 54 | context.boundedDates = [boundedLeftDate, boundedRightDate]; |
| 55 | |
| 56 | var boundedValues = []; |
| 57 | var maxYPixelsToDraw = g.height_ * g.attr_("panEdgeFraction"); |
| 58 | |
| 59 | for (i = 0; i < g.axes_.length; i++) { |
| 60 | axis = g.axes_[i]; |
| 61 | var yExtremes = axis.extremeRange; |
| 62 | |
| 63 | var boundedTopY = g.toDomYCoord(yExtremes[0], i) + maxYPixelsToDraw; |
| 64 | var boundedBottomY = g.toDomYCoord(yExtremes[1], i) - maxYPixelsToDraw; |
| 65 | |
| 66 | var boundedTopValue = g.toDataYCoord(boundedTopY); |
| 67 | var boundedBottomValue = g.toDataYCoord(boundedBottomY); |
| 68 | |
| 69 | boundedValues[i] = [boundedTopValue, boundedBottomValue]; |
| 70 | } |
| 71 | context.boundedValues = boundedValues; |
| 72 | } |
| 73 | |
| 74 | // Record the range of each y-axis at the start of the drag. |
| 75 | // If any axis has a valueRange or valueWindow, then we want a 2D pan. |
| 76 | // We can't store data directly in g.axes_, because it does not belong to us |
| 77 | // and could change out from under us during a pan (say if there's a data |
| 78 | // update). |
| 79 | context.is2DPan = false; |
| 80 | context.axes = []; |
| 81 | for (i = 0; i < g.axes_.length; i++) { |
| 82 | axis = g.axes_[i]; |
| 83 | var axis_data = {}; |
| 84 | var yRange = g.yAxisRange(i); |
| 85 | // TODO(konigsberg): These values should be in |context|. |
| 86 | // In log scale, initialTopValue, dragValueRange and unitsPerPixel are log scale. |
| 87 | var logscale = g.attributes_.getForAxis("logscale", i); |
| 88 | if (logscale) { |
| 89 | axis_data.initialTopValue = Dygraph.log10(yRange[1]); |
| 90 | axis_data.dragValueRange = Dygraph.log10(yRange[1]) - Dygraph.log10(yRange[0]); |
| 91 | } else { |
| 92 | axis_data.initialTopValue = yRange[1]; |
| 93 | axis_data.dragValueRange = yRange[1] - yRange[0]; |
| 94 | } |
| 95 | axis_data.unitsPerPixel = axis_data.dragValueRange / (g.plotter_.area.h - 1); |
| 96 | context.axes.push(axis_data); |
| 97 | |
| 98 | // While calculating axes, set 2dpan. |
| 99 | if (axis.valueWindow || axis.valueRange) context.is2DPan = true; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | /** |
| 104 | * Called in response to an interaction model operation that |
| 105 | * responds to an event that pans the view. |
| 106 | * |
| 107 | * It's used in the default callback for "mousemove" operations. |
| 108 | * Custom interaction model builders can use it to provide the default |
| 109 | * panning behavior. |
| 110 | * |
| 111 | * @param {Event} event the event object which led to the movePan call. |
| 112 | * @param {Dygraph} g The dygraph on which to act. |
| 113 | * @param {Object} context The dragging context object (with |
| 114 | * dragStartX/dragStartY/etc. properties). This function modifies the |
| 115 | * context. |
| 116 | */ |
| 117 | Dygraph.Interaction.movePan = function(event, g, context) { |
| 118 | context.dragEndX = g.dragGetX_(event, context); |
| 119 | context.dragEndY = g.dragGetY_(event, context); |
| 120 | |
| 121 | var minDate = context.initialLeftmostDate - |
| 122 | (context.dragEndX - context.dragStartX) * context.xUnitsPerPixel; |
| 123 | if (context.boundedDates) { |
| 124 | minDate = Math.max(minDate, context.boundedDates[0]); |
| 125 | } |
| 126 | var maxDate = minDate + context.dateRange; |
| 127 | if (context.boundedDates) { |
| 128 | if (maxDate > context.boundedDates[1]) { |
| 129 | // Adjust minDate, and recompute maxDate. |
| 130 | minDate = minDate - (maxDate - context.boundedDates[1]); |
| 131 | maxDate = minDate + context.dateRange; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | g.dateWindow_ = [minDate, maxDate]; |
| 136 | |
| 137 | // y-axis scaling is automatic unless this is a full 2D pan. |
| 138 | if (context.is2DPan) { |
| 139 | // Adjust each axis appropriately. |
| 140 | for (var i = 0; i < g.axes_.length; i++) { |
| 141 | var axis = g.axes_[i]; |
| 142 | var axis_data = context.axes[i]; |
| 143 | |
| 144 | var pixelsDragged = context.dragEndY - context.dragStartY; |
| 145 | var unitsDragged = pixelsDragged * axis_data.unitsPerPixel; |
| 146 | |
| 147 | var boundedValue = context.boundedValues ? context.boundedValues[i] : null; |
| 148 | |
| 149 | // In log scale, maxValue and minValue are the logs of those values. |
| 150 | var maxValue = axis_data.initialTopValue + unitsDragged; |
| 151 | if (boundedValue) { |
| 152 | maxValue = Math.min(maxValue, boundedValue[1]); |
| 153 | } |
| 154 | var minValue = maxValue - axis_data.dragValueRange; |
| 155 | if (boundedValue) { |
| 156 | if (minValue < boundedValue[0]) { |
| 157 | // Adjust maxValue, and recompute minValue. |
| 158 | maxValue = maxValue - (minValue - boundedValue[0]); |
| 159 | minValue = maxValue - axis_data.dragValueRange; |
| 160 | } |
| 161 | } |
| 162 | var logscale = g.attributes_.getForAxis("logscale", i); |
| 163 | if (logscale) { |
| 164 | axis.valueWindow = [ Math.pow(Dygraph.LOG_SCALE, minValue), |
| 165 | Math.pow(Dygraph.LOG_SCALE, maxValue) ]; |
| 166 | } else { |
| 167 | axis.valueWindow = [ minValue, maxValue ]; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | g.drawGraph_(false); |
| 173 | }; |
| 174 | |
| 175 | /** |
| 176 | * Called in response to an interaction model operation that |
| 177 | * responds to an event that ends panning. |
| 178 | * |
| 179 | * It's used in the default callback for "mouseup" operations. |
| 180 | * Custom interaction model builders can use it to provide the default |
| 181 | * panning behavior. |
| 182 | * |
| 183 | * @param {Event} event the event object which led to the endPan call. |
| 184 | * @param {Dygraph} g The dygraph on which to act. |
| 185 | * @param {Object} context The dragging context object (with |
| 186 | * dragStartX/dragStartY/etc. properties). This function modifies the |
| 187 | * context. |
| 188 | */ |
| 189 | Dygraph.Interaction.endPan = function(event, g, context) { |
| 190 | context.dragEndX = g.dragGetX_(event, context); |
| 191 | context.dragEndY = g.dragGetY_(event, context); |
| 192 | |
| 193 | var regionWidth = Math.abs(context.dragEndX - context.dragStartX); |
| 194 | var regionHeight = Math.abs(context.dragEndY - context.dragStartY); |
| 195 | |
| 196 | if (regionWidth < 2 && regionHeight < 2 && |
| 197 | g.lastx_ !== undefined && g.lastx_ != -1) { |
| 198 | Dygraph.Interaction.treatMouseOpAsClick(g, event, context); |
| 199 | } |
| 200 | |
| 201 | // TODO(konigsberg): mouseup should just delete the |
| 202 | // context object, and mousedown should create a new one. |
| 203 | context.isPanning = false; |
| 204 | context.is2DPan = false; |
| 205 | context.initialLeftmostDate = null; |
| 206 | context.dateRange = null; |
| 207 | context.valueRange = null; |
| 208 | context.boundedDates = null; |
| 209 | context.boundedValues = null; |
| 210 | context.axes = null; |
| 211 | }; |
| 212 | |
| 213 | /** |
| 214 | * Called in response to an interaction model operation that |
| 215 | * responds to an event that starts zooming. |
| 216 | * |
| 217 | * It's used in the default callback for "mousedown" operations. |
| 218 | * Custom interaction model builders can use it to provide the default |
| 219 | * zooming behavior. |
| 220 | * |
| 221 | * @param {Event} event the event object which led to the startZoom call. |
| 222 | * @param {Dygraph} g The dygraph on which to act. |
| 223 | * @param {Object} context The dragging context object (with |
| 224 | * dragStartX/dragStartY/etc. properties). This function modifies the |
| 225 | * context. |
| 226 | */ |
| 227 | Dygraph.Interaction.startZoom = function(event, g, context) { |
| 228 | context.isZooming = true; |
| 229 | context.zoomMoved = false; |
| 230 | }; |
| 231 | |
| 232 | /** |
| 233 | * Called in response to an interaction model operation that |
| 234 | * responds to an event that defines zoom boundaries. |
| 235 | * |
| 236 | * It's used in the default callback for "mousemove" operations. |
| 237 | * Custom interaction model builders can use it to provide the default |
| 238 | * zooming behavior. |
| 239 | * |
| 240 | * @param {Event} event the event object which led to the moveZoom call. |
| 241 | * @param {Dygraph} g The dygraph on which to act. |
| 242 | * @param {Object} context The dragging context object (with |
| 243 | * dragStartX/dragStartY/etc. properties). This function modifies the |
| 244 | * context. |
| 245 | */ |
| 246 | Dygraph.Interaction.moveZoom = function(event, g, context) { |
| 247 | context.zoomMoved = true; |
| 248 | context.dragEndX = g.dragGetX_(event, context); |
| 249 | context.dragEndY = g.dragGetY_(event, context); |
| 250 | |
| 251 | var xDelta = Math.abs(context.dragStartX - context.dragEndX); |
| 252 | var yDelta = Math.abs(context.dragStartY - context.dragEndY); |
| 253 | |
| 254 | // drag direction threshold for y axis is twice as large as x axis |
| 255 | context.dragDirection = (xDelta < yDelta / 2) ? Dygraph.VERTICAL : Dygraph.HORIZONTAL; |
| 256 | |
| 257 | g.drawZoomRect_( |
| 258 | context.dragDirection, |
| 259 | context.dragStartX, |
| 260 | context.dragEndX, |
| 261 | context.dragStartY, |
| 262 | context.dragEndY, |
| 263 | context.prevDragDirection, |
| 264 | context.prevEndX, |
| 265 | context.prevEndY); |
| 266 | |
| 267 | context.prevEndX = context.dragEndX; |
| 268 | context.prevEndY = context.dragEndY; |
| 269 | context.prevDragDirection = context.dragDirection; |
| 270 | }; |
| 271 | |
| 272 | /** |
| 273 | * @param {Dygraph} g |
| 274 | * @param {Event} event |
| 275 | * @param {Object} context |
| 276 | */ |
| 277 | Dygraph.Interaction.treatMouseOpAsClick = function(g, event, context) { |
| 278 | var clickCallback = g.attr_('clickCallback'); |
| 279 | var pointClickCallback = g.attr_('pointClickCallback'); |
| 280 | |
| 281 | var selectedPoint = null; |
| 282 | |
| 283 | // Find out if the click occurs on a point. This only matters if there's a |
| 284 | // pointClickCallback. |
| 285 | if (pointClickCallback) { |
| 286 | var closestIdx = -1; |
| 287 | var closestDistance = Number.MAX_VALUE; |
| 288 | |
| 289 | // check if the click was on a particular point. |
| 290 | for (var i = 0; i < g.selPoints_.length; i++) { |
| 291 | var p = g.selPoints_[i]; |
| 292 | var distance = Math.pow(p.canvasx - context.dragEndX, 2) + |
| 293 | Math.pow(p.canvasy - context.dragEndY, 2); |
| 294 | if (!isNaN(distance) && |
| 295 | (closestIdx == -1 || distance < closestDistance)) { |
| 296 | closestDistance = distance; |
| 297 | closestIdx = i; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // Allow any click within two pixels of the dot. |
| 302 | var radius = g.attr_('highlightCircleSize') + 2; |
| 303 | if (closestDistance <= radius * radius) { |
| 304 | selectedPoint = g.selPoints_[closestIdx]; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if (selectedPoint) { |
| 309 | pointClickCallback(event, selectedPoint); |
| 310 | } |
| 311 | |
| 312 | // TODO(danvk): pass along more info about the points, e.g. 'x' |
| 313 | if (clickCallback) { |
| 314 | clickCallback(event, g.lastx_, g.selPoints_); |
| 315 | } |
| 316 | }; |
| 317 | |
| 318 | /** |
| 319 | * Called in response to an interaction model operation that |
| 320 | * responds to an event that performs a zoom based on previously defined |
| 321 | * bounds.. |
| 322 | * |
| 323 | * It's used in the default callback for "mouseup" operations. |
| 324 | * Custom interaction model builders can use it to provide the default |
| 325 | * zooming behavior. |
| 326 | * |
| 327 | * @param {Event} event the event object which led to the endZoom call. |
| 328 | * @param {Dygraph} g The dygraph on which to end the zoom. |
| 329 | * @param {Object} context The dragging context object (with |
| 330 | * dragStartX/dragStartY/etc. properties). This function modifies the |
| 331 | * context. |
| 332 | */ |
| 333 | Dygraph.Interaction.endZoom = function(event, g, context) { |
| 334 | context.isZooming = false; |
| 335 | context.dragEndX = g.dragGetX_(event, context); |
| 336 | context.dragEndY = g.dragGetY_(event, context); |
| 337 | var regionWidth = Math.abs(context.dragEndX - context.dragStartX); |
| 338 | var regionHeight = Math.abs(context.dragEndY - context.dragStartY); |
| 339 | |
| 340 | if (regionWidth < 2 && regionHeight < 2 && |
| 341 | g.lastx_ !== undefined && g.lastx_ != -1) { |
| 342 | Dygraph.Interaction.treatMouseOpAsClick(g, event, context); |
| 343 | } |
| 344 | |
| 345 | if (regionWidth >= 10 && context.dragDirection == Dygraph.HORIZONTAL) { |
| 346 | g.doZoomX_(Math.min(context.dragStartX, context.dragEndX), |
| 347 | Math.max(context.dragStartX, context.dragEndX)); |
| 348 | context.cancelNextDblclick = true; |
| 349 | } else if (regionHeight >= 10 && context.dragDirection == Dygraph.VERTICAL) { |
| 350 | g.doZoomY_(Math.min(context.dragStartY, context.dragEndY), |
| 351 | Math.max(context.dragStartY, context.dragEndY)); |
| 352 | context.cancelNextDblclick = true; |
| 353 | } else { |
| 354 | if (context.zoomMoved) g.clearZoomRect_(); |
| 355 | } |
| 356 | context.dragStartX = null; |
| 357 | context.dragStartY = null; |
| 358 | }; |
| 359 | |
| 360 | /** |
| 361 | * @private |
| 362 | */ |
| 363 | Dygraph.Interaction.startTouch = function(event, g, context) { |
| 364 | event.preventDefault(); // touch browsers are all nice. |
| 365 | var touches = []; |
| 366 | for (var i = 0; i < event.touches.length; i++) { |
| 367 | var t = event.touches[i]; |
| 368 | // we dispense with 'dragGetX_' because all touchBrowsers support pageX |
| 369 | touches.push({ |
| 370 | pageX: t.pageX, |
| 371 | pageY: t.pageY, |
| 372 | dataX: g.toDataXCoord(t.pageX), |
| 373 | dataY: g.toDataYCoord(t.pageY) |
| 374 | // identifier: t.identifier |
| 375 | }); |
| 376 | } |
| 377 | context.initialTouches = touches; |
| 378 | |
| 379 | if (touches.length == 1) { |
| 380 | // This is just a swipe. |
| 381 | context.initialPinchCenter = touches[0]; |
| 382 | context.touchDirections = { x: true, y: true }; |
| 383 | } else if (touches.length == 2) { |
| 384 | // It's become a pinch! |
| 385 | |
| 386 | // only screen coordinates can be averaged (data coords could be log scale). |
| 387 | context.initialPinchCenter = { |
| 388 | pageX: 0.5 * (touches[0].pageX + touches[1].pageX), |
| 389 | pageY: 0.5 * (touches[0].pageY + touches[1].pageY), |
| 390 | |
| 391 | // TODO(danvk): remove |
| 392 | dataX: 0.5 * (touches[0].dataX + touches[1].dataX), |
| 393 | dataY: 0.5 * (touches[0].dataY + touches[1].dataY) |
| 394 | }; |
| 395 | |
| 396 | // Make pinches in a 45-degree swath around either axis 1-dimensional zooms. |
| 397 | var initialAngle = 180 / Math.PI * Math.atan2( |
| 398 | context.initialPinchCenter.pageY - touches[0].pageY, |
| 399 | touches[0].pageX - context.initialPinchCenter.pageX); |
| 400 | |
| 401 | // use symmetry to get it into the first quadrant. |
| 402 | initialAngle = Math.abs(initialAngle); |
| 403 | if (initialAngle > 90) initialAngle = 90 - initialAngle; |
| 404 | |
| 405 | context.touchDirections = { |
| 406 | x: (initialAngle < (90 - 45/2)), |
| 407 | y: (initialAngle > 45/2) |
| 408 | }; |
| 409 | } |
| 410 | |
| 411 | // save the full x & y ranges. |
| 412 | context.initialRange = { |
| 413 | x: g.xAxisRange(), |
| 414 | y: g.yAxisRange() |
| 415 | }; |
| 416 | }; |
| 417 | |
| 418 | /** |
| 419 | * @private |
| 420 | */ |
| 421 | Dygraph.Interaction.moveTouch = function(event, g, context) { |
| 422 | var i, touches = []; |
| 423 | for (i = 0; i < event.touches.length; i++) { |
| 424 | var t = event.touches[i]; |
| 425 | touches.push({ |
| 426 | pageX: t.pageX, |
| 427 | pageY: t.pageY |
| 428 | }); |
| 429 | } |
| 430 | var initialTouches = context.initialTouches; |
| 431 | |
| 432 | var c_now; |
| 433 | |
| 434 | // old and new centers. |
| 435 | var c_init = context.initialPinchCenter; |
| 436 | if (touches.length == 1) { |
| 437 | c_now = touches[0]; |
| 438 | } else { |
| 439 | c_now = { |
| 440 | pageX: 0.5 * (touches[0].pageX + touches[1].pageX), |
| 441 | pageY: 0.5 * (touches[0].pageY + touches[1].pageY) |
| 442 | }; |
| 443 | } |
| 444 | |
| 445 | // this is the "swipe" component |
| 446 | // we toss it out for now, but could use it in the future. |
| 447 | var swipe = { |
| 448 | pageX: c_now.pageX - c_init.pageX, |
| 449 | pageY: c_now.pageY - c_init.pageY |
| 450 | }; |
| 451 | var dataWidth = context.initialRange.x[1] - context.initialRange.x[0]; |
| 452 | var dataHeight = context.initialRange.y[0] - context.initialRange.y[1]; |
| 453 | swipe.dataX = (swipe.pageX / g.plotter_.area.w) * dataWidth; |
| 454 | swipe.dataY = (swipe.pageY / g.plotter_.area.h) * dataHeight; |
| 455 | var xScale, yScale; |
| 456 | |
| 457 | // The residual bits are usually split into scale & rotate bits, but we split |
| 458 | // them into x-scale and y-scale bits. |
| 459 | if (touches.length == 1) { |
| 460 | xScale = 1.0; |
| 461 | yScale = 1.0; |
| 462 | } else if (touches.length == 2) { |
| 463 | var initHalfWidth = (initialTouches[1].pageX - c_init.pageX); |
| 464 | xScale = (touches[1].pageX - c_now.pageX) / initHalfWidth; |
| 465 | |
| 466 | var initHalfHeight = (initialTouches[1].pageY - c_init.pageY); |
| 467 | yScale = (touches[1].pageY - c_now.pageY) / initHalfHeight; |
| 468 | } |
| 469 | |
| 470 | // Clip scaling to [1/8, 8] to prevent too much blowup. |
| 471 | xScale = Math.min(8, Math.max(0.125, xScale)); |
| 472 | yScale = Math.min(8, Math.max(0.125, yScale)); |
| 473 | |
| 474 | if (context.touchDirections.x) { |
| 475 | g.dateWindow_ = [ |
| 476 | c_init.dataX - swipe.dataX + (context.initialRange.x[0] - c_init.dataX) / xScale, |
| 477 | c_init.dataX - swipe.dataX + (context.initialRange.x[1] - c_init.dataX) / xScale |
| 478 | ]; |
| 479 | } |
| 480 | |
| 481 | if (context.touchDirections.y) { |
| 482 | for (i = 0; i < 1 /*g.axes_.length*/; i++) { |
| 483 | var axis = g.axes_[i]; |
| 484 | var logscale = g.attributes_.getForAxis("logscale", i); |
| 485 | if (logscale) { |
| 486 | // TODO(danvk): implement |
| 487 | } else { |
| 488 | axis.valueWindow = [ |
| 489 | c_init.dataY - swipe.dataY + (context.initialRange.y[0] - c_init.dataY) / yScale, |
| 490 | c_init.dataY - swipe.dataY + (context.initialRange.y[1] - c_init.dataY) / yScale |
| 491 | ]; |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | g.drawGraph_(false); |
| 497 | }; |
| 498 | |
| 499 | /** |
| 500 | * @private |
| 501 | */ |
| 502 | Dygraph.Interaction.endTouch = function(event, g, context) { |
| 503 | if (event.touches.length !== 0) { |
| 504 | // this is effectively a "reset" |
| 505 | Dygraph.Interaction.startTouch(event, g, context); |
| 506 | } |
| 507 | }; |
| 508 | |
| 509 | /** |
| 510 | * Default interation model for dygraphs. You can refer to specific elements of |
| 511 | * this when constructing your own interaction model, e.g.: |
| 512 | * g.updateOptions( { |
| 513 | * interactionModel: { |
| 514 | * mousedown: Dygraph.defaultInteractionModel.mousedown |
| 515 | * } |
| 516 | * } ); |
| 517 | */ |
| 518 | Dygraph.Interaction.defaultModel = { |
| 519 | // Track the beginning of drag events |
| 520 | mousedown: function(event, g, context) { |
| 521 | // Right-click should not initiate a zoom. |
| 522 | if (event.button && event.button == 2) return; |
| 523 | |
| 524 | context.initializeMouseDown(event, g, context); |
| 525 | |
| 526 | if (event.altKey || event.shiftKey) { |
| 527 | Dygraph.startPan(event, g, context); |
| 528 | } else { |
| 529 | Dygraph.startZoom(event, g, context); |
| 530 | } |
| 531 | }, |
| 532 | |
| 533 | // Draw zoom rectangles when the mouse is down and the user moves around |
| 534 | mousemove: function(event, g, context) { |
| 535 | if (context.isZooming) { |
| 536 | Dygraph.moveZoom(event, g, context); |
| 537 | } else if (context.isPanning) { |
| 538 | Dygraph.movePan(event, g, context); |
| 539 | } |
| 540 | }, |
| 541 | |
| 542 | mouseup: function(event, g, context) { |
| 543 | if (context.isZooming) { |
| 544 | Dygraph.endZoom(event, g, context); |
| 545 | } else if (context.isPanning) { |
| 546 | Dygraph.endPan(event, g, context); |
| 547 | } |
| 548 | }, |
| 549 | |
| 550 | touchstart: function(event, g, context) { |
| 551 | Dygraph.Interaction.startTouch(event, g, context); |
| 552 | }, |
| 553 | touchmove: function(event, g, context) { |
| 554 | Dygraph.Interaction.moveTouch(event, g, context); |
| 555 | }, |
| 556 | touchend: function(event, g, context) { |
| 557 | Dygraph.Interaction.endTouch(event, g, context); |
| 558 | }, |
| 559 | |
| 560 | // Temporarily cancel the dragging event when the mouse leaves the graph |
| 561 | mouseout: function(event, g, context) { |
| 562 | if (context.isZooming) { |
| 563 | context.dragEndX = null; |
| 564 | context.dragEndY = null; |
| 565 | } |
| 566 | }, |
| 567 | |
| 568 | // Disable zooming out if panning. |
| 569 | dblclick: function(event, g, context) { |
| 570 | if (context.cancelNextDblclick) { |
| 571 | context.cancelNextDblclick = false; |
| 572 | return; |
| 573 | } |
| 574 | if (event.altKey || event.shiftKey) { |
| 575 | return; |
| 576 | } |
| 577 | g.resetZoom(); |
| 578 | } |
| 579 | }; |
| 580 | |
| 581 | Dygraph.DEFAULT_ATTRS.interactionModel = Dygraph.Interaction.defaultModel; |
| 582 | |
| 583 | // old ways of accessing these methods/properties |
| 584 | Dygraph.defaultInteractionModel = Dygraph.Interaction.defaultModel; |
| 585 | Dygraph.endZoom = Dygraph.Interaction.endZoom; |
| 586 | Dygraph.moveZoom = Dygraph.Interaction.moveZoom; |
| 587 | Dygraph.startZoom = Dygraph.Interaction.startZoom; |
| 588 | Dygraph.endPan = Dygraph.Interaction.endPan; |
| 589 | Dygraph.movePan = Dygraph.Interaction.movePan; |
| 590 | Dygraph.startPan = Dygraph.Interaction.startPan; |
| 591 | |
| 592 | Dygraph.Interaction.nonInteractiveModel_ = { |
| 593 | mousedown: function(event, g, context) { |
| 594 | context.initializeMouseDown(event, g, context); |
| 595 | }, |
| 596 | mouseup: function(event, g, context) { |
| 597 | // TODO(danvk): this logic is repeated in Dygraph.Interaction.endZoom |
| 598 | context.dragEndX = g.dragGetX_(event, context); |
| 599 | context.dragEndY = g.dragGetY_(event, context); |
| 600 | var regionWidth = Math.abs(context.dragEndX - context.dragStartX); |
| 601 | var regionHeight = Math.abs(context.dragEndY - context.dragStartY); |
| 602 | |
| 603 | if (regionWidth < 2 && regionHeight < 2 && |
| 604 | g.lastx_ !== undefined && g.lastx_ != -1) { |
| 605 | Dygraph.Interaction.treatMouseOpAsClick(g, event, context); |
| 606 | } |
| 607 | } |
| 608 | }; |
| 609 | |
| 610 | // Default interaction model when using the range selector. |
| 611 | Dygraph.Interaction.dragIsPanInteractionModel = { |
| 612 | mousedown: function(event, g, context) { |
| 613 | context.initializeMouseDown(event, g, context); |
| 614 | Dygraph.startPan(event, g, context); |
| 615 | }, |
| 616 | mousemove: function(event, g, context) { |
| 617 | if (context.isPanning) { |
| 618 | Dygraph.movePan(event, g, context); |
| 619 | } |
| 620 | }, |
| 621 | mouseup: function(event, g, context) { |
| 622 | if (context.isPanning) { |
| 623 | Dygraph.endPan(event, g, context); |
| 624 | } |
| 625 | } |
| 626 | }; |