3 * Copyright 2011 Robert Konigsberg (konigsberg@google.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
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)
13 /*jshint globalstrict: true */
14 /*global Dygraph:false */
18 * A collection of functions to facilitate build custom interaction models.
21 Dygraph
.Interaction
= {};
24 * Called in response to an interaction model operation that
25 * should start the default panning behavior.
27 * It's used in the default callback for "mousedown" operations.
28 * Custom interaction model builders can use it to provide the default
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
37 Dygraph
.Interaction
.startPan
= function(event
, g
, context
) {
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);
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!
49 var boundedLeftX
= g
.toDomXCoord(xExtremes
[0]) - maxXPixelsToDraw
;
50 var boundedRightX
= g
.toDomXCoord(xExtremes
[1]) + maxXPixelsToDraw
;
52 var boundedLeftDate
= g
.toDataXCoord(boundedLeftX
);
53 var boundedRightDate
= g
.toDataXCoord(boundedRightX
);
54 context
.boundedDates
= [boundedLeftDate
, boundedRightDate
];
56 var boundedValues
= [];
57 var maxYPixelsToDraw
= g
.height_
* g
.attr_("panEdgeFraction");
59 for (i
= 0; i
< g
.axes_
.length
; i
++) {
61 var yExtremes
= axis
.extremeRange
;
63 var boundedTopY
= g
.toDomYCoord(yExtremes
[0], i
) + maxYPixelsToDraw
;
64 var boundedBottomY
= g
.toDomYCoord(yExtremes
[1], i
) - maxYPixelsToDraw
;
66 var boundedTopValue
= g
.toDataYCoord(boundedTopY
);
67 var boundedBottomValue
= g
.toDataYCoord(boundedBottomY
);
69 boundedValues
[i
] = [boundedTopValue
, boundedBottomValue
];
71 context
.boundedValues
= boundedValues
;
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
79 context
.is2DPan
= false;
81 for (i
= 0; i
< g
.axes_
.length
; i
++) {
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.
88 axis_data
.initialTopValue
= Dygraph
.log10(yRange
[1]);
89 axis_data
.dragValueRange
= Dygraph
.log10(yRange
[1]) - Dygraph
.log10(yRange
[0]);
91 axis_data
.initialTopValue
= yRange
[1];
92 axis_data
.dragValueRange
= yRange
[1] - yRange
[0];
94 axis_data
.unitsPerPixel
= axis_data
.dragValueRange
/ (g
.plotter_
.area
.h
- 1);
95 context
.axes
.push(axis_data
);
97 // While calculating axes, set 2dpan.
98 if (axis
.valueWindow
|| axis
.valueRange
) context
.is2DPan
= true;
103 * Called in response to an interaction model operation that
104 * responds to an event that pans the view.
106 * It's used in the default callback for "mousemove" operations.
107 * Custom interaction model builders can use it to provide the default
110 * @param {Event} event the event object which led to the movePan call.
111 * @param {Dygraph} g The dygraph on which to act.
112 * @param {Object} context The dragging context object (with
113 * dragStartX/dragStartY/etc. properties). This function modifies the
116 Dygraph
.Interaction
.movePan
= function(event
, g
, context
) {
117 context
.dragEndX
= g
.dragGetX_(event
, context
);
118 context
.dragEndY
= g
.dragGetY_(event
, context
);
120 var minDate
= context
.initialLeftmostDate
-
121 (context
.dragEndX
- context
.dragStartX
) * context
.xUnitsPerPixel
;
122 if (context
.boundedDates
) {
123 minDate
= Math
.max(minDate
, context
.boundedDates
[0]);
125 var maxDate
= minDate
+ context
.dateRange
;
126 if (context
.boundedDates
) {
127 if (maxDate
> context
.boundedDates
[1]) {
128 // Adjust minDate, and recompute maxDate.
129 minDate
= minDate
- (maxDate
- context
.boundedDates
[1]);
130 maxDate
= minDate
+ context
.dateRange
;
134 g
.dateWindow_
= [minDate
, maxDate
];
136 // y-axis scaling is automatic unless this is a full 2D pan.
137 if (context
.is2DPan
) {
138 // Adjust each axis appropriately.
139 for (var i
= 0; i
< g
.axes_
.length
; i
++) {
140 var axis
= g
.axes_
[i
];
141 var axis_data
= context
.axes
[i
];
143 var pixelsDragged
= context
.dragEndY
- context
.dragStartY
;
144 var unitsDragged
= pixelsDragged
* axis_data
.unitsPerPixel
;
146 var boundedValue
= context
.boundedValues
? context
.boundedValues
[i
] : null;
148 // In log scale, maxValue and minValue are the logs of those values.
149 var maxValue
= axis_data
.initialTopValue
+ unitsDragged
;
151 maxValue
= Math
.min(maxValue
, boundedValue
[1]);
153 var minValue
= maxValue
- axis_data
.dragValueRange
;
155 if (minValue
< boundedValue
[0]) {
156 // Adjust maxValue, and recompute minValue.
157 maxValue
= maxValue
- (minValue
- boundedValue
[0]);
158 minValue
= maxValue
- axis_data
.dragValueRange
;
162 axis
.valueWindow
= [ Math
.pow(Dygraph
.LOG_SCALE
, minValue
),
163 Math
.pow(Dygraph
.LOG_SCALE
, maxValue
) ];
165 axis
.valueWindow
= [ minValue
, maxValue
];
174 * Called in response to an interaction model operation that
175 * responds to an event that ends panning.
177 * It's used in the default callback for "mouseup" operations.
178 * Custom interaction model builders can use it to provide the default
181 * @param {Event} event the event object which led to the endPan call.
182 * @param {Dygraph} g The dygraph on which to act.
183 * @param {Object} context The dragging context object (with
184 * dragStartX/dragStartY/etc. properties). This function modifies the
187 Dygraph
.Interaction
.endPan
= function(event
, g
, context
) {
188 context
.dragEndX
= g
.dragGetX_(event
, context
);
189 context
.dragEndY
= g
.dragGetY_(event
, context
);
191 var regionWidth
= Math
.abs(context
.dragEndX
- context
.dragStartX
);
192 var regionHeight
= Math
.abs(context
.dragEndY
- context
.dragStartY
);
194 if (regionWidth
< 2 && regionHeight
< 2 &&
195 g
.lastx_
!== undefined
&& g
.lastx_
!= -1) {
196 Dygraph
.Interaction
.treatMouseOpAsClick(g
, event
, context
);
199 // TODO(konigsberg): mouseup should just delete the
200 // context object, and mousedown should create a new one.
201 context
.isPanning
= false;
202 context
.is2DPan
= false;
203 context
.initialLeftmostDate
= null;
204 context
.dateRange
= null;
205 context
.valueRange
= null;
206 context
.boundedDates
= null;
207 context
.boundedValues
= null;
212 * Called in response to an interaction model operation that
213 * responds to an event that starts zooming.
215 * It's used in the default callback for "mousedown" operations.
216 * Custom interaction model builders can use it to provide the default
219 * @param {Event} event the event object which led to the startZoom call.
220 * @param {Dygraph} g The dygraph on which to act.
221 * @param {Object} context The dragging context object (with
222 * dragStartX/dragStartY/etc. properties). This function modifies the
225 Dygraph
.Interaction
.startZoom
= function(event
, g
, context
) {
226 context
.isZooming
= true;
227 context
.zoomMoved
= false;
231 * Called in response to an interaction model operation that
232 * responds to an event that defines zoom boundaries.
234 * It's used in the default callback for "mousemove" operations.
235 * Custom interaction model builders can use it to provide the default
238 * @param {Event} event the event object which led to the moveZoom call.
239 * @param {Dygraph} g The dygraph on which to act.
240 * @param {Object} context The dragging context object (with
241 * dragStartX/dragStartY/etc. properties). This function modifies the
244 Dygraph
.Interaction
.moveZoom
= function(event
, g
, context
) {
245 context
.zoomMoved
= true;
246 context
.dragEndX
= g
.dragGetX_(event
, context
);
247 context
.dragEndY
= g
.dragGetY_(event
, context
);
249 var xDelta
= Math
.abs(context
.dragStartX
- context
.dragEndX
);
250 var yDelta
= Math
.abs(context
.dragStartY
- context
.dragEndY
);
252 // drag direction threshold for y axis is twice as large as x axis
253 context
.dragDirection
= (xDelta
< yDelta
/ 2) ? Dygraph
.VERTICAL
: Dygraph
.HORIZONTAL
;
256 context
.dragDirection
,
261 context
.prevDragDirection
,
265 context
.prevEndX
= context
.dragEndX
;
266 context
.prevEndY
= context
.dragEndY
;
267 context
.prevDragDirection
= context
.dragDirection
;
272 * @param {Event} event
273 * @param {Object} context
275 Dygraph
.Interaction
.treatMouseOpAsClick
= function(g
, event
, context
) {
276 var clickCallback
= g
.attr_('clickCallback');
277 var pointClickCallback
= g
.attr_('pointClickCallback');
279 var selectedPoint
= null;
281 // Find out if the click occurs on a point. This only matters if there's a
282 // pointClickCallback.
283 if (pointClickCallback
) {
285 var closestDistance
= Number
.MAX_VALUE
;
287 // check if the click was on a particular point.
288 for (var i
= 0; i
< g
.selPoints_
.length
; i
++) {
289 var p
= g
.selPoints_
[i
];
290 var distance
= Math
.pow(p
.canvasx
- context
.dragEndX
, 2) +
291 Math
.pow(p
.canvasy
- context
.dragEndY
, 2);
292 if (!isNaN(distance
) &&
293 (closestIdx
== -1 || distance
< closestDistance
)) {
294 closestDistance
= distance
;
299 // Allow any click within two pixels of the dot.
300 var radius
= g
.attr_('highlightCircleSize') + 2;
301 if (closestDistance
<= radius
* radius
) {
302 selectedPoint
= g
.selPoints_
[closestIdx
];
307 pointClickCallback(event
, selectedPoint
);
310 // TODO(danvk): pass along more info about the points, e.g. 'x'
312 clickCallback(event
, g
.lastx_
, g
.selPoints_
);
317 * Called in response to an interaction model operation that
318 * responds to an event that performs a zoom based on previously defined
321 * It's used in the default callback for "mouseup" operations.
322 * Custom interaction model builders can use it to provide the default
325 * @param {Event} event the event object which led to the endZoom call.
326 * @param {Dygraph} g The dygraph on which to end the zoom.
327 * @param {Object} context The dragging context object (with
328 * dragStartX/dragStartY/etc. properties). This function modifies the
331 Dygraph
.Interaction
.endZoom
= function(event
, g
, context
) {
332 context
.isZooming
= false;
333 context
.dragEndX
= g
.dragGetX_(event
, context
);
334 context
.dragEndY
= g
.dragGetY_(event
, context
);
335 var regionWidth
= Math
.abs(context
.dragEndX
- context
.dragStartX
);
336 var regionHeight
= Math
.abs(context
.dragEndY
- context
.dragStartY
);
338 if (regionWidth
< 2 && regionHeight
< 2 &&
339 g
.lastx_
!== undefined
&& g
.lastx_
!= -1) {
340 Dygraph
.Interaction
.treatMouseOpAsClick(g
, event
, context
);
343 if (regionWidth
>= 10 && context
.dragDirection
== Dygraph
.HORIZONTAL
) {
344 g
.doZoomX_(Math
.min(context
.dragStartX
, context
.dragEndX
),
345 Math
.max(context
.dragStartX
, context
.dragEndX
));
346 context
.cancelNextDblclick
= true;
347 } else if (regionHeight
>= 10 && context
.dragDirection
== Dygraph
.VERTICAL
) {
348 g
.doZoomY_(Math
.min(context
.dragStartY
, context
.dragEndY
),
349 Math
.max(context
.dragStartY
, context
.dragEndY
));
350 context
.cancelNextDblclick
= true;
352 if (context
.zoomMoved
) g
.clearZoomRect_();
354 context
.dragStartX
= null;
355 context
.dragStartY
= null;
361 Dygraph
.Interaction
.startTouch
= function(event
, g
, context
) {
362 event
.preventDefault(); // touch browsers are all nice.
364 for (var i
= 0; i
< event
.touches
.length
; i
++) {
365 var t
= event
.touches
[i
];
366 // we dispense with 'dragGetX_' because all touchBrowsers support pageX
370 dataX
: g
.toDataXCoord(t
.pageX
),
371 dataY
: g
.toDataYCoord(t
.pageY
)
372 // identifier: t.identifier
375 context
.initialTouches
= touches
;
377 if (touches
.length
== 1) {
378 // This is just a swipe.
379 context
.initialPinchCenter
= touches
[0];
380 context
.touchDirections
= { x
: true, y
: true };
381 } else if (touches
.length
== 2) {
382 // It's become a pinch!
384 // only screen coordinates can be averaged (data coords could be log scale).
385 context
.initialPinchCenter
= {
386 pageX
: 0.5 * (touches
[0].pageX
+ touches
[1].pageX
),
387 pageY
: 0.5 * (touches
[0].pageY
+ touches
[1].pageY
),
389 // TODO(danvk): remove
390 dataX
: 0.5 * (touches
[0].dataX
+ touches
[1].dataX
),
391 dataY
: 0.5 * (touches
[0].dataY
+ touches
[1].dataY
)
394 // Make pinches in a 45-degree swath around either axis 1-dimensional zooms.
395 var initialAngle
= 180 / Math
.PI
* Math
.atan2(
396 context
.initialPinchCenter
.pageY
- touches
[0].pageY
,
397 touches
[0].pageX
- context
.initialPinchCenter
.pageX
);
399 // use symmetry to get it into the first quadrant.
400 initialAngle
= Math
.abs(initialAngle
);
401 if (initialAngle
> 90) initialAngle
= 90 - initialAngle
;
403 context
.touchDirections
= {
404 x
: (initialAngle
< (90 - 45/2)),
405 y
: (initialAngle
> 45/2)
409 // save the full x & y ranges.
410 context
.initialRange
= {
419 Dygraph
.Interaction
.moveTouch
= function(event
, g
, context
) {
421 for (i
= 0; i
< event
.touches
.length
; i
++) {
422 var t
= event
.touches
[i
];
428 var initialTouches
= context
.initialTouches
;
432 // old and new centers.
433 var c_init
= context
.initialPinchCenter
;
434 if (touches
.length
== 1) {
438 pageX
: 0.5 * (touches
[0].pageX
+ touches
[1].pageX
),
439 pageY
: 0.5 * (touches
[0].pageY
+ touches
[1].pageY
)
443 // this is the "swipe" component
444 // we toss it out for now, but could use it in the future.
446 pageX
: c_now
.pageX
- c_init
.pageX
,
447 pageY
: c_now
.pageY
- c_init
.pageY
449 var dataWidth
= context
.initialRange
.x
[1] - context
.initialRange
.x
[0];
450 var dataHeight
= context
.initialRange
.y
[0] - context
.initialRange
.y
[1];
451 swipe
.dataX
= (swipe
.pageX
/ g
.plotter_
.area
.w
) * dataWidth
;
452 swipe
.dataY
= (swipe
.pageY
/ g
.plotter_
.area
.h
) * dataHeight
;
455 // The residual bits are usually split into scale & rotate bits, but we split
456 // them into x-scale and y-scale bits.
457 if (touches
.length
== 1) {
460 } else if (touches
.length
== 2) {
461 var initHalfWidth
= (initialTouches
[1].pageX
- c_init
.pageX
);
462 xScale
= (touches
[1].pageX
- c_now
.pageX
) / initHalfWidth
;
464 var initHalfHeight
= (initialTouches
[1].pageY
- c_init
.pageY
);
465 yScale
= (touches
[1].pageY
- c_now
.pageY
) / initHalfHeight
;
468 // Clip scaling to [1/8, 8] to prevent too much blowup
.
469 xScale
= Math
.min(8, Math
.max(0.125, xScale
));
470 yScale
= Math
.min(8, Math
.max(0.125, yScale
));
472 if (context
.touchDirections
.x
) {
474 c_init
.dataX
- swipe
.dataX
+ (context
.initialRange
.x
[0] - c_init
.dataX
) / xScale
,
475 c_init
.dataX
- swipe
.dataX
+ (context
.initialRange
.x
[1] - c_init
.dataX
) / xScale
479 if (context
.touchDirections
.y
) {
480 for (i
= 0; i
< 1 /*g.axes_.length*/; i
++) {
481 var axis
= g
.axes_
[i
];
483 // TODO(danvk): implement
486 c_init
.dataY
- swipe
.dataY
+ (context
.initialRange
.y
[0] - c_init
.dataY
) / yScale
,
487 c_init
.dataY
- swipe
.dataY
+ (context
.initialRange
.y
[1] - c_init
.dataY
) / yScale
499 Dygraph
.Interaction
.endTouch
= function(event
, g
, context
) {
500 if (event
.touches
.length
!== 0) {
501 // this is effectively a "reset"
502 Dygraph
.Interaction
.startTouch(event
, g
, context
);
507 * Default interation model for dygraphs. You can refer to specific elements of
508 * this when constructing your own interaction model, e.g.:
510 * interactionModel: {
511 * mousedown: Dygraph.defaultInteractionModel.mousedown
515 Dygraph
.Interaction
.defaultModel
= {
516 // Track the beginning of drag events
517 mousedown
: function(event
, g
, context
) {
518 // Right-click should not initiate a zoom.
519 if (event
.button
&& event
.button
== 2) return;
521 context
.initializeMouseDown(event
, g
, context
);
523 if (event
.altKey
|| event
.shiftKey
) {
524 Dygraph
.startPan(event
, g
, context
);
526 Dygraph
.startZoom(event
, g
, context
);
530 // Draw zoom rectangles when the mouse is down and the user moves around
531 mousemove
: function(event
, g
, context
) {
532 if (context
.isZooming
) {
533 Dygraph
.moveZoom(event
, g
, context
);
534 } else if (context
.isPanning
) {
535 Dygraph
.movePan(event
, g
, context
);
539 mouseup
: function(event
, g
, context
) {
540 if (context
.isZooming
) {
541 Dygraph
.endZoom(event
, g
, context
);
542 } else if (context
.isPanning
) {
543 Dygraph
.endPan(event
, g
, context
);
547 touchstart
: function(event
, g
, context
) {
548 Dygraph
.Interaction
.startTouch(event
, g
, context
);
550 touchmove
: function(event
, g
, context
) {
551 Dygraph
.Interaction
.moveTouch(event
, g
, context
);
553 touchend
: function(event
, g
, context
) {
554 Dygraph
.Interaction
.endTouch(event
, g
, context
);
557 // Temporarily cancel the dragging event when the mouse leaves the graph
558 mouseout
: function(event
, g
, context
) {
559 if (context
.isZooming
) {
560 context
.dragEndX
= null;
561 context
.dragEndY
= null;
565 // Disable zooming out if panning.
566 dblclick
: function(event
, g
, context
) {
567 if (context
.cancelNextDblclick
) {
568 context
.cancelNextDblclick
= false;
571 if (event
.altKey
|| event
.shiftKey
) {
574 // TODO(konigsberg): replace g.doUnzoom()_ with something that is
575 // friendlier to public use.
580 Dygraph
.DEFAULT_ATTRS
.interactionModel
= Dygraph
.Interaction
.defaultModel
;
582 // old ways of accessing these methods/properties
583 Dygraph
.defaultInteractionModel
= Dygraph
.Interaction
.defaultModel
;
584 Dygraph
.endZoom
= Dygraph
.Interaction
.endZoom
;
585 Dygraph
.moveZoom
= Dygraph
.Interaction
.moveZoom
;
586 Dygraph
.startZoom
= Dygraph
.Interaction
.startZoom
;
587 Dygraph
.endPan
= Dygraph
.Interaction
.endPan
;
588 Dygraph
.movePan
= Dygraph
.Interaction
.movePan
;
589 Dygraph
.startPan
= Dygraph
.Interaction
.startPan
;
591 Dygraph
.Interaction
.nonInteractiveModel_
= {
592 mousedown
: function(event
, g
, context
) {
593 context
.initializeMouseDown(event
, g
, context
);
595 mouseup
: function(event
, g
, context
) {
596 // TODO(danvk): this logic is repeated in Dygraph.Interaction.endZoom
597 context
.dragEndX
= g
.dragGetX_(event
, context
);
598 context
.dragEndY
= g
.dragGetY_(event
, context
);
599 var regionWidth
= Math
.abs(context
.dragEndX
- context
.dragStartX
);
600 var regionHeight
= Math
.abs(context
.dragEndY
- context
.dragStartY
);
602 if (regionWidth
< 2 && regionHeight
< 2 &&
603 g
.lastx_
!== undefined
&& g
.lastx_
!= -1) {
604 Dygraph
.Interaction
.treatMouseOpAsClick(g
, event
, context
);
609 // Default interaction model when using the range selector.
610 Dygraph
.Interaction
.dragIsPanInteractionModel
= {
611 mousedown
: function(event
, g
, context
) {
612 context
.initializeMouseDown(event
, g
, context
);
613 Dygraph
.startPan(event
, g
, context
);
615 mousemove
: function(event
, g
, context
) {
616 if (context
.isPanning
) {
617 Dygraph
.movePan(event
, g
, context
);
620 mouseup
: function(event
, g
, context
) {
621 if (context
.isPanning
) {
622 Dygraph
.endPan(event
, g
, context
);