| 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 context. |
| 35 | */ |
| 36 | Dygraph.Interaction.startPan = function(event, g, context) { |
| 37 | var i, axis; |
| 38 | context.isPanning = true; |
| 39 | var xRange = g.xAxisRange(); |
| 40 | context.dateRange = xRange[1] - xRange[0]; |
| 41 | context.initialLeftmostDate = xRange[0]; |
| 42 | context.xUnitsPerPixel = context.dateRange / (g.plotter_.area.w - 1); |
| 43 | |
| 44 | if (g.attr_("panEdgeFraction")) { |
| 45 | var maxXPixelsToDraw = g.width_ * g.attr_("panEdgeFraction"); |
| 46 | var xExtremes = g.xAxisExtremes(); // I REALLY WANT TO CALL THIS xTremes! |
| 47 | |
| 48 | var boundedLeftX = g.toDomXCoord(xExtremes[0]) - maxXPixelsToDraw; |
| 49 | var boundedRightX = g.toDomXCoord(xExtremes[1]) + maxXPixelsToDraw; |
| 50 | |
| 51 | var boundedLeftDate = g.toDataXCoord(boundedLeftX); |
| 52 | var boundedRightDate = g.toDataXCoord(boundedRightX); |
| 53 | context.boundedDates = [boundedLeftDate, boundedRightDate]; |
| 54 | |
| 55 | var boundedValues = []; |
| 56 | var maxYPixelsToDraw = g.height_ * g.attr_("panEdgeFraction"); |
| 57 | |
| 58 | for (i = 0; i < g.axes_.length; i++) { |
| 59 | axis = g.axes_[i]; |
| 60 | var yExtremes = axis.extremeRange; |
| 61 | |
| 62 | var boundedTopY = g.toDomYCoord(yExtremes[0], i) + maxYPixelsToDraw; |
| 63 | var boundedBottomY = g.toDomYCoord(yExtremes[1], i) - maxYPixelsToDraw; |
| 64 | |
| 65 | var boundedTopValue = g.toDataYCoord(boundedTopY); |
| 66 | var boundedBottomValue = g.toDataYCoord(boundedBottomY); |
| 67 | |
| 68 | boundedValues[i] = [boundedTopValue, boundedBottomValue]; |
| 69 | } |
| 70 | context.boundedValues = boundedValues; |
| 71 | } |
| 72 | |
| 73 | // Record the range of each y-axis at the start of the drag. |
| 74 | // If any axis has a valueRange or valueWindow, then we want a 2D pan. |
| 75 | // We can't store data directly in g.axes_, because it does not belong to us |
| 76 | // and could change out from under us during a pan (say if there's a data |
| 77 | // update). |
| 78 | context.is2DPan = false; |
| 79 | context.axes = []; |
| 80 | for (i = 0; i < g.axes_.length; i++) { |
| 81 | axis = g.axes_[i]; |
| 82 | var axis_data = {}; |
| 83 | var yRange = g.yAxisRange(i); |
| 84 | // TODO(konigsberg): These values should be in |context|. |
| 85 | // In log scale, initialTopValue, dragValueRange and unitsPerPixel are log scale. |
| 86 | if (axis.logscale) { |
| 87 | axis_data.initialTopValue = Dygraph.log10(yRange[1]); |
| 88 | axis_data.dragValueRange = Dygraph.log10(yRange[1]) - Dygraph.log10(yRange[0]); |
| 89 | } else { |
| 90 | axis_data.initialTopValue = yRange[1]; |
| 91 | axis_data.dragValueRange = yRange[1] - yRange[0]; |
| 92 | } |
| 93 | axis_data.unitsPerPixel = axis_data.dragValueRange / (g.plotter_.area.h - 1); |
| 94 | context.axes.push(axis_data); |
| 95 | |
| 96 | // While calculating axes, set 2dpan. |
| 97 | if (axis.valueWindow || axis.valueRange) context.is2DPan = true; |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | /** |
| 102 | * Called in response to an interaction model operation that |
| 103 | * responds to an event that pans the view. |
| 104 | * |
| 105 | * It's used in the default callback for "mousemove" operations. |
| 106 | * Custom interaction model builders can use it to provide the default |
| 107 | * panning behavior. |
| 108 | * |
| 109 | * @param { Event } event the event object which led to the movePan call. |
| 110 | * @param { Dygraph} g The dygraph on which to act. |
| 111 | * @param { Object} context The dragging context object (with |
| 112 | * dragStartX/dragStartY/etc. properties). This function modifies the context. |
| 113 | */ |
| 114 | Dygraph.Interaction.movePan = function(event, g, context) { |
| 115 | context.dragEndX = g.dragGetX_(event, context); |
| 116 | context.dragEndY = g.dragGetY_(event, context); |
| 117 | |
| 118 | var minDate = context.initialLeftmostDate - |
| 119 | (context.dragEndX - context.dragStartX) * context.xUnitsPerPixel; |
| 120 | if (context.boundedDates) { |
| 121 | minDate = Math.max(minDate, context.boundedDates[0]); |
| 122 | } |
| 123 | var maxDate = minDate + context.dateRange; |
| 124 | if (context.boundedDates) { |
| 125 | if (maxDate > context.boundedDates[1]) { |
| 126 | // Adjust minDate, and recompute maxDate. |
| 127 | minDate = minDate - (maxDate - context.boundedDates[1]); |
| 128 | maxDate = minDate + context.dateRange; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | g.dateWindow_ = [minDate, maxDate]; |
| 133 | |
| 134 | // y-axis scaling is automatic unless this is a full 2D pan. |
| 135 | if (context.is2DPan) { |
| 136 | // Adjust each axis appropriately. |
| 137 | for (var i = 0; i < g.axes_.length; i++) { |
| 138 | var axis = g.axes_[i]; |
| 139 | var axis_data = context.axes[i]; |
| 140 | |
| 141 | var pixelsDragged = context.dragEndY - context.dragStartY; |
| 142 | var unitsDragged = pixelsDragged * axis_data.unitsPerPixel; |
| 143 | |
| 144 | var boundedValue = context.boundedValues ? context.boundedValues[i] : null; |
| 145 | |
| 146 | // In log scale, maxValue and minValue are the logs of those values. |
| 147 | var maxValue = axis_data.initialTopValue + unitsDragged; |
| 148 | if (boundedValue) { |
| 149 | maxValue = Math.min(maxValue, boundedValue[1]); |
| 150 | } |
| 151 | var minValue = maxValue - axis_data.dragValueRange; |
| 152 | if (boundedValue) { |
| 153 | if (minValue < boundedValue[0]) { |
| 154 | // Adjust maxValue, and recompute minValue. |
| 155 | maxValue = maxValue - (minValue - boundedValue[0]); |
| 156 | minValue = maxValue - axis_data.dragValueRange; |
| 157 | } |
| 158 | } |
| 159 | if (axis.logscale) { |
| 160 | axis.valueWindow = [ Math.pow(Dygraph.LOG_SCALE, minValue), |
| 161 | Math.pow(Dygraph.LOG_SCALE, maxValue) ]; |
| 162 | } else { |
| 163 | axis.valueWindow = [ minValue, maxValue ]; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | g.drawGraph_(false); |
| 169 | }; |
| 170 | |
| 171 | /** |
| 172 | * Called in response to an interaction model operation that |
| 173 | * responds to an event that ends panning. |
| 174 | * |
| 175 | * It's used in the default callback for "mouseup" operations. |
| 176 | * Custom interaction model builders can use it to provide the default |
| 177 | * panning behavior. |
| 178 | * |
| 179 | * @param { Event } event the event object which led to the startZoom call. |
| 180 | * @param { Dygraph} g The dygraph on which to act. |
| 181 | * @param { Object} context The dragging context object (with |
| 182 | * dragStartX/dragStartY/etc. properties). This function modifies the context. |
| 183 | */ |
| 184 | Dygraph.Interaction.endPan = function(event, g, context) { |
| 185 | context.dragEndX = g.dragGetX_(event, context); |
| 186 | context.dragEndY = g.dragGetY_(event, context); |
| 187 | |
| 188 | var regionWidth = Math.abs(context.dragEndX - context.dragStartX); |
| 189 | var regionHeight = Math.abs(context.dragEndY - context.dragStartY); |
| 190 | |
| 191 | if (regionWidth < 2 && regionHeight < 2 && |
| 192 | g.lastx_ !== undefined && g.lastx_ != -1) { |
| 193 | Dygraph.Interaction.treatMouseOpAsClick(g, event, context); |
| 194 | } |
| 195 | |
| 196 | // TODO(konigsberg): mouseup should just delete the |
| 197 | // context object, and mousedown should create a new one. |
| 198 | context.isPanning = false; |
| 199 | context.is2DPan = false; |
| 200 | context.initialLeftmostDate = null; |
| 201 | context.dateRange = null; |
| 202 | context.valueRange = null; |
| 203 | context.boundedDates = null; |
| 204 | context.boundedValues = null; |
| 205 | context.axes = null; |
| 206 | }; |
| 207 | |
| 208 | /** |
| 209 | * Called in response to an interaction model operation that |
| 210 | * responds to an event that starts zooming. |
| 211 | * |
| 212 | * It's used in the default callback for "mousedown" operations. |
| 213 | * Custom interaction model builders can use it to provide the default |
| 214 | * zooming behavior. |
| 215 | * |
| 216 | * @param { Event } event the event object which led to the startZoom call. |
| 217 | * @param { Dygraph} g The dygraph on which to act. |
| 218 | * @param { Object} context The dragging context object (with |
| 219 | * dragStartX/dragStartY/etc. properties). This function modifies the context. |
| 220 | */ |
| 221 | Dygraph.Interaction.startZoom = function(event, g, context) { |
| 222 | context.isZooming = true; |
| 223 | }; |
| 224 | |
| 225 | /** |
| 226 | * Called in response to an interaction model operation that |
| 227 | * responds to an event that defines zoom boundaries. |
| 228 | * |
| 229 | * It's used in the default callback for "mousemove" operations. |
| 230 | * Custom interaction model builders can use it to provide the default |
| 231 | * zooming behavior. |
| 232 | * |
| 233 | * @param { Event } event the event object which led to the moveZoom call. |
| 234 | * @param { Dygraph} g The dygraph on which to act. |
| 235 | * @param { Object} context The dragging context object (with |
| 236 | * dragStartX/dragStartY/etc. properties). This function modifies the context. |
| 237 | */ |
| 238 | Dygraph.Interaction.moveZoom = function(event, g, context) { |
| 239 | context.dragEndX = g.dragGetX_(event, context); |
| 240 | context.dragEndY = g.dragGetY_(event, context); |
| 241 | |
| 242 | var xDelta = Math.abs(context.dragStartX - context.dragEndX); |
| 243 | var yDelta = Math.abs(context.dragStartY - context.dragEndY); |
| 244 | |
| 245 | // drag direction threshold for y axis is twice as large as x axis |
| 246 | context.dragDirection = (xDelta < yDelta / 2) ? Dygraph.VERTICAL : Dygraph.HORIZONTAL; |
| 247 | |
| 248 | g.drawZoomRect_( |
| 249 | context.dragDirection, |
| 250 | context.dragStartX, |
| 251 | context.dragEndX, |
| 252 | context.dragStartY, |
| 253 | context.dragEndY, |
| 254 | context.prevDragDirection, |
| 255 | context.prevEndX, |
| 256 | context.prevEndY); |
| 257 | |
| 258 | context.prevEndX = context.dragEndX; |
| 259 | context.prevEndY = context.dragEndY; |
| 260 | context.prevDragDirection = context.dragDirection; |
| 261 | }; |
| 262 | |
| 263 | Dygraph.Interaction.treatMouseOpAsClick = function(g, event, context) { |
| 264 | var clickCallback = g.attr_('clickCallback'); |
| 265 | var pointClickCallback = g.attr_('pointClickCallback'); |
| 266 | |
| 267 | var selectedPoint = null; |
| 268 | |
| 269 | // Find out if the click occurs on a point. This only matters if there's a pointClickCallback. |
| 270 | if (pointClickCallback) { |
| 271 | var closestIdx = -1; |
| 272 | var closestDistance = Number.MAX_VALUE; |
| 273 | |
| 274 | // check if the click was on a particular point. |
| 275 | for (var i = 0; i < g.selPoints_.length; i++) { |
| 276 | var p = g.selPoints_[i]; |
| 277 | var distance = Math.pow(p.canvasx - context.dragEndX, 2) + |
| 278 | Math.pow(p.canvasy - context.dragEndY, 2); |
| 279 | if (!isNaN(distance) && |
| 280 | (closestIdx == -1 || distance < closestDistance)) { |
| 281 | closestDistance = distance; |
| 282 | closestIdx = i; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // Allow any click within two pixels of the dot. |
| 287 | var radius = g.attr_('highlightCircleSize') + 2; |
| 288 | if (closestDistance <= radius * radius) { |
| 289 | selectedPoint = g.selPoints_[closestIdx]; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if (selectedPoint) { |
| 294 | pointClickCallback(event, selectedPoint); |
| 295 | } |
| 296 | |
| 297 | // TODO(danvk): pass along more info about the points, e.g. 'x' |
| 298 | if (clickCallback) { |
| 299 | clickCallback(event, g.lastx_, g.selPoints_); |
| 300 | } |
| 301 | }; |
| 302 | |
| 303 | /** |
| 304 | * Called in response to an interaction model operation that |
| 305 | * responds to an event that performs a zoom based on previously defined |
| 306 | * bounds.. |
| 307 | * |
| 308 | * It's used in the default callback for "mouseup" operations. |
| 309 | * Custom interaction model builders can use it to provide the default |
| 310 | * zooming behavior. |
| 311 | * |
| 312 | * @param { Event } event the event object which led to the endZoom call. |
| 313 | * @param { Dygraph} g The dygraph on which to end the zoom. |
| 314 | * @param { Object} context The dragging context object (with |
| 315 | * dragStartX/dragStartY/etc. properties). This function modifies the context. |
| 316 | */ |
| 317 | Dygraph.Interaction.endZoom = function(event, g, context) { |
| 318 | context.isZooming = false; |
| 319 | context.dragEndX = g.dragGetX_(event, context); |
| 320 | context.dragEndY = g.dragGetY_(event, context); |
| 321 | var regionWidth = Math.abs(context.dragEndX - context.dragStartX); |
| 322 | var regionHeight = Math.abs(context.dragEndY - context.dragStartY); |
| 323 | |
| 324 | if (regionWidth < 2 && regionHeight < 2 && |
| 325 | g.lastx_ !== undefined && g.lastx_ != -1) { |
| 326 | Dygraph.Interaction.treatMouseOpAsClick(g, event, context); |
| 327 | } |
| 328 | |
| 329 | if (regionWidth >= 10 && context.dragDirection == Dygraph.HORIZONTAL) { |
| 330 | g.doZoomX_(Math.min(context.dragStartX, context.dragEndX), |
| 331 | Math.max(context.dragStartX, context.dragEndX)); |
| 332 | } else if (regionHeight >= 10 && context.dragDirection == Dygraph.VERTICAL) { |
| 333 | g.doZoomY_(Math.min(context.dragStartY, context.dragEndY), |
| 334 | Math.max(context.dragStartY, context.dragEndY)); |
| 335 | } else { |
| 336 | g.clearZoomRect_(); |
| 337 | } |
| 338 | context.dragStartX = null; |
| 339 | context.dragStartY = null; |
| 340 | }; |
| 341 | |
| 342 | /** |
| 343 | * Default interation model for dygraphs. You can refer to specific elements of |
| 344 | * this when constructing your own interaction model, e.g.: |
| 345 | * g.updateOptions( { |
| 346 | * interactionModel: { |
| 347 | * mousedown: Dygraph.defaultInteractionModel.mousedown |
| 348 | * } |
| 349 | * } ); |
| 350 | */ |
| 351 | Dygraph.Interaction.defaultModel = { |
| 352 | // Track the beginning of drag events |
| 353 | mousedown: function(event, g, context) { |
| 354 | context.initializeMouseDown(event, g, context); |
| 355 | |
| 356 | if (event.altKey || event.shiftKey) { |
| 357 | Dygraph.startPan(event, g, context); |
| 358 | } else { |
| 359 | Dygraph.startZoom(event, g, context); |
| 360 | } |
| 361 | }, |
| 362 | |
| 363 | // Draw zoom rectangles when the mouse is down and the user moves around |
| 364 | mousemove: function(event, g, context) { |
| 365 | if (context.isZooming) { |
| 366 | Dygraph.moveZoom(event, g, context); |
| 367 | } else if (context.isPanning) { |
| 368 | Dygraph.movePan(event, g, context); |
| 369 | } |
| 370 | }, |
| 371 | |
| 372 | mouseup: function(event, g, context) { |
| 373 | if (context.isZooming) { |
| 374 | Dygraph.endZoom(event, g, context); |
| 375 | } else if (context.isPanning) { |
| 376 | Dygraph.endPan(event, g, context); |
| 377 | } |
| 378 | }, |
| 379 | |
| 380 | // Temporarily cancel the dragging event when the mouse leaves the graph |
| 381 | mouseout: function(event, g, context) { |
| 382 | if (context.isZooming) { |
| 383 | context.dragEndX = null; |
| 384 | context.dragEndY = null; |
| 385 | } |
| 386 | }, |
| 387 | |
| 388 | // Disable zooming out if panning. |
| 389 | dblclick: function(event, g, context) { |
| 390 | if (event.altKey || event.shiftKey) { |
| 391 | return; |
| 392 | } |
| 393 | // TODO(konigsberg): replace g.doUnzoom()_ with something that is |
| 394 | // friendlier to public use. |
| 395 | g.doUnzoom_(); |
| 396 | } |
| 397 | }; |
| 398 | |
| 399 | Dygraph.DEFAULT_ATTRS.interactionModel = Dygraph.Interaction.defaultModel; |
| 400 | |
| 401 | // old ways of accessing these methods/properties |
| 402 | Dygraph.defaultInteractionModel = Dygraph.Interaction.defaultModel; |
| 403 | Dygraph.endZoom = Dygraph.Interaction.endZoom; |
| 404 | Dygraph.moveZoom = Dygraph.Interaction.moveZoom; |
| 405 | Dygraph.startZoom = Dygraph.Interaction.startZoom; |
| 406 | Dygraph.endPan = Dygraph.Interaction.endPan; |
| 407 | Dygraph.movePan = Dygraph.Interaction.movePan; |
| 408 | Dygraph.startPan = Dygraph.Interaction.startPan; |
| 409 | |
| 410 | Dygraph.Interaction.nonInteractiveModel_ = { |
| 411 | mousedown: function(event, g, context) { |
| 412 | context.initializeMouseDown(event, g, context); |
| 413 | }, |
| 414 | mouseup: function(event, g, context) { |
| 415 | // TODO(danvk): this logic is repeated in Dygraph.Interaction.endZoom |
| 416 | context.dragEndX = g.dragGetX_(event, context); |
| 417 | context.dragEndY = g.dragGetY_(event, context); |
| 418 | var regionWidth = Math.abs(context.dragEndX - context.dragStartX); |
| 419 | var regionHeight = Math.abs(context.dragEndY - context.dragStartY); |
| 420 | |
| 421 | if (regionWidth < 2 && regionHeight < 2 && |
| 422 | g.lastx_ !== undefined && g.lastx_ != -1) { |
| 423 | Dygraph.Interaction.treatMouseOpAsClick(g, event, context); |
| 424 | } |
| 425 | } |
| 426 | }; |
| 427 | |
| 428 | // Default interaction model when using the range selector. |
| 429 | Dygraph.Interaction.dragIsPanInteractionModel = { |
| 430 | mousedown: function(event, g, context) { |
| 431 | context.initializeMouseDown(event, g, context); |
| 432 | Dygraph.startPan(event, g, context); |
| 433 | }, |
| 434 | mousemove: function(event, g, context) { |
| 435 | if (context.isPanning) { |
| 436 | Dygraph.movePan(event, g, context); |
| 437 | } |
| 438 | }, |
| 439 | mouseup: function(event, g, context) { |
| 440 | if (context.isPanning) { |
| 441 | Dygraph.endPan(event, g, context); |
| 442 | } |
| 443 | } |
| 444 | }; |