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 /*jshint globalstrict: true */
16 /*global Dygraph:false */
19 var rangeSelector
= function() {
20 this.isIE_
= /MSIE/.test(navigator
.userAgent
) && !window
.opera
;
21 this.hasTouchInterface_
= typeof(TouchEvent
) != 'undefined';
22 this.isMobileDevice_
= /mobile|android/gi.test(navigator
.appVersion
);
23 this.interfaceCreated_
= false;
26 rangeSelector
.prototype.toString
= function() {
27 return "RangeSelector Plugin";
30 rangeSelector
.prototype.activate
= function(dygraph
) {
31 this.dygraph_
= dygraph
;
32 this.isUsingExcanvas_
= dygraph
.isUsingExcanvas_
;
33 if (this.getOption_('showRangeSelector')) {
34 this.createInterface_();
37 layout
: this.reserveSpace_
,
38 predraw
: this.renderStaticLayer_
,
39 didDrawChart
: this.renderInteractiveLayer_
43 rangeSelector
.prototype.destroy
= function() {
44 this.bgcanvas_
= null;
45 this.fgcanvas_
= null;
46 this.leftZoomHandle_
= null;
47 this.rightZoomHandle_
= null;
48 this.iePanOverlay_
= null;
51 //------------------------------------------------------------------
53 //------------------------------------------------------------------
55 rangeSelector
.prototype.getOption_
= function(name
, opt_series
) {
56 return this.dygraph_
.getOption(name
, opt_series
);
59 rangeSelector
.prototype.setDefaultOption_
= function(name
, value
) {
60 return this.dygraph_
.attrs_
[name
] = value
;
65 * Creates the range selector elements and adds them to the graph.
67 rangeSelector
.prototype.createInterface_
= function() {
68 this.createCanvases_();
69 if (this.isUsingExcanvas_
) {
70 this.createIEPanOverlay_();
72 this.createZoomHandles_();
73 this.initInteraction_();
75 // Range selector and animatedZooms have a bad interaction. See issue 359.
76 if (this.getOption_('animatedZooms')) {
77 console
.warn('Animated zooms and range selector are not compatible; disabling animatedZooms.');
78 this.dygraph_
.updateOptions({animatedZooms
: false}, true);
81 this.interfaceCreated_
= true;
87 * Adds the range selector to the graph.
89 rangeSelector
.prototype.addToGraph_
= function() {
90 var graphDiv
= this.graphDiv_
= this.dygraph_
.graphDiv
;
91 graphDiv
.appendChild(this.bgcanvas_
);
92 graphDiv
.appendChild(this.fgcanvas_
);
93 graphDiv
.appendChild(this.leftZoomHandle_
);
94 graphDiv
.appendChild(this.rightZoomHandle_
);
99 * Removes the range selector from the graph.
101 rangeSelector
.prototype.removeFromGraph_
= function() {
102 var graphDiv
= this.graphDiv_
;
103 graphDiv
.removeChild(this.bgcanvas_
);
104 graphDiv
.removeChild(this.fgcanvas_
);
105 graphDiv
.removeChild(this.leftZoomHandle_
);
106 graphDiv
.removeChild(this.rightZoomHandle_
);
107 this.graphDiv_
= null;
112 * Called by Layout to allow range selector to reserve its space.
114 rangeSelector
.prototype.reserveSpace_
= function(e
) {
115 if (this.getOption_('showRangeSelector')) {
116 e
.reserveSpaceBottom(this.getOption_('rangeSelectorHeight') + 4);
122 * Renders the static portion of the range selector at the predraw stage.
124 rangeSelector
.prototype.renderStaticLayer_
= function() {
125 if (!this.updateVisibility_()) {
129 this.drawStaticLayer_();
134 * Renders the interactive portion of the range selector after the chart has been drawn.
136 rangeSelector
.prototype.renderInteractiveLayer_
= function() {
137 if (!this.updateVisibility_() || this.isChangingRange_
) {
140 this.placeZoomHandles_();
141 this.drawInteractiveLayer_();
146 * Check to see if the range selector is enabled/disabled and update visibility accordingly.
148 rangeSelector
.prototype.updateVisibility_
= function() {
149 var enabled
= this.getOption_('showRangeSelector');
151 if (!this.interfaceCreated_
) {
152 this.createInterface_();
153 } else if (!this.graphDiv_
|| !this.graphDiv_
.parentNode
) {
156 } else if (this.graphDiv_
) {
157 this.removeFromGraph_();
158 var dygraph
= this.dygraph_
;
159 setTimeout(function() { dygraph
.width_
= 0; dygraph
.resize(); }, 1);
166 * Resizes the range selector.
168 rangeSelector
.prototype.resize_
= function() {
169 function setElementRect(canvas
, context
, rect
) {
170 var canvasScale
= Dygraph
.getContextPixelRatio(context
);
172 canvas
.style
.top
= rect
.y
+ 'px';
173 canvas
.style
.left
= rect
.x
+ 'px';
174 canvas
.width
= rect
.w
* canvasScale
;
175 canvas
.height
= rect
.h
* canvasScale
;
176 canvas
.style
.width
= rect
.w
+ 'px';
177 canvas
.style
.height
= rect
.h
+ 'px';
179 if(canvasScale
!= 1) {
180 context
.scale(canvasScale
, canvasScale
);
184 var plotArea
= this.dygraph_
.layout_
.getPlotArea();
186 var xAxisLabelHeight
= 0;
187 if (this.dygraph_
.getOptionForAxis('drawAxis', 'x')) {
188 xAxisLabelHeight
= this.getOption_('xAxisHeight') || (this.getOption_('axisLabelFontSize') + 2 * this.getOption_('axisTickSize'));
192 y
: plotArea
.y
+ plotArea
.h
+ xAxisLabelHeight
+ 4,
194 h
: this.getOption_('rangeSelectorHeight')
197 setElementRect(this.bgcanvas_
, this.bgcanvas_ctx_
, this.canvasRect_
);
198 setElementRect(this.fgcanvas_
, this.fgcanvas_ctx_
, this.canvasRect_
);
203 * Creates the background and foreground canvases.
205 rangeSelector
.prototype.createCanvases_
= function() {
206 this.bgcanvas_
= Dygraph
.createCanvas();
207 this.bgcanvas_
.className
= 'dygraph-rangesel-bgcanvas';
208 this.bgcanvas_
.style
.position
= 'absolute';
209 this.bgcanvas_
.style
.zIndex
= 9;
210 this.bgcanvas_ctx_
= Dygraph
.getContext(this.bgcanvas_
);
212 this.fgcanvas_
= Dygraph
.createCanvas();
213 this.fgcanvas_
.className
= 'dygraph-rangesel-fgcanvas';
214 this.fgcanvas_
.style
.position
= 'absolute';
215 this.fgcanvas_
.style
.zIndex
= 9;
216 this.fgcanvas_
.style
.cursor
= 'default';
217 this.fgcanvas_ctx_
= Dygraph
.getContext(this.fgcanvas_
);
222 * Creates overlay divs for IE/Excanvas so that mouse events are handled properly.
224 rangeSelector
.prototype.createIEPanOverlay_
= function() {
225 this.iePanOverlay_
= document
.createElement("div");
226 this.iePanOverlay_
.style
.position
= 'absolute';
227 this.iePanOverlay_
.style
.backgroundColor
= 'white';
228 this.iePanOverlay_
.style
.filter
= 'alpha(opacity=0)';
229 this.iePanOverlay_
.style
.display
= 'none';
230 this.iePanOverlay_
.style
.cursor
= 'move';
231 this.fgcanvas_
.appendChild(this.iePanOverlay_
);
236 * Creates the zoom handle elements.
238 rangeSelector
.prototype.createZoomHandles_
= function() {
239 var img
= new Image();
240 img
.className
= 'dygraph-rangesel-zoomhandle';
241 img
.style
.position
= 'absolute';
242 img
.style
.zIndex
= 10;
243 img
.style
.visibility
= 'hidden'; // Initially hidden so they don't show up in the wrong place.
244 img
.style
.cursor
= 'col-resize';
246 if (/MSIE 7/.test(navigator
.userAgent
)) { // IE7 doesn't support embedded src data.
249 img
.style
.backgroundColor
= 'white';
250 img
.style
.border
= '1px solid #333333'; // Just show box in IE7.
254 img
.src
= 'data:image/png;base64,' +
255 'iVBORw0KGgoAAAANSUhEUgAAAAkAAAAQCAYAAADESFVDAAAAAXNSR0IArs4c6QAAAAZiS0dEANAA' +
256 'zwDP4Z7KegAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9sHGw0cMqdt1UwAAAAZdEVYdENv' +
257 'bW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAAaElEQVQoz+3SsRFAQBCF4Z9WJM8KCDVwownl' +
258 '6YXsTmCUsyKGkZzcl7zkz3YLkypgAnreFmDEpHkIwVOMfpdi9CEEN2nGpFdwD03yEqDtOgCaun7s' +
259 'qSTDH32I1pQA2Pb9sZecAxc5r3IAb21d6878xsAAAAAASUVORK5CYII=';
262 if (this.isMobileDevice_
) {
267 this.leftZoomHandle_
= img
;
268 this.rightZoomHandle_
= img
.cloneNode(false);
273 * Sets up the interaction for the range selector.
275 rangeSelector
.prototype.initInteraction_
= function() {
277 var topElem
= document
;
280 var isZooming
= false;
281 var isPanning
= false;
282 var dynamic
= !this.isMobileDevice_
&& !this.isUsingExcanvas_
;
284 // We cover iframes during mouse interactions. See comments in
285 // dygraph-utils.js for more info on why this is a good idea.
286 var tarp
= new Dygraph
.IFrameTarp();
288 // functions, defined below. Defining them this way (rather than with
289 // "function foo() {...}" makes JSHint happy.
290 var toXDataWindow
, onZoomStart
, onZoom
, onZoomEnd
, doZoom
, isMouseInPanZone
,
291 onPanStart
, onPan
, onPanEnd
, doPan
, onCanvasHover
;
293 // Touch event functions
294 var onZoomHandleTouchEvent
, onCanvasTouchEvent
, addTouchEvents
;
296 toXDataWindow
= function(zoomHandleStatus
) {
297 var xDataLimits
= self
.dygraph_
.xAxisExtremes();
298 var fact
= (xDataLimits
[1] - xDataLimits
[0])/self
.canvasRect_
.w
;
299 var xDataMin
= xDataLimits
[0] + (zoomHandleStatus
.leftHandlePos
- self
.canvasRect_
.x
)*fact
;
300 var xDataMax
= xDataLimits
[0] + (zoomHandleStatus
.rightHandlePos
- self
.canvasRect_
.x
)*fact
;
301 return [xDataMin
, xDataMax
];
304 onZoomStart
= function(e
) {
305 Dygraph
.cancelEvent(e
);
307 clientXLast
= e
.clientX
;
308 handle
= e
.target
? e
.target
: e
.srcElement
;
309 if (e
.type
=== 'mousedown' || e
.type
=== 'dragstart') {
310 // These events are removed manually.
311 Dygraph
.addEvent(topElem
, 'mousemove', onZoom
);
312 Dygraph
.addEvent(topElem
, 'mouseup', onZoomEnd
);
314 self
.fgcanvas_
.style
.cursor
= 'col-resize';
319 onZoom
= function(e
) {
323 Dygraph
.cancelEvent(e
);
325 var delX
= e
.clientX
- clientXLast
;
326 if (Math
.abs(delX
) < 4) {
329 clientXLast
= e
.clientX
;
332 var zoomHandleStatus
= self
.getZoomHandleStatus_();
334 if (handle
== self
.leftZoomHandle_
) {
335 newPos
= zoomHandleStatus
.leftHandlePos
+ delX
;
336 newPos
= Math
.min(newPos
, zoomHandleStatus
.rightHandlePos
- handle
.width
- 3);
337 newPos
= Math
.max(newPos
, self
.canvasRect_
.x
);
339 newPos
= zoomHandleStatus
.rightHandlePos
+ delX
;
340 newPos
= Math
.min(newPos
, self
.canvasRect_
.x
+ self
.canvasRect_
.w
);
341 newPos
= Math
.max(newPos
, zoomHandleStatus
.leftHandlePos
+ handle
.width
+ 3);
343 var halfHandleWidth
= handle
.width
/2;
344 handle
.style
.left
= (newPos
- halfHandleWidth
) + 'px';
345 self
.drawInteractiveLayer_();
347 // Zoom on the fly (if not using excanvas).
354 onZoomEnd
= function(e
) {
360 Dygraph
.removeEvent(topElem
, 'mousemove', onZoom
);
361 Dygraph
.removeEvent(topElem
, 'mouseup', onZoomEnd
);
362 self
.fgcanvas_
.style
.cursor
= 'default';
364 // If using excanvas, Zoom now.
371 doZoom
= function() {
373 var zoomHandleStatus
= self
.getZoomHandleStatus_();
374 self
.isChangingRange_
= true;
375 if (!zoomHandleStatus
.isZoomed
) {
376 self
.dygraph_
.resetZoom();
378 var xDataWindow
= toXDataWindow(zoomHandleStatus
);
379 self
.dygraph_
.doZoomXDates_(xDataWindow
[0], xDataWindow
[1]);
382 self
.isChangingRange_
= false;
386 isMouseInPanZone
= function(e
) {
387 if (self
.isUsingExcanvas_
) {
388 return e
.srcElement
== self
.iePanOverlay_
;
390 var rect
= self
.leftZoomHandle_
.getBoundingClientRect();
391 var leftHandleClientX
= rect
.left
+ rect
.width
/2;
392 rect
= self
.rightZoomHandle_
.getBoundingClientRect();
393 var rightHandleClientX
= rect
.left
+ rect
.width
/2;
394 return (e
.clientX
> leftHandleClientX
&& e
.clientX
< rightHandleClientX
);
398 onPanStart
= function(e
) {
399 if (!isPanning
&& isMouseInPanZone(e
) && self
.getZoomHandleStatus_().isZoomed
) {
400 Dygraph
.cancelEvent(e
);
402 clientXLast
= e
.clientX
;
403 if (e
.type
=== 'mousedown') {
404 // These events are removed manually.
405 Dygraph
.addEvent(topElem
, 'mousemove', onPan
);
406 Dygraph
.addEvent(topElem
, 'mouseup', onPanEnd
);
413 onPan
= function(e
) {
417 Dygraph
.cancelEvent(e
);
419 var delX
= e
.clientX
- clientXLast
;
420 if (Math
.abs(delX
) < 4) {
423 clientXLast
= e
.clientX
;
426 var zoomHandleStatus
= self
.getZoomHandleStatus_();
427 var leftHandlePos
= zoomHandleStatus
.leftHandlePos
;
428 var rightHandlePos
= zoomHandleStatus
.rightHandlePos
;
429 var rangeSize
= rightHandlePos
- leftHandlePos
;
430 if (leftHandlePos
+ delX
<= self
.canvasRect_
.x
) {
431 leftHandlePos
= self
.canvasRect_
.x
;
432 rightHandlePos
= leftHandlePos
+ rangeSize
;
433 } else if (rightHandlePos
+ delX
>= self
.canvasRect_
.x
+ self
.canvasRect_
.w
) {
434 rightHandlePos
= self
.canvasRect_
.x
+ self
.canvasRect_
.w
;
435 leftHandlePos
= rightHandlePos
- rangeSize
;
437 leftHandlePos
+= delX
;
438 rightHandlePos
+= delX
;
440 var halfHandleWidth
= self
.leftZoomHandle_
.width
/2;
441 self
.leftZoomHandle_
.style
.left
= (leftHandlePos
- halfHandleWidth
) + 'px';
442 self
.rightZoomHandle_
.style
.left
= (rightHandlePos
- halfHandleWidth
) + 'px';
443 self
.drawInteractiveLayer_();
445 // Do pan on the fly (if not using excanvas).
452 onPanEnd
= function(e
) {
457 Dygraph
.removeEvent(topElem
, 'mousemove', onPan
);
458 Dygraph
.removeEvent(topElem
, 'mouseup', onPanEnd
);
459 // If using excanvas, do pan now.
468 self
.isChangingRange_
= true;
469 self
.dygraph_
.dateWindow_
= toXDataWindow(self
.getZoomHandleStatus_());
470 self
.dygraph_
.drawGraph_(false);
472 self
.isChangingRange_
= false;
476 onCanvasHover
= function(e
) {
477 if (isZooming
|| isPanning
) {
480 var cursor
= isMouseInPanZone(e
) ? 'move' : 'default';
481 if (cursor
!= self
.fgcanvas_
.style
.cursor
) {
482 self
.fgcanvas_
.style
.cursor
= cursor
;
486 onZoomHandleTouchEvent
= function(e
) {
487 if (e
.type
== 'touchstart' && e
.targetTouches
.length
== 1) {
488 if (onZoomStart(e
.targetTouches
[0])) {
489 Dygraph
.cancelEvent(e
);
491 } else if (e
.type
== 'touchmove' && e
.targetTouches
.length
== 1) {
492 if (onZoom(e
.targetTouches
[0])) {
493 Dygraph
.cancelEvent(e
);
500 onCanvasTouchEvent
= function(e
) {
501 if (e
.type
== 'touchstart' && e
.targetTouches
.length
== 1) {
502 if (onPanStart(e
.targetTouches
[0])) {
503 Dygraph
.cancelEvent(e
);
505 } else if (e
.type
== 'touchmove' && e
.targetTouches
.length
== 1) {
506 if (onPan(e
.targetTouches
[0])) {
507 Dygraph
.cancelEvent(e
);
514 addTouchEvents
= function(elem
, fn
) {
515 var types
= ['touchstart', 'touchend', 'touchmove', 'touchcancel'];
516 for (var i
= 0; i
< types
.length
; i
++) {
517 self
.dygraph_
.addAndTrackEvent(elem
, types
[i
], fn
);
521 this.setDefaultOption_('interactionModel', Dygraph
.Interaction
.dragIsPanInteractionModel
);
522 this.setDefaultOption_('panEdgeFraction', 0.0001);
524 var dragStartEvent
= window
.opera
? 'mousedown' : 'dragstart';
525 this.dygraph_
.addAndTrackEvent(this.leftZoomHandle_
, dragStartEvent
, onZoomStart
);
526 this.dygraph_
.addAndTrackEvent(this.rightZoomHandle_
, dragStartEvent
, onZoomStart
);
528 if (this.isUsingExcanvas_
) {
529 this.dygraph_
.addAndTrackEvent(this.iePanOverlay_
, 'mousedown', onPanStart
);
531 this.dygraph_
.addAndTrackEvent(this.fgcanvas_
, 'mousedown', onPanStart
);
532 this.dygraph_
.addAndTrackEvent(this.fgcanvas_
, 'mousemove', onCanvasHover
);
536 if (this.hasTouchInterface_
) {
537 addTouchEvents(this.leftZoomHandle_
, onZoomHandleTouchEvent
);
538 addTouchEvents(this.rightZoomHandle_
, onZoomHandleTouchEvent
);
539 addTouchEvents(this.fgcanvas_
, onCanvasTouchEvent
);
545 * Draws the static layer in the background canvas.
547 rangeSelector
.prototype.drawStaticLayer_
= function() {
548 var ctx
= this.bgcanvas_ctx_
;
549 ctx
.clearRect(0, 0, this.canvasRect_
.w
, this.canvasRect_
.h
);
551 this.drawMiniPlot_();
557 this.bgcanvas_ctx_
.lineWidth
= 1;
558 ctx
.strokeStyle
= 'gray';
560 ctx
.moveTo(margin
, margin
);
561 ctx
.lineTo(margin
, this.canvasRect_
.h
-margin
);
562 ctx
.lineTo(this.canvasRect_
.w
-margin
, this.canvasRect_
.h
-margin
);
563 ctx
.lineTo(this.canvasRect_
.w
-margin
, margin
);
570 * Draws the mini plot in the background canvas.
572 rangeSelector
.prototype.drawMiniPlot_
= function() {
573 var fillStyle
= this.getOption_('rangeSelectorPlotFillColor');
574 var strokeStyle
= this.getOption_('rangeSelectorPlotStrokeColor');
575 if (!fillStyle
&& !strokeStyle
) {
579 var stepPlot
= this.getOption_('stepPlot');
581 var combinedSeriesData
= this.computeCombinedSeriesAndLimits_();
582 var yRange
= combinedSeriesData
.yMax
- combinedSeriesData
.yMin
;
584 // Draw the mini plot.
585 var ctx
= this.bgcanvas_ctx_
;
588 var xExtremes
= this.dygraph_
.xAxisExtremes();
589 var xRange
= Math
.max(xExtremes
[1] - xExtremes
[0], 1.e
-30);
590 var xFact
= (this.canvasRect_
.w
- margin
)/xRange
;
591 var yFact
= (this.canvasRect_
.h
- margin
)/yRange
;
592 var canvasWidth
= this.canvasRect_
.w
- margin
;
593 var canvasHeight
= this.canvasRect_
.h
- margin
;
595 var prevX
= null, prevY
= null;
598 ctx
.moveTo(margin
, canvasHeight
);
599 for (var i
= 0; i
< combinedSeriesData
.data
.length
; i
++) {
600 var dataPoint
= combinedSeriesData
.data
[i
];
601 var x
= ((dataPoint
[0] !== null) ? ((dataPoint
[0] - xExtremes
[0])*xFact
) : NaN
);
602 var y
= ((dataPoint
[1] !== null) ? (canvasHeight
- (dataPoint
[1] - combinedSeriesData
.yMin
)*yFact
) : NaN
);
604 // Skip points that don't change the x-value. Overly fine-grained points
605 // can cause major slowdowns with the ctx.fill() call below.
606 if (!stepPlot
&& prevX
!== null && Math
.round(x
) == Math
.round(prevX
)) {
610 if (isFinite(x
) && isFinite(y
)) {
612 ctx
.lineTo(x
, canvasHeight
);
615 ctx
.lineTo(x
, prevY
);
624 ctx
.lineTo(x
, prevY
);
625 ctx
.lineTo(x
, canvasHeight
);
628 ctx
.lineTo(prevX
, canvasHeight
);
631 prevX
= prevY
= null;
634 ctx
.lineTo(canvasWidth
, canvasHeight
);
638 var lingrad
= this.bgcanvas_ctx_
.createLinearGradient(0, 0, 0, canvasHeight
);
639 lingrad
.addColorStop(0, 'white');
640 lingrad
.addColorStop(1, fillStyle
);
641 this.bgcanvas_ctx_
.fillStyle
= lingrad
;
646 this.bgcanvas_ctx_
.strokeStyle
= strokeStyle
;
647 this.bgcanvas_ctx_
.lineWidth
= 1.5;
654 * Computes and returns the combined series data along with min/max for the mini plot.
655 * The combined series consists of averaged values for all series.
656 * When series have error bars, the error bars are ignored.
657 * @return {Object} An object containing combined series array, ymin, ymax.
659 rangeSelector
.prototype.computeCombinedSeriesAndLimits_
= function() {
660 var g
= this.dygraph_
;
661 var logscale
= this.getOption_('logscale');
664 // Select series to combine. By default, all series are combined.
665 var numColumns
= g
.numColumns();
666 var labels
= g
.getLabels();
667 var includeSeries
= new Array(numColumns
);
669 for (i
= 1; i
< numColumns
; i
++) {
670 var include
= this.getOption_('showInRangeSelector', labels
[i
]);
671 includeSeries
[i
] = include
;
672 if (include
!== null) anySet
= true; // it's set explicitly for this series
675 for (i
= 0; i
< includeSeries
.length
; i
++) includeSeries
[i
] = true;
678 // Create a combined series (average of selected series values).
679 // TODO(danvk): short-circuit if there's only one series.
680 var rolledSeries
= [];
681 var dataHandler
= g
.dataHandler_
;
682 var options
= g
.attributes_
;
683 for (i
= 1; i
< g
.numColumns(); i
++) {
684 if (!includeSeries
[i
]) continue;
685 var series
= dataHandler
.extractSeries(g
.rawData_
, i
, options
);
686 if (g
.rollPeriod() > 1) {
687 series
= dataHandler
.rollingAverage(series
, g
.rollPeriod(), options
);
690 rolledSeries
.push(series
);
693 var combinedSeries
= [];
694 for (i
= 0; i
< rolledSeries
[0].length
; i
++) {
697 for (var j
= 0; j
< rolledSeries
.length
; j
++) {
698 var y
= rolledSeries
[j
][i
][1];
699 if (y
=== null || isNaN(y
)) continue;
703 combinedSeries
.push([rolledSeries
[0][i
][0], sum
/ count
]);
706 // Compute the y range.
707 var yMin
= Number
.MAX_VALUE
;
708 var yMax
= -Number
.MAX_VALUE
;
709 for (i
= 0; i
< combinedSeries
.length
; i
++) {
710 var yVal
= combinedSeries
[i
][1];
711 if (yVal
!== null && isFinite(yVal
) && (!logscale
|| yVal
> 0)) {
712 yMin
= Math
.min(yMin
, yVal
);
713 yMax
= Math
.max(yMax
, yVal
);
717 // Convert Y data to log scale if needed.
718 // Also, expand the Y range to compress the mini plot a little.
719 var extraPercent
= 0.25;
721 yMax
= Dygraph
.log10(yMax
);
722 yMax
+= yMax
*extraPercent
;
723 yMin
= Dygraph
.log10(yMin
);
724 for (i
= 0; i
< combinedSeries
.length
; i
++) {
725 combinedSeries
[i
][1] = Dygraph
.log10(combinedSeries
[i
][1]);
729 var yRange
= yMax
- yMin
;
730 if (yRange
<= Number
.MIN_VALUE
) {
731 yExtra
= yMax
*extraPercent
;
733 yExtra
= yRange
*extraPercent
;
739 return {data
: combinedSeries
, yMin
: yMin
, yMax
: yMax
};
744 * Places the zoom handles in the proper position based on the current X data window.
746 rangeSelector
.prototype.placeZoomHandles_
= function() {
747 var xExtremes
= this.dygraph_
.xAxisExtremes();
748 var xWindowLimits
= this.dygraph_
.xAxisRange();
749 var xRange
= xExtremes
[1] - xExtremes
[0];
750 var leftPercent
= Math
.max(0, (xWindowLimits
[0] - xExtremes
[0])/xRange
);
751 var rightPercent
= Math
.max(0, (xExtremes
[1] - xWindowLimits
[1])/xRange
);
752 var leftCoord
= this.canvasRect_
.x
+ this.canvasRect_
.w
*leftPercent
;
753 var rightCoord
= this.canvasRect_
.x
+ this.canvasRect_
.w
*(1 - rightPercent
);
754 var handleTop
= Math
.max(this.canvasRect_
.y
, this.canvasRect_
.y
+ (this.canvasRect_
.h
- this.leftZoomHandle_
.height
)/2);
755 var halfHandleWidth
= this.leftZoomHandle_
.width
/2;
756 this.leftZoomHandle_
.style
.left
= (leftCoord
- halfHandleWidth
) + 'px';
757 this.leftZoomHandle_
.style
.top
= handleTop
+ 'px';
758 this.rightZoomHandle_
.style
.left
= (rightCoord
- halfHandleWidth
) + 'px';
759 this.rightZoomHandle_
.style
.top
= this.leftZoomHandle_
.style
.top
;
761 this.leftZoomHandle_
.style
.visibility
= 'visible';
762 this.rightZoomHandle_
.style
.visibility
= 'visible';
767 * Draws the interactive layer in the foreground canvas.
769 rangeSelector
.prototype.drawInteractiveLayer_
= function() {
770 var ctx
= this.fgcanvas_ctx_
;
771 ctx
.clearRect(0, 0, this.canvasRect_
.w
, this.canvasRect_
.h
);
773 var width
= this.canvasRect_
.w
- margin
;
774 var height
= this.canvasRect_
.h
- margin
;
775 var zoomHandleStatus
= this.getZoomHandleStatus_();
777 ctx
.strokeStyle
= 'black';
778 if (!zoomHandleStatus
.isZoomed
) {
780 ctx
.moveTo(margin
, margin
);
781 ctx
.lineTo(margin
, height
);
782 ctx
.lineTo(width
, height
);
783 ctx
.lineTo(width
, margin
);
785 if (this.iePanOverlay_
) {
786 this.iePanOverlay_
.style
.display
= 'none';
789 var leftHandleCanvasPos
= Math
.max(margin
, zoomHandleStatus
.leftHandlePos
- this.canvasRect_
.x
);
790 var rightHandleCanvasPos
= Math
.min(width
, zoomHandleStatus
.rightHandlePos
- this.canvasRect_
.x
);
792 ctx
.fillStyle
= 'rgba(240, 240, 240, 0.6)';
793 ctx
.fillRect(0, 0, leftHandleCanvasPos
, this.canvasRect_
.h
);
794 ctx
.fillRect(rightHandleCanvasPos
, 0, this.canvasRect_
.w
- rightHandleCanvasPos
, this.canvasRect_
.h
);
797 ctx
.moveTo(margin
, margin
);
798 ctx
.lineTo(leftHandleCanvasPos
, margin
);
799 ctx
.lineTo(leftHandleCanvasPos
, height
);
800 ctx
.lineTo(rightHandleCanvasPos
, height
);
801 ctx
.lineTo(rightHandleCanvasPos
, margin
);
802 ctx
.lineTo(width
, margin
);
805 if (this.isUsingExcanvas_
) {
806 this.iePanOverlay_
.style
.width
= (rightHandleCanvasPos
- leftHandleCanvasPos
) + 'px';
807 this.iePanOverlay_
.style
.left
= leftHandleCanvasPos
+ 'px';
808 this.iePanOverlay_
.style
.height
= height
+ 'px';
809 this.iePanOverlay_
.style
.display
= 'inline';
816 * Returns the current zoom handle position information.
817 * @return {Object} The zoom handle status.
819 rangeSelector
.prototype.getZoomHandleStatus_
= function() {
820 var halfHandleWidth
= this.leftZoomHandle_
.width
/2;
821 var leftHandlePos
= parseFloat(this.leftZoomHandle_
.style
.left
) + halfHandleWidth
;
822 var rightHandlePos
= parseFloat(this.rightZoomHandle_
.style
.left
) + halfHandleWidth
;
824 leftHandlePos
: leftHandlePos
,
825 rightHandlePos
: rightHandlePos
,
826 isZoomed
: (leftHandlePos
- 1 > this.canvasRect_
.x
|| rightHandlePos
+ 1 < this.canvasRect_
.x
+this.canvasRect_
.w
)
830 return rangeSelector
;