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 Dygraph
.Plugins
.RangeSelector
= (function() {
15 /*global Dygraph:false */
18 var rangeSelector
= function() {
19 this.hasTouchInterface_
= typeof(TouchEvent
) != 'undefined';
20 this.isMobileDevice_
= /mobile|android/gi.test(navigator
.appVersion
);
21 this.interfaceCreated_
= false;
24 rangeSelector
.prototype.toString
= function() {
25 return "RangeSelector Plugin";
28 rangeSelector
.prototype.activate
= function(dygraph
) {
29 this.dygraph_
= dygraph
;
30 if (this.getOption_('showRangeSelector')) {
31 this.createInterface_();
34 layout
: this.reserveSpace_
,
35 predraw
: this.renderStaticLayer_
,
36 didDrawChart
: this.renderInteractiveLayer_
40 rangeSelector
.prototype.destroy
= function() {
41 this.bgcanvas_
= null;
42 this.fgcanvas_
= null;
43 this.leftZoomHandle_
= null;
44 this.rightZoomHandle_
= null;
47 //------------------------------------------------------------------
49 //------------------------------------------------------------------
51 rangeSelector
.prototype.getOption_
= function(name
, opt_series
) {
52 return this.dygraph_
.getOption(name
, opt_series
);
55 rangeSelector
.prototype.setDefaultOption_
= function(name
, value
) {
56 this.dygraph_
.attrs_
[name
] = value
;
61 * Creates the range selector elements and adds them to the graph.
63 rangeSelector
.prototype.createInterface_
= function() {
64 this.createCanvases_();
65 this.createZoomHandles_();
66 this.initInteraction_();
68 // Range selector and animatedZooms have a bad interaction. See issue 359.
69 if (this.getOption_('animatedZooms')) {
70 console
.warn('Animated zooms and range selector are not compatible; disabling animatedZooms.');
71 this.dygraph_
.updateOptions({animatedZooms
: false}, true);
74 this.interfaceCreated_
= true;
80 * Adds the range selector to the graph.
82 rangeSelector
.prototype.addToGraph_
= function() {
83 var graphDiv
= this.graphDiv_
= this.dygraph_
.graphDiv
;
84 graphDiv
.appendChild(this.bgcanvas_
);
85 graphDiv
.appendChild(this.fgcanvas_
);
86 graphDiv
.appendChild(this.leftZoomHandle_
);
87 graphDiv
.appendChild(this.rightZoomHandle_
);
92 * Removes the range selector from the graph.
94 rangeSelector
.prototype.removeFromGraph_
= function() {
95 var graphDiv
= this.graphDiv_
;
96 graphDiv
.removeChild(this.bgcanvas_
);
97 graphDiv
.removeChild(this.fgcanvas_
);
98 graphDiv
.removeChild(this.leftZoomHandle_
);
99 graphDiv
.removeChild(this.rightZoomHandle_
);
100 this.graphDiv_
= null;
105 * Called by Layout to allow range selector to reserve its space.
107 rangeSelector
.prototype.reserveSpace_
= function(e
) {
108 if (this.getOption_('showRangeSelector')) {
109 e
.reserveSpaceBottom(this.getOption_('rangeSelectorHeight') + 4);
115 * Renders the static portion of the range selector at the predraw stage.
117 rangeSelector
.prototype.renderStaticLayer_
= function() {
118 if (!this.updateVisibility_()) {
122 this.drawStaticLayer_();
127 * Renders the interactive portion of the range selector after the chart has been drawn.
129 rangeSelector
.prototype.renderInteractiveLayer_
= function() {
130 if (!this.updateVisibility_() || this.isChangingRange_
) {
133 this.placeZoomHandles_();
134 this.drawInteractiveLayer_();
139 * Check to see if the range selector is enabled/disabled and update visibility accordingly.
141 rangeSelector
.prototype.updateVisibility_
= function() {
142 var enabled
= this.getOption_('showRangeSelector');
144 if (!this.interfaceCreated_
) {
145 this.createInterface_();
146 } else if (!this.graphDiv_
|| !this.graphDiv_
.parentNode
) {
149 } else if (this.graphDiv_
) {
150 this.removeFromGraph_();
151 var dygraph
= this.dygraph_
;
152 setTimeout(function() { dygraph
.width_
= 0; dygraph
.resize(); }, 1);
159 * Resizes the range selector.
161 rangeSelector
.prototype.resize_
= function() {
162 function setElementRect(canvas
, context
, rect
) {
163 var canvasScale
= Dygraph
.getContextPixelRatio(context
);
165 canvas
.style
.top
= rect
.y
+ 'px';
166 canvas
.style
.left
= rect
.x
+ 'px';
167 canvas
.width
= rect
.w
* canvasScale
;
168 canvas
.height
= rect
.h
* canvasScale
;
169 canvas
.style
.width
= rect
.w
+ 'px';
170 canvas
.style
.height
= rect
.h
+ 'px';
172 if(canvasScale
!= 1) {
173 context
.scale(canvasScale
, canvasScale
);
177 var plotArea
= this.dygraph_
.layout_
.getPlotArea();
179 var xAxisLabelHeight
= 0;
180 if (this.dygraph_
.getOptionForAxis('drawAxis', 'x')) {
181 xAxisLabelHeight
= this.getOption_('xAxisHeight') || (this.getOption_('axisLabelFontSize') + 2 * this.getOption_('axisTickSize'));
185 y
: plotArea
.y
+ plotArea
.h
+ xAxisLabelHeight
+ 4,
187 h
: this.getOption_('rangeSelectorHeight')
190 setElementRect(this.bgcanvas_
, this.bgcanvas_ctx_
, this.canvasRect_
);
191 setElementRect(this.fgcanvas_
, this.fgcanvas_ctx_
, this.canvasRect_
);
196 * Creates the background and foreground canvases.
198 rangeSelector
.prototype.createCanvases_
= function() {
199 this.bgcanvas_
= Dygraph
.createCanvas();
200 this.bgcanvas_
.className
= 'dygraph-rangesel-bgcanvas';
201 this.bgcanvas_
.style
.position
= 'absolute';
202 this.bgcanvas_
.style
.zIndex
= 9;
203 this.bgcanvas_ctx_
= Dygraph
.getContext(this.bgcanvas_
);
205 this.fgcanvas_
= Dygraph
.createCanvas();
206 this.fgcanvas_
.className
= 'dygraph-rangesel-fgcanvas';
207 this.fgcanvas_
.style
.position
= 'absolute';
208 this.fgcanvas_
.style
.zIndex
= 9;
209 this.fgcanvas_
.style
.cursor
= 'default';
210 this.fgcanvas_ctx_
= Dygraph
.getContext(this.fgcanvas_
);
215 * Creates the zoom handle elements.
217 rangeSelector
.prototype.createZoomHandles_
= function() {
218 var img
= new Image();
219 img
.className
= 'dygraph-rangesel-zoomhandle';
220 img
.style
.position
= 'absolute';
221 img
.style
.zIndex
= 10;
222 img
.style
.visibility
= 'hidden'; // Initially hidden so they don't show up in the wrong place.
223 img
.style
.cursor
= 'col-resize';
224 // TODO: change image to more options
227 img
.src
= 'data:image/png;base64,' +
228 'iVBORw0KGgoAAAANSUhEUgAAAAkAAAAQCAYAAADESFVDAAAAAXNSR0IArs4c6QAAAAZiS0dEANAA' +
229 'zwDP4Z7KegAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9sHGw0cMqdt1UwAAAAZdEVYdENv' +
230 'bW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAAaElEQVQoz+3SsRFAQBCF4Z9WJM8KCDVwownl' +
231 '6YXsTmCUsyKGkZzcl7zkz3YLkypgAnreFmDEpHkIwVOMfpdi9CEEN2nGpFdwD03yEqDtOgCaun7s' +
232 'qSTDH32I1pQA2Pb9sZecAxc5r3IAb21d6878xsAAAAAASUVORK5CYII=';
234 if (this.isMobileDevice_
) {
239 this.leftZoomHandle_
= img
;
240 this.rightZoomHandle_
= img
.cloneNode(false);
245 * Sets up the interaction for the range selector.
247 rangeSelector
.prototype.initInteraction_
= function() {
249 var topElem
= document
;
252 var isZooming
= false;
253 var isPanning
= false;
254 var dynamic
= !this.isMobileDevice_
;
256 // We cover iframes during mouse interactions. See comments in
257 // dygraph-utils.js for more info on why this is a good idea.
258 var tarp
= new Dygraph
.IFrameTarp();
260 // functions, defined below. Defining them this way (rather than with
261 // "function foo() {...}" makes JSHint happy.
262 var toXDataWindow
, onZoomStart
, onZoom
, onZoomEnd
, doZoom
, isMouseInPanZone
,
263 onPanStart
, onPan
, onPanEnd
, doPan
, onCanvasHover
;
265 // Touch event functions
266 var onZoomHandleTouchEvent
, onCanvasTouchEvent
, addTouchEvents
;
268 toXDataWindow
= function(zoomHandleStatus
) {
269 var xDataLimits
= self
.dygraph_
.xAxisExtremes();
270 var fact
= (xDataLimits
[1] - xDataLimits
[0])/self
.canvasRect_
.w
;
271 var xDataMin
= xDataLimits
[0] + (zoomHandleStatus
.leftHandlePos
- self
.canvasRect_
.x
)*fact
;
272 var xDataMax
= xDataLimits
[0] + (zoomHandleStatus
.rightHandlePos
- self
.canvasRect_
.x
)*fact
;
273 return [xDataMin
, xDataMax
];
276 onZoomStart
= function(e
) {
277 Dygraph
.cancelEvent(e
);
279 clientXLast
= e
.clientX
;
280 handle
= e
.target
? e
.target
: e
.srcElement
;
281 if (e
.type
=== 'mousedown' || e
.type
=== 'dragstart') {
282 // These events are removed manually.
283 Dygraph
.addEvent(topElem
, 'mousemove', onZoom
);
284 Dygraph
.addEvent(topElem
, 'mouseup', onZoomEnd
);
286 self
.fgcanvas_
.style
.cursor
= 'col-resize';
291 onZoom
= function(e
) {
295 Dygraph
.cancelEvent(e
);
297 var delX
= e
.clientX
- clientXLast
;
298 if (Math
.abs(delX
) < 4) {
301 clientXLast
= e
.clientX
;
304 var zoomHandleStatus
= self
.getZoomHandleStatus_();
306 if (handle
== self
.leftZoomHandle_
) {
307 newPos
= zoomHandleStatus
.leftHandlePos
+ delX
;
308 newPos
= Math
.min(newPos
, zoomHandleStatus
.rightHandlePos
- handle
.width
- 3);
309 newPos
= Math
.max(newPos
, self
.canvasRect_
.x
);
311 newPos
= zoomHandleStatus
.rightHandlePos
+ delX
;
312 newPos
= Math
.min(newPos
, self
.canvasRect_
.x
+ self
.canvasRect_
.w
);
313 newPos
= Math
.max(newPos
, zoomHandleStatus
.leftHandlePos
+ handle
.width
+ 3);
315 var halfHandleWidth
= handle
.width
/2;
316 handle
.style
.left
= (newPos
- halfHandleWidth
) + 'px';
317 self
.drawInteractiveLayer_();
326 onZoomEnd
= function(e
) {
332 Dygraph
.removeEvent(topElem
, 'mousemove', onZoom
);
333 Dygraph
.removeEvent(topElem
, 'mouseup', onZoomEnd
);
334 self
.fgcanvas_
.style
.cursor
= 'default';
336 // If on a slower device, zoom now.
343 doZoom
= function() {
345 var zoomHandleStatus
= self
.getZoomHandleStatus_();
346 self
.isChangingRange_
= true;
347 if (!zoomHandleStatus
.isZoomed
) {
348 self
.dygraph_
.resetZoom();
350 var xDataWindow
= toXDataWindow(zoomHandleStatus
);
351 self
.dygraph_
.doZoomXDates_(xDataWindow
[0], xDataWindow
[1]);
354 self
.isChangingRange_
= false;
358 isMouseInPanZone
= function(e
) {
359 var rect
= self
.leftZoomHandle_
.getBoundingClientRect();
360 var leftHandleClientX
= rect
.left
+ rect
.width
/2;
361 rect
= self
.rightZoomHandle_
.getBoundingClientRect();
362 var rightHandleClientX
= rect
.left
+ rect
.width
/2;
363 return (e
.clientX
> leftHandleClientX
&& e
.clientX
< rightHandleClientX
);
366 onPanStart
= function(e
) {
367 if (!isPanning
&& isMouseInPanZone(e
) && self
.getZoomHandleStatus_().isZoomed
) {
368 Dygraph
.cancelEvent(e
);
370 clientXLast
= e
.clientX
;
371 if (e
.type
=== 'mousedown') {
372 // These events are removed manually.
373 Dygraph
.addEvent(topElem
, 'mousemove', onPan
);
374 Dygraph
.addEvent(topElem
, 'mouseup', onPanEnd
);
381 onPan
= function(e
) {
385 Dygraph
.cancelEvent(e
);
387 var delX
= e
.clientX
- clientXLast
;
388 if (Math
.abs(delX
) < 4) {
391 clientXLast
= e
.clientX
;
394 var zoomHandleStatus
= self
.getZoomHandleStatus_();
395 var leftHandlePos
= zoomHandleStatus
.leftHandlePos
;
396 var rightHandlePos
= zoomHandleStatus
.rightHandlePos
;
397 var rangeSize
= rightHandlePos
- leftHandlePos
;
398 if (leftHandlePos
+ delX
<= self
.canvasRect_
.x
) {
399 leftHandlePos
= self
.canvasRect_
.x
;
400 rightHandlePos
= leftHandlePos
+ rangeSize
;
401 } else if (rightHandlePos
+ delX
>= self
.canvasRect_
.x
+ self
.canvasRect_
.w
) {
402 rightHandlePos
= self
.canvasRect_
.x
+ self
.canvasRect_
.w
;
403 leftHandlePos
= rightHandlePos
- rangeSize
;
405 leftHandlePos
+= delX
;
406 rightHandlePos
+= delX
;
408 var halfHandleWidth
= self
.leftZoomHandle_
.width
/2;
409 self
.leftZoomHandle_
.style
.left
= (leftHandlePos
- halfHandleWidth
) + 'px';
410 self
.rightZoomHandle_
.style
.left
= (rightHandlePos
- halfHandleWidth
) + 'px';
411 self
.drawInteractiveLayer_();
413 // Do pan on the fly.
420 onPanEnd
= function(e
) {
425 Dygraph
.removeEvent(topElem
, 'mousemove', onPan
);
426 Dygraph
.removeEvent(topElem
, 'mouseup', onPanEnd
);
427 // If on a slower device, do pan now.
436 self
.isChangingRange_
= true;
437 self
.dygraph_
.dateWindow_
= toXDataWindow(self
.getZoomHandleStatus_());
438 self
.dygraph_
.drawGraph_(false);
440 self
.isChangingRange_
= false;
444 onCanvasHover
= function(e
) {
445 if (isZooming
|| isPanning
) {
448 var cursor
= isMouseInPanZone(e
) ? 'move' : 'default';
449 if (cursor
!= self
.fgcanvas_
.style
.cursor
) {
450 self
.fgcanvas_
.style
.cursor
= cursor
;
454 onZoomHandleTouchEvent
= function(e
) {
455 if (e
.type
== 'touchstart' && e
.targetTouches
.length
== 1) {
456 if (onZoomStart(e
.targetTouches
[0])) {
457 Dygraph
.cancelEvent(e
);
459 } else if (e
.type
== 'touchmove' && e
.targetTouches
.length
== 1) {
460 if (onZoom(e
.targetTouches
[0])) {
461 Dygraph
.cancelEvent(e
);
468 onCanvasTouchEvent
= function(e
) {
469 if (e
.type
== 'touchstart' && e
.targetTouches
.length
== 1) {
470 if (onPanStart(e
.targetTouches
[0])) {
471 Dygraph
.cancelEvent(e
);
473 } else if (e
.type
== 'touchmove' && e
.targetTouches
.length
== 1) {
474 if (onPan(e
.targetTouches
[0])) {
475 Dygraph
.cancelEvent(e
);
482 addTouchEvents
= function(elem
, fn
) {
483 var types
= ['touchstart', 'touchend', 'touchmove', 'touchcancel'];
484 for (var i
= 0; i
< types
.length
; i
++) {
485 self
.dygraph_
.addAndTrackEvent(elem
, types
[i
], fn
);
489 this.setDefaultOption_('interactionModel', Dygraph
.Interaction
.dragIsPanInteractionModel
);
490 this.setDefaultOption_('panEdgeFraction', 0.0001);
492 var dragStartEvent
= window
.opera
? 'mousedown' : 'dragstart';
493 this.dygraph_
.addAndTrackEvent(this.leftZoomHandle_
, dragStartEvent
, onZoomStart
);
494 this.dygraph_
.addAndTrackEvent(this.rightZoomHandle_
, dragStartEvent
, onZoomStart
);
496 this.dygraph_
.addAndTrackEvent(this.fgcanvas_
, 'mousedown', onPanStart
);
497 this.dygraph_
.addAndTrackEvent(this.fgcanvas_
, 'mousemove', onCanvasHover
);
500 if (this.hasTouchInterface_
) {
501 addTouchEvents(this.leftZoomHandle_
, onZoomHandleTouchEvent
);
502 addTouchEvents(this.rightZoomHandle_
, onZoomHandleTouchEvent
);
503 addTouchEvents(this.fgcanvas_
, onCanvasTouchEvent
);
509 * Draws the static layer in the background canvas.
511 rangeSelector
.prototype.drawStaticLayer_
= function() {
512 var ctx
= this.bgcanvas_ctx_
;
513 ctx
.clearRect(0, 0, this.canvasRect_
.w
, this.canvasRect_
.h
);
515 this.drawMiniPlot_();
521 this.bgcanvas_ctx_
.lineWidth
= this.getOption_('rangeSelectorBackgroundLineWidth');
522 ctx
.strokeStyle
= this.getOption_('rangeSelectorBackgroundStrokeColor');
524 ctx
.moveTo(margin
, margin
);
525 ctx
.lineTo(margin
, this.canvasRect_
.h
-margin
);
526 ctx
.lineTo(this.canvasRect_
.w
-margin
, this.canvasRect_
.h
-margin
);
527 ctx
.lineTo(this.canvasRect_
.w
-margin
, margin
);
534 * Draws the mini plot in the background canvas.
536 rangeSelector
.prototype.drawMiniPlot_
= function() {
537 var fillStyle
= this.getOption_('rangeSelectorPlotFillColor');
538 var fillGradientStyle
= this.getOption_('rangeSelectorPlotFillGradientColor');
539 var strokeStyle
= this.getOption_('rangeSelectorPlotStrokeColor');
540 if (!fillStyle
&& !strokeStyle
) {
544 var stepPlot
= this.getOption_('stepPlot');
546 var combinedSeriesData
= this.computeCombinedSeriesAndLimits_();
547 var yRange
= combinedSeriesData
.yMax
- combinedSeriesData
.yMin
;
549 // Draw the mini plot.
550 var ctx
= this.bgcanvas_ctx_
;
553 var xExtremes
= this.dygraph_
.xAxisExtremes();
554 var xRange
= Math
.max(xExtremes
[1] - xExtremes
[0], 1.e
-30);
555 var xFact
= (this.canvasRect_
.w
- margin
)/xRange
;
556 var yFact
= (this.canvasRect_
.h
- margin
)/yRange
;
557 var canvasWidth
= this.canvasRect_
.w
- margin
;
558 var canvasHeight
= this.canvasRect_
.h
- margin
;
560 var prevX
= null, prevY
= null;
563 ctx
.moveTo(margin
, canvasHeight
);
564 for (var i
= 0; i
< combinedSeriesData
.data
.length
; i
++) {
565 var dataPoint
= combinedSeriesData
.data
[i
];
566 var x
= ((dataPoint
[0] !== null) ? ((dataPoint
[0] - xExtremes
[0])*xFact
) : NaN
);
567 var y
= ((dataPoint
[1] !== null) ? (canvasHeight
- (dataPoint
[1] - combinedSeriesData
.yMin
)*yFact
) : NaN
);
569 // Skip points that don't change the x-value. Overly fine-grained points
570 // can cause major slowdowns with the ctx.fill() call below.
571 if (!stepPlot
&& prevX
!== null && Math
.round(x
) == Math
.round(prevX
)) {
575 if (isFinite(x
) && isFinite(y
)) {
577 ctx
.lineTo(x
, canvasHeight
);
580 ctx
.lineTo(x
, prevY
);
589 ctx
.lineTo(x
, prevY
);
590 ctx
.lineTo(x
, canvasHeight
);
593 ctx
.lineTo(prevX
, canvasHeight
);
596 prevX
= prevY
= null;
599 ctx
.lineTo(canvasWidth
, canvasHeight
);
603 var lingrad
= this.bgcanvas_ctx_
.createLinearGradient(0, 0, 0, canvasHeight
);
604 if (fillGradientStyle
) {
605 lingrad
.addColorStop(0, fillGradientStyle
);
607 lingrad
.addColorStop(1, fillStyle
);
608 this.bgcanvas_ctx_
.fillStyle
= lingrad
;
613 this.bgcanvas_ctx_
.strokeStyle
= strokeStyle
;
614 this.bgcanvas_ctx_
.lineWidth
= this.getOption_('rangeSelectorPlotLineWidth');
621 * Computes and returns the combined series data along with min/max for the mini plot.
622 * The combined series consists of averaged values for all series.
623 * When series have error bars, the error bars are ignored.
624 * @return {Object} An object containing combined series array, ymin, ymax.
626 rangeSelector
.prototype.computeCombinedSeriesAndLimits_
= function() {
627 var g
= this.dygraph_
;
628 var logscale
= this.getOption_('logscale');
631 // Select series to combine. By default, all series are combined.
632 var numColumns
= g
.numColumns();
633 var labels
= g
.getLabels();
634 var includeSeries
= new Array(numColumns
);
636 for (i
= 1; i
< numColumns
; i
++) {
637 var include
= this.getOption_('showInRangeSelector', labels
[i
]);
638 includeSeries
[i
] = include
;
639 if (include
!== null) anySet
= true; // it's set explicitly for this series
642 for (i
= 0; i
< includeSeries
.length
; i
++) includeSeries
[i
] = true;
645 // Create a combined series (average of selected series values).
646 // TODO(danvk): short-circuit if there's only one series.
647 var rolledSeries
= [];
648 var dataHandler
= g
.dataHandler_
;
649 var options
= g
.attributes_
;
650 for (i
= 1; i
< g
.numColumns(); i
++) {
651 if (!includeSeries
[i
]) continue;
652 var series
= dataHandler
.extractSeries(g
.rawData_
, i
, options
);
653 if (g
.rollPeriod() > 1) {
654 series
= dataHandler
.rollingAverage(series
, g
.rollPeriod(), options
);
657 rolledSeries
.push(series
);
660 var combinedSeries
= [];
661 for (i
= 0; i
< rolledSeries
[0].length
; i
++) {
664 for (var j
= 0; j
< rolledSeries
.length
; j
++) {
665 var y
= rolledSeries
[j
][i
][1];
666 if (y
=== null || isNaN(y
)) continue;
670 combinedSeries
.push([rolledSeries
[0][i
][0], sum
/ count
]);
673 // Compute the y range.
674 var yMin
= Number
.MAX_VALUE
;
675 var yMax
= -Number
.MAX_VALUE
;
676 for (i
= 0; i
< combinedSeries
.length
; i
++) {
677 var yVal
= combinedSeries
[i
][1];
678 if (yVal
!== null && isFinite(yVal
) && (!logscale
|| yVal
> 0)) {
679 yMin
= Math
.min(yMin
, yVal
);
680 yMax
= Math
.max(yMax
, yVal
);
684 // Convert Y data to log scale if needed.
685 // Also, expand the Y range to compress the mini plot a little.
686 var extraPercent
= 0.25;
688 yMax
= Dygraph
.log10(yMax
);
689 yMax
+= yMax
*extraPercent
;
690 yMin
= Dygraph
.log10(yMin
);
691 for (i
= 0; i
< combinedSeries
.length
; i
++) {
692 combinedSeries
[i
][1] = Dygraph
.log10(combinedSeries
[i
][1]);
696 var yRange
= yMax
- yMin
;
697 if (yRange
<= Number
.MIN_VALUE
) {
698 yExtra
= yMax
*extraPercent
;
700 yExtra
= yRange
*extraPercent
;
706 return {data
: combinedSeries
, yMin
: yMin
, yMax
: yMax
};
711 * Places the zoom handles in the proper position based on the current X data window.
713 rangeSelector
.prototype.placeZoomHandles_
= function() {
714 var xExtremes
= this.dygraph_
.xAxisExtremes();
715 var xWindowLimits
= this.dygraph_
.xAxisRange();
716 var xRange
= xExtremes
[1] - xExtremes
[0];
717 var leftPercent
= Math
.max(0, (xWindowLimits
[0] - xExtremes
[0])/xRange
);
718 var rightPercent
= Math
.max(0, (xExtremes
[1] - xWindowLimits
[1])/xRange
);
719 var leftCoord
= this.canvasRect_
.x
+ this.canvasRect_
.w
*leftPercent
;
720 var rightCoord
= this.canvasRect_
.x
+ this.canvasRect_
.w
*(1 - rightPercent
);
721 var handleTop
= Math
.max(this.canvasRect_
.y
, this.canvasRect_
.y
+ (this.canvasRect_
.h
- this.leftZoomHandle_
.height
)/2);
722 var halfHandleWidth
= this.leftZoomHandle_
.width
/2;
723 this.leftZoomHandle_
.style
.left
= (leftCoord
- halfHandleWidth
) + 'px';
724 this.leftZoomHandle_
.style
.top
= handleTop
+ 'px';
725 this.rightZoomHandle_
.style
.left
= (rightCoord
- halfHandleWidth
) + 'px';
726 this.rightZoomHandle_
.style
.top
= this.leftZoomHandle_
.style
.top
;
728 this.leftZoomHandle_
.style
.visibility
= 'visible';
729 this.rightZoomHandle_
.style
.visibility
= 'visible';
734 * Draws the interactive layer in the foreground canvas.
736 rangeSelector
.prototype.drawInteractiveLayer_
= function() {
737 var ctx
= this.fgcanvas_ctx_
;
738 ctx
.clearRect(0, 0, this.canvasRect_
.w
, this.canvasRect_
.h
);
740 var width
= this.canvasRect_
.w
- margin
;
741 var height
= this.canvasRect_
.h
- margin
;
742 var zoomHandleStatus
= this.getZoomHandleStatus_();
744 ctx
.strokeStyle
= this.getOption_('rangeSelectorForegroundStrokeColor');
745 ctx
.lineWidth
= this.getOption_('rangeSelectorForegroundLineWidth');
746 if (!zoomHandleStatus
.isZoomed
) {
748 ctx
.moveTo(margin
, margin
);
749 ctx
.lineTo(margin
, height
);
750 ctx
.lineTo(width
, height
);
751 ctx
.lineTo(width
, margin
);
754 var leftHandleCanvasPos
= Math
.max(margin
, zoomHandleStatus
.leftHandlePos
- this.canvasRect_
.x
);
755 var rightHandleCanvasPos
= Math
.min(width
, zoomHandleStatus
.rightHandlePos
- this.canvasRect_
.x
);
757 ctx
.fillStyle
= 'rgba(240, 240, 240, ' + this.getOption_('rangeSelectorAlpha').toString() + ')';
758 ctx
.fillRect(0, 0, leftHandleCanvasPos
, this.canvasRect_
.h
);
759 ctx
.fillRect(rightHandleCanvasPos
, 0, this.canvasRect_
.w
- rightHandleCanvasPos
, this.canvasRect_
.h
);
762 ctx
.moveTo(margin
, margin
);
763 ctx
.lineTo(leftHandleCanvasPos
, margin
);
764 ctx
.lineTo(leftHandleCanvasPos
, height
);
765 ctx
.lineTo(rightHandleCanvasPos
, height
);
766 ctx
.lineTo(rightHandleCanvasPos
, margin
);
767 ctx
.lineTo(width
, margin
);
774 * Returns the current zoom handle position information.
775 * @return {Object} The zoom handle status.
777 rangeSelector
.prototype.getZoomHandleStatus_
= function() {
778 var halfHandleWidth
= this.leftZoomHandle_
.width
/2;
779 var leftHandlePos
= parseFloat(this.leftZoomHandle_
.style
.left
) + halfHandleWidth
;
780 var rightHandlePos
= parseFloat(this.rightZoomHandle_
.style
.left
) + halfHandleWidth
;
782 leftHandlePos
: leftHandlePos
,
783 rightHandlePos
: rightHandlePos
,
784 isZoomed
: (leftHandlePos
- 1 > this.canvasRect_
.x
|| rightHandlePos
+ 1 < this.canvasRect_
.x
+this.canvasRect_
.w
)
788 return rangeSelector
;