3 * Copyright 2011 Paul Felix (paul.eric.felix@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
6 /*global Dygraph:false,TouchEvent:false */
9 * @fileoverview This file contains the RangeSelector plugin used to provide
10 * a timeline range selector widget for dygraphs.
13 /*global Dygraph:false */
16 import * as utils from
'../dygraph-utils';
17 import DygraphInteraction from
'../dygraph-interaction-model';
19 var rangeSelector
= function() {
20 this.hasTouchInterface_
= typeof(TouchEvent
) != 'undefined';
21 this.isMobileDevice_
= /mobile|android/gi.test(navigator
.appVersion
);
22 this.interfaceCreated_
= false;
25 rangeSelector
.prototype.toString
= function() {
26 return "RangeSelector Plugin";
29 rangeSelector
.prototype.activate
= function(dygraph
) {
30 this.dygraph_
= dygraph
;
31 if (this.getOption_('showRangeSelector')) {
32 this.createInterface_();
35 layout
: this.reserveSpace_
,
36 predraw
: this.renderStaticLayer_
,
37 didDrawChart
: this.renderInteractiveLayer_
41 rangeSelector
.prototype.destroy
= function() {
42 this.bgcanvas_
= null;
43 this.fgcanvas_
= null;
44 this.leftZoomHandle_
= null;
45 this.rightZoomHandle_
= null;
48 //------------------------------------------------------------------
50 //------------------------------------------------------------------
52 rangeSelector
.prototype.getOption_
= function(name
, opt_series
) {
53 return this.dygraph_
.getOption(name
, opt_series
);
56 rangeSelector
.prototype.setDefaultOption_
= function(name
, value
) {
57 this.dygraph_
.attrs_
[name
] = value
;
62 * Creates the range selector elements and adds them to the graph.
64 rangeSelector
.prototype.createInterface_
= function() {
65 this.createCanvases_();
66 this.createZoomHandles_();
67 this.initInteraction_();
69 // Range selector and animatedZooms have a bad interaction. See issue 359.
70 if (this.getOption_('animatedZooms')) {
71 console
.warn('Animated zooms and range selector are not compatible; disabling animatedZooms.');
72 this.dygraph_
.updateOptions({animatedZooms
: false}, true);
75 this.interfaceCreated_
= true;
81 * Adds the range selector to the graph.
83 rangeSelector
.prototype.addToGraph_
= function() {
84 var graphDiv
= this.graphDiv_
= this.dygraph_
.graphDiv
;
85 graphDiv
.appendChild(this.bgcanvas_
);
86 graphDiv
.appendChild(this.fgcanvas_
);
87 graphDiv
.appendChild(this.leftZoomHandle_
);
88 graphDiv
.appendChild(this.rightZoomHandle_
);
93 * Removes the range selector from the graph.
95 rangeSelector
.prototype.removeFromGraph_
= function() {
96 var graphDiv
= this.graphDiv_
;
97 graphDiv
.removeChild(this.bgcanvas_
);
98 graphDiv
.removeChild(this.fgcanvas_
);
99 graphDiv
.removeChild(this.leftZoomHandle_
);
100 graphDiv
.removeChild(this.rightZoomHandle_
);
101 this.graphDiv_
= null;
106 * Called by Layout to allow range selector to reserve its space.
108 rangeSelector
.prototype.reserveSpace_
= function(e
) {
109 if (this.getOption_('showRangeSelector')) {
110 e
.reserveSpaceBottom(this.getOption_('rangeSelectorHeight') + 4);
116 * Renders the static portion of the range selector at the predraw stage.
118 rangeSelector
.prototype.renderStaticLayer_
= function() {
119 if (!this.updateVisibility_()) {
123 this.drawStaticLayer_();
128 * Renders the interactive portion of the range selector after the chart has been drawn.
130 rangeSelector
.prototype.renderInteractiveLayer_
= function() {
131 if (!this.updateVisibility_() || this.isChangingRange_
) {
134 this.placeZoomHandles_();
135 this.drawInteractiveLayer_();
140 * Check to see if the range selector is enabled/disabled and update visibility accordingly.
142 rangeSelector
.prototype.updateVisibility_
= function() {
143 var enabled
= this.getOption_('showRangeSelector');
145 if (!this.interfaceCreated_
) {
146 this.createInterface_();
147 } else if (!this.graphDiv_
|| !this.graphDiv_
.parentNode
) {
150 } else if (this.graphDiv_
) {
151 this.removeFromGraph_();
152 var dygraph
= this.dygraph_
;
153 setTimeout(function() { dygraph
.width_
= 0; dygraph
.resize(); }, 1);
160 * Resizes the range selector.
162 rangeSelector
.prototype.resize_
= function() {
163 function setElementRect(canvas
, context
, rect
) {
164 var canvasScale
= utils
.getContextPixelRatio(context
);
166 canvas
.style
.top
= rect
.y
+ 'px';
167 canvas
.style
.left
= rect
.x
+ 'px';
168 canvas
.width
= rect
.w
* canvasScale
;
169 canvas
.height
= rect
.h
* canvasScale
;
170 canvas
.style
.width
= rect
.w
+ 'px';
171 canvas
.style
.height
= rect
.h
+ 'px';
173 if(canvasScale
!= 1) {
174 context
.scale(canvasScale
, canvasScale
);
178 var plotArea
= this.dygraph_
.layout_
.getPlotArea();
180 var xAxisLabelHeight
= 0;
181 if (this.dygraph_
.getOptionForAxis('drawAxis', 'x')) {
182 xAxisLabelHeight
= this.getOption_('xAxisHeight') || (this.getOption_('axisLabelFontSize') + 2 * this.getOption_('axisTickSize'));
186 y
: plotArea
.y
+ plotArea
.h
+ xAxisLabelHeight
+ 4,
188 h
: this.getOption_('rangeSelectorHeight')
191 setElementRect(this.bgcanvas_
, this.bgcanvas_ctx_
, this.canvasRect_
);
192 setElementRect(this.fgcanvas_
, this.fgcanvas_ctx_
, this.canvasRect_
);
197 * Creates the background and foreground canvases.
199 rangeSelector
.prototype.createCanvases_
= function() {
200 this.bgcanvas_
= utils
.createCanvas();
201 this.bgcanvas_
.className
= 'dygraph-rangesel-bgcanvas';
202 this.bgcanvas_
.style
.position
= 'absolute';
203 this.bgcanvas_
.style
.zIndex
= 9;
204 this.bgcanvas_ctx_
= utils
.getContext(this.bgcanvas_
);
206 this.fgcanvas_
= utils
.createCanvas();
207 this.fgcanvas_
.className
= 'dygraph-rangesel-fgcanvas';
208 this.fgcanvas_
.style
.position
= 'absolute';
209 this.fgcanvas_
.style
.zIndex
= 9;
210 this.fgcanvas_
.style
.cursor
= 'default';
211 this.fgcanvas_ctx_
= utils
.getContext(this.fgcanvas_
);
216 * Creates the zoom handle elements.
218 rangeSelector
.prototype.createZoomHandles_
= function() {
219 var img
= new Image();
220 img
.className
= 'dygraph-rangesel-zoomhandle';
221 img
.style
.position
= 'absolute';
222 img
.style
.zIndex
= 10;
223 img
.style
.visibility
= 'hidden'; // Initially hidden so they don't show up in the wrong place.
224 img
.style
.cursor
= 'col-resize';
225 // TODO: change image to more options
228 img
.src
= 'data:image/png;base64,' +
229 'iVBORw0KGgoAAAANSUhEUgAAAAkAAAAQCAYAAADESFVDAAAAAXNSR0IArs4c6QAAAAZiS0dEANAA' +
230 'zwDP4Z7KegAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9sHGw0cMqdt1UwAAAAZdEVYdENv' +
231 'bW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAAaElEQVQoz+3SsRFAQBCF4Z9WJM8KCDVwownl' +
232 '6YXsTmCUsyKGkZzcl7zkz3YLkypgAnreFmDEpHkIwVOMfpdi9CEEN2nGpFdwD03yEqDtOgCaun7s' +
233 'qSTDH32I1pQA2Pb9sZecAxc5r3IAb21d6878xsAAAAAASUVORK5CYII=';
235 if (this.isMobileDevice_
) {
240 this.leftZoomHandle_
= img
;
241 this.rightZoomHandle_
= img
.cloneNode(false);
246 * Sets up the interaction for the range selector.
248 rangeSelector
.prototype.initInteraction_
= function() {
250 var topElem
= document
;
253 var isZooming
= false;
254 var isPanning
= false;
255 var dynamic
= !this.isMobileDevice_
;
257 // We cover iframes during mouse interactions. See comments in
258 // dygraph-utils.js for more info on why this is a good idea.
259 var tarp
= new utils
.IFrameTarp();
261 // functions, defined below. Defining them this way (rather than with
262 // "function foo() {...}" makes JSHint happy.
263 var toXDataWindow
, onZoomStart
, onZoom
, onZoomEnd
, doZoom
, isMouseInPanZone
,
264 onPanStart
, onPan
, onPanEnd
, doPan
, onCanvasHover
;
266 // Touch event functions
267 var onZoomHandleTouchEvent
, onCanvasTouchEvent
, addTouchEvents
;
269 toXDataWindow
= function(zoomHandleStatus
) {
270 var xDataLimits
= self
.dygraph_
.xAxisExtremes();
271 var fact
= (xDataLimits
[1] - xDataLimits
[0])/self
.canvasRect_
.w
;
272 var xDataMin
= xDataLimits
[0] + (zoomHandleStatus
.leftHandlePos
- self
.canvasRect_
.x
)*fact
;
273 var xDataMax
= xDataLimits
[0] + (zoomHandleStatus
.rightHandlePos
- self
.canvasRect_
.x
)*fact
;
274 return [xDataMin
, xDataMax
];
277 onZoomStart
= function(e
) {
278 utils
.cancelEvent(e
);
280 clientXLast
= e
.clientX
;
281 handle
= e
.target
? e
.target
: e
.srcElement
;
282 if (e
.type
=== 'mousedown' || e
.type
=== 'dragstart') {
283 // These events are removed manually.
284 utils
.addEvent(topElem
, 'mousemove', onZoom
);
285 utils
.addEvent(topElem
, 'mouseup', onZoomEnd
);
287 self
.fgcanvas_
.style
.cursor
= 'col-resize';
292 onZoom
= function(e
) {
296 utils
.cancelEvent(e
);
298 var delX
= e
.clientX
- clientXLast
;
299 if (Math
.abs(delX
) < 4) {
302 clientXLast
= e
.clientX
;
305 var zoomHandleStatus
= self
.getZoomHandleStatus_();
307 if (handle
== self
.leftZoomHandle_
) {
308 newPos
= zoomHandleStatus
.leftHandlePos
+ delX
;
309 newPos
= Math
.min(newPos
, zoomHandleStatus
.rightHandlePos
- handle
.width
- 3);
310 newPos
= Math
.max(newPos
, self
.canvasRect_
.x
);
312 newPos
= zoomHandleStatus
.rightHandlePos
+ delX
;
313 newPos
= Math
.min(newPos
, self
.canvasRect_
.x
+ self
.canvasRect_
.w
);
314 newPos
= Math
.max(newPos
, zoomHandleStatus
.leftHandlePos
+ handle
.width
+ 3);
316 var halfHandleWidth
= handle
.width
/2;
317 handle
.style
.left
= (newPos
- halfHandleWidth
) + 'px';
318 self
.drawInteractiveLayer_();
327 onZoomEnd
= function(e
) {
333 utils
.removeEvent(topElem
, 'mousemove', onZoom
);
334 utils
.removeEvent(topElem
, 'mouseup', onZoomEnd
);
335 self
.fgcanvas_
.style
.cursor
= 'default';
337 // If on a slower device, zoom now.
344 doZoom
= function() {
346 var zoomHandleStatus
= self
.getZoomHandleStatus_();
347 self
.isChangingRange_
= true;
348 if (!zoomHandleStatus
.isZoomed
) {
349 self
.dygraph_
.resetZoom();
351 var xDataWindow
= toXDataWindow(zoomHandleStatus
);
352 self
.dygraph_
.doZoomXDates_(xDataWindow
[0], xDataWindow
[1]);
355 self
.isChangingRange_
= false;
359 isMouseInPanZone
= function(e
) {
360 var rect
= self
.leftZoomHandle_
.getBoundingClientRect();
361 var leftHandleClientX
= rect
.left
+ rect
.width
/2;
362 rect
= self
.rightZoomHandle_
.getBoundingClientRect();
363 var rightHandleClientX
= rect
.left
+ rect
.width
/2;
364 return (e
.clientX
> leftHandleClientX
&& e
.clientX
< rightHandleClientX
);
367 onPanStart
= function(e
) {
368 if (!isPanning
&& isMouseInPanZone(e
) && self
.getZoomHandleStatus_().isZoomed
) {
369 utils
.cancelEvent(e
);
371 clientXLast
= e
.clientX
;
372 if (e
.type
=== 'mousedown') {
373 // These events are removed manually.
374 utils
.addEvent(topElem
, 'mousemove', onPan
);
375 utils
.addEvent(topElem
, 'mouseup', onPanEnd
);
382 onPan
= function(e
) {
386 utils
.cancelEvent(e
);
388 var delX
= e
.clientX
- clientXLast
;
389 if (Math
.abs(delX
) < 4) {
392 clientXLast
= e
.clientX
;
395 var zoomHandleStatus
= self
.getZoomHandleStatus_();
396 var leftHandlePos
= zoomHandleStatus
.leftHandlePos
;
397 var rightHandlePos
= zoomHandleStatus
.rightHandlePos
;
398 var rangeSize
= rightHandlePos
- leftHandlePos
;
399 if (leftHandlePos
+ delX
<= self
.canvasRect_
.x
) {
400 leftHandlePos
= self
.canvasRect_
.x
;
401 rightHandlePos
= leftHandlePos
+ rangeSize
;
402 } else if (rightHandlePos
+ delX
>= self
.canvasRect_
.x
+ self
.canvasRect_
.w
) {
403 rightHandlePos
= self
.canvasRect_
.x
+ self
.canvasRect_
.w
;
404 leftHandlePos
= rightHandlePos
- rangeSize
;
406 leftHandlePos
+= delX
;
407 rightHandlePos
+= delX
;
409 var halfHandleWidth
= self
.leftZoomHandle_
.width
/2;
410 self
.leftZoomHandle_
.style
.left
= (leftHandlePos
- halfHandleWidth
) + 'px';
411 self
.rightZoomHandle_
.style
.left
= (rightHandlePos
- halfHandleWidth
) + 'px';
412 self
.drawInteractiveLayer_();
414 // Do pan on the fly.
421 onPanEnd
= function(e
) {
426 utils
.removeEvent(topElem
, 'mousemove', onPan
);
427 utils
.removeEvent(topElem
, 'mouseup', onPanEnd
);
428 // If on a slower device, do pan now.
437 self
.isChangingRange_
= true;
438 self
.dygraph_
.dateWindow_
= toXDataWindow(self
.getZoomHandleStatus_());
439 self
.dygraph_
.drawGraph_(false);
441 self
.isChangingRange_
= false;
445 onCanvasHover
= function(e
) {
446 if (isZooming
|| isPanning
) {
449 var cursor
= isMouseInPanZone(e
) ? 'move' : 'default';
450 if (cursor
!= self
.fgcanvas_
.style
.cursor
) {
451 self
.fgcanvas_
.style
.cursor
= cursor
;
455 onZoomHandleTouchEvent
= function(e
) {
456 if (e
.type
== 'touchstart' && e
.targetTouches
.length
== 1) {
457 if (onZoomStart(e
.targetTouches
[0])) {
458 utils
.cancelEvent(e
);
460 } else if (e
.type
== 'touchmove' && e
.targetTouches
.length
== 1) {
461 if (onZoom(e
.targetTouches
[0])) {
462 utils
.cancelEvent(e
);
469 onCanvasTouchEvent
= function(e
) {
470 if (e
.type
== 'touchstart' && e
.targetTouches
.length
== 1) {
471 if (onPanStart(e
.targetTouches
[0])) {
472 utils
.cancelEvent(e
);
474 } else if (e
.type
== 'touchmove' && e
.targetTouches
.length
== 1) {
475 if (onPan(e
.targetTouches
[0])) {
476 utils
.cancelEvent(e
);
483 addTouchEvents
= function(elem
, fn
) {
484 var types
= ['touchstart', 'touchend', 'touchmove', 'touchcancel'];
485 for (var i
= 0; i
< types
.length
; i
++) {
486 self
.dygraph_
.addAndTrackEvent(elem
, types
[i
], fn
);
490 this.setDefaultOption_('interactionModel', DygraphInteraction
.dragIsPanInteractionModel
);
491 this.setDefaultOption_('panEdgeFraction', 0.0001);
493 var dragStartEvent
= window
.opera
? 'mousedown' : 'dragstart';
494 this.dygraph_
.addAndTrackEvent(this.leftZoomHandle_
, dragStartEvent
, onZoomStart
);
495 this.dygraph_
.addAndTrackEvent(this.rightZoomHandle_
, dragStartEvent
, onZoomStart
);
497 this.dygraph_
.addAndTrackEvent(this.fgcanvas_
, 'mousedown', onPanStart
);
498 this.dygraph_
.addAndTrackEvent(this.fgcanvas_
, 'mousemove', onCanvasHover
);
501 if (this.hasTouchInterface_
) {
502 addTouchEvents(this.leftZoomHandle_
, onZoomHandleTouchEvent
);
503 addTouchEvents(this.rightZoomHandle_
, onZoomHandleTouchEvent
);
504 addTouchEvents(this.fgcanvas_
, onCanvasTouchEvent
);
510 * Draws the static layer in the background canvas.
512 rangeSelector
.prototype.drawStaticLayer_
= function() {
513 var ctx
= this.bgcanvas_ctx_
;
514 ctx
.clearRect(0, 0, this.canvasRect_
.w
, this.canvasRect_
.h
);
516 this.drawMiniPlot_();
522 this.bgcanvas_ctx_
.lineWidth
= this.getOption_('rangeSelectorBackgroundLineWidth');
523 ctx
.strokeStyle
= this.getOption_('rangeSelectorBackgroundStrokeColor');
525 ctx
.moveTo(margin
, margin
);
526 ctx
.lineTo(margin
, this.canvasRect_
.h
-margin
);
527 ctx
.lineTo(this.canvasRect_
.w
-margin
, this.canvasRect_
.h
-margin
);
528 ctx
.lineTo(this.canvasRect_
.w
-margin
, margin
);
535 * Draws the mini plot in the background canvas.
537 rangeSelector
.prototype.drawMiniPlot_
= function() {
538 var fillStyle
= this.getOption_('rangeSelectorPlotFillColor');
539 var fillGradientStyle
= this.getOption_('rangeSelectorPlotFillGradientColor');
540 var strokeStyle
= this.getOption_('rangeSelectorPlotStrokeColor');
541 if (!fillStyle
&& !strokeStyle
) {
545 var stepPlot
= this.getOption_('stepPlot');
547 var combinedSeriesData
= this.computeCombinedSeriesAndLimits_();
548 var yRange
= combinedSeriesData
.yMax
- combinedSeriesData
.yMin
;
550 // Draw the mini plot.
551 var ctx
= this.bgcanvas_ctx_
;
554 var xExtremes
= this.dygraph_
.xAxisExtremes();
555 var xRange
= Math
.max(xExtremes
[1] - xExtremes
[0], 1.e
-30);
556 var xFact
= (this.canvasRect_
.w
- margin
)/xRange
;
557 var yFact
= (this.canvasRect_
.h
- margin
)/yRange
;
558 var canvasWidth
= this.canvasRect_
.w
- margin
;
559 var canvasHeight
= this.canvasRect_
.h
- margin
;
561 var prevX
= null, prevY
= null;
564 ctx
.moveTo(margin
, canvasHeight
);
565 for (var i
= 0; i
< combinedSeriesData
.data
.length
; i
++) {
566 var dataPoint
= combinedSeriesData
.data
[i
];
567 var x
= ((dataPoint
[0] !== null) ? ((dataPoint
[0] - xExtremes
[0])*xFact
) : NaN
);
568 var y
= ((dataPoint
[1] !== null) ? (canvasHeight
- (dataPoint
[1] - combinedSeriesData
.yMin
)*yFact
) : NaN
);
570 // Skip points that don't change the x-value. Overly fine-grained points
571 // can cause major slowdowns with the ctx.fill() call below.
572 if (!stepPlot
&& prevX
!== null && Math
.round(x
) == Math
.round(prevX
)) {
576 if (isFinite(x
) && isFinite(y
)) {
578 ctx
.lineTo(x
, canvasHeight
);
581 ctx
.lineTo(x
, prevY
);
590 ctx
.lineTo(x
, prevY
);
591 ctx
.lineTo(x
, canvasHeight
);
594 ctx
.lineTo(prevX
, canvasHeight
);
597 prevX
= prevY
= null;
600 ctx
.lineTo(canvasWidth
, canvasHeight
);
604 var lingrad
= this.bgcanvas_ctx_
.createLinearGradient(0, 0, 0, canvasHeight
);
605 if (fillGradientStyle
) {
606 lingrad
.addColorStop(0, fillGradientStyle
);
608 lingrad
.addColorStop(1, fillStyle
);
609 this.bgcanvas_ctx_
.fillStyle
= lingrad
;
614 this.bgcanvas_ctx_
.strokeStyle
= strokeStyle
;
615 this.bgcanvas_ctx_
.lineWidth
= this.getOption_('rangeSelectorPlotLineWidth');
622 * Computes and returns the combined series data along with min/max for the mini plot.
623 * The combined series consists of averaged values for all series.
624 * When series have error bars, the error bars are ignored.
625 * @return {Object} An object containing combined series array, ymin, ymax.
627 rangeSelector
.prototype.computeCombinedSeriesAndLimits_
= function() {
628 var g
= this.dygraph_
;
629 var logscale
= this.getOption_('logscale');
632 // Select series to combine. By default, all series are combined.
633 var numColumns
= g
.numColumns();
634 var labels
= g
.getLabels();
635 var includeSeries
= new Array(numColumns
);
637 for (i
= 1; i
< numColumns
; i
++) {
638 var include
= this.getOption_('showInRangeSelector', labels
[i
]);
639 includeSeries
[i
] = include
;
640 if (include
!== null) anySet
= true; // it's set explicitly for this series
643 for (i
= 0; i
< includeSeries
.length
; i
++) includeSeries
[i
] = true;
646 // Create a combined series (average of selected series values).
647 // TODO(danvk): short-circuit if there's only one series.
648 var rolledSeries
= [];
649 var dataHandler
= g
.dataHandler_
;
650 var options
= g
.attributes_
;
651 for (i
= 1; i
< g
.numColumns(); i
++) {
652 if (!includeSeries
[i
]) continue;
653 var series
= dataHandler
.extractSeries(g
.rawData_
, i
, options
);
654 if (g
.rollPeriod() > 1) {
655 series
= dataHandler
.rollingAverage(series
, g
.rollPeriod(), options
);
658 rolledSeries
.push(series
);
661 var combinedSeries
= [];
662 for (i
= 0; i
< rolledSeries
[0].length
; i
++) {
665 for (var j
= 0; j
< rolledSeries
.length
; j
++) {
666 var y
= rolledSeries
[j
][i
][1];
667 if (y
=== null || isNaN(y
)) continue;
671 combinedSeries
.push([rolledSeries
[0][i
][0], sum
/ count
]);
674 // Compute the y range.
675 var yMin
= Number
.MAX_VALUE
;
676 var yMax
= -Number
.MAX_VALUE
;
677 for (i
= 0; i
< combinedSeries
.length
; i
++) {
678 var yVal
= combinedSeries
[i
][1];
679 if (yVal
!== null && isFinite(yVal
) && (!logscale
|| yVal
> 0)) {
680 yMin
= Math
.min(yMin
, yVal
);
681 yMax
= Math
.max(yMax
, yVal
);
685 // Convert Y data to log scale if needed.
686 // Also, expand the Y range to compress the mini plot a little.
687 var extraPercent
= 0.25;
689 yMax
= utils
.log10(yMax
);
690 yMax
+= yMax
*extraPercent
;
691 yMin
= utils
.log10(yMin
);
692 for (i
= 0; i
< combinedSeries
.length
; i
++) {
693 combinedSeries
[i
][1] = utils
.log10(combinedSeries
[i
][1]);
697 var yRange
= yMax
- yMin
;
698 if (yRange
<= Number
.MIN_VALUE
) {
699 yExtra
= yMax
*extraPercent
;
701 yExtra
= yRange
*extraPercent
;
707 return {data
: combinedSeries
, yMin
: yMin
, yMax
: yMax
};
712 * Places the zoom handles in the proper position based on the current X data window.
714 rangeSelector
.prototype.placeZoomHandles_
= function() {
715 var xExtremes
= this.dygraph_
.xAxisExtremes();
716 var xWindowLimits
= this.dygraph_
.xAxisRange();
717 var xRange
= xExtremes
[1] - xExtremes
[0];
718 var leftPercent
= Math
.max(0, (xWindowLimits
[0] - xExtremes
[0])/xRange
);
719 var rightPercent
= Math
.max(0, (xExtremes
[1] - xWindowLimits
[1])/xRange
);
720 var leftCoord
= this.canvasRect_
.x
+ this.canvasRect_
.w
*leftPercent
;
721 var rightCoord
= this.canvasRect_
.x
+ this.canvasRect_
.w
*(1 - rightPercent
);
722 var handleTop
= Math
.max(this.canvasRect_
.y
, this.canvasRect_
.y
+ (this.canvasRect_
.h
- this.leftZoomHandle_
.height
)/2);
723 var halfHandleWidth
= this.leftZoomHandle_
.width
/2;
724 this.leftZoomHandle_
.style
.left
= (leftCoord
- halfHandleWidth
) + 'px';
725 this.leftZoomHandle_
.style
.top
= handleTop
+ 'px';
726 this.rightZoomHandle_
.style
.left
= (rightCoord
- halfHandleWidth
) + 'px';
727 this.rightZoomHandle_
.style
.top
= this.leftZoomHandle_
.style
.top
;
729 this.leftZoomHandle_
.style
.visibility
= 'visible';
730 this.rightZoomHandle_
.style
.visibility
= 'visible';
735 * Draws the interactive layer in the foreground canvas.
737 rangeSelector
.prototype.drawInteractiveLayer_
= function() {
738 var ctx
= this.fgcanvas_ctx_
;
739 ctx
.clearRect(0, 0, this.canvasRect_
.w
, this.canvasRect_
.h
);
741 var width
= this.canvasRect_
.w
- margin
;
742 var height
= this.canvasRect_
.h
- margin
;
743 var zoomHandleStatus
= this.getZoomHandleStatus_();
745 ctx
.strokeStyle
= this.getOption_('rangeSelectorForegroundStrokeColor');
746 ctx
.lineWidth
= this.getOption_('rangeSelectorForegroundLineWidth');
747 if (!zoomHandleStatus
.isZoomed
) {
749 ctx
.moveTo(margin
, margin
);
750 ctx
.lineTo(margin
, height
);
751 ctx
.lineTo(width
, height
);
752 ctx
.lineTo(width
, margin
);
755 var leftHandleCanvasPos
= Math
.max(margin
, zoomHandleStatus
.leftHandlePos
- this.canvasRect_
.x
);
756 var rightHandleCanvasPos
= Math
.min(width
, zoomHandleStatus
.rightHandlePos
- this.canvasRect_
.x
);
758 ctx
.fillStyle
= 'rgba(240, 240, 240, ' + this.getOption_('rangeSelectorAlpha').toString() + ')';
759 ctx
.fillRect(0, 0, leftHandleCanvasPos
, this.canvasRect_
.h
);
760 ctx
.fillRect(rightHandleCanvasPos
, 0, this.canvasRect_
.w
- rightHandleCanvasPos
, this.canvasRect_
.h
);
763 ctx
.moveTo(margin
, margin
);
764 ctx
.lineTo(leftHandleCanvasPos
, margin
);
765 ctx
.lineTo(leftHandleCanvasPos
, height
);
766 ctx
.lineTo(rightHandleCanvasPos
, height
);
767 ctx
.lineTo(rightHandleCanvasPos
, margin
);
768 ctx
.lineTo(width
, margin
);
775 * Returns the current zoom handle position information.
776 * @return {Object} The zoom handle status.
778 rangeSelector
.prototype.getZoomHandleStatus_
= function() {
779 var halfHandleWidth
= this.leftZoomHandle_
.width
/2;
780 var leftHandlePos
= parseFloat(this.leftZoomHandle_
.style
.left
) + halfHandleWidth
;
781 var rightHandlePos
= parseFloat(this.rightZoomHandle_
.style
.left
) + halfHandleWidth
;
783 leftHandlePos
: leftHandlePos
,
784 rightHandlePos
: rightHandlePos
,
785 isZoomed
: (leftHandlePos
- 1 > this.canvasRect_
.x
|| rightHandlePos
+ 1 < this.canvasRect_
.x
+this.canvasRect_
.w
)
789 export default rangeSelector
;