Bug fix for dygraph point selection touch event.
[dygraphs.git] / src / plugins / range-selector.js
1 /**
2 * @license
3 * Copyright 2011 Paul Felix (paul.eric.felix@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
5 */
6 /*global Dygraph:false,TouchEvent:false */
7
8 /**
9 * @fileoverview This file contains the RangeSelector plugin used to provide
10 * a timeline range selector widget for dygraphs.
11 */
12
13 /*global Dygraph:false */
14 "use strict";
15
16 import * as utils from '../dygraph-utils';
17 import DygraphInteraction from '../dygraph-interaction-model';
18 import IFrameTarp from '../iframe-tarp';
19
20 var rangeSelector = function() {
21 this.hasTouchInterface_ = typeof(TouchEvent) != 'undefined';
22 this.isMobileDevice_ = /mobile|android/gi.test(navigator.appVersion);
23 this.interfaceCreated_ = false;
24 };
25
26 rangeSelector.prototype.toString = function() {
27 return "RangeSelector Plugin";
28 };
29
30 rangeSelector.prototype.activate = function(dygraph) {
31 this.dygraph_ = dygraph;
32 if (this.getOption_('showRangeSelector')) {
33 this.createInterface_();
34 }
35 return {
36 layout: this.reserveSpace_,
37 predraw: this.renderStaticLayer_,
38 didDrawChart: this.renderInteractiveLayer_
39 };
40 };
41
42 rangeSelector.prototype.destroy = function() {
43 this.bgcanvas_ = null;
44 this.fgcanvas_ = null;
45 this.leftZoomHandle_ = null;
46 this.rightZoomHandle_ = null;
47 };
48
49 //------------------------------------------------------------------
50 // Private methods
51 //------------------------------------------------------------------
52
53 rangeSelector.prototype.getOption_ = function(name, opt_series) {
54 return this.dygraph_.getOption(name, opt_series);
55 };
56
57 rangeSelector.prototype.setDefaultOption_ = function(name, value) {
58 this.dygraph_.attrs_[name] = value;
59 };
60
61 /**
62 * @private
63 * Creates the range selector elements and adds them to the graph.
64 */
65 rangeSelector.prototype.createInterface_ = function() {
66 this.createCanvases_();
67 this.createZoomHandles_();
68 this.initInteraction_();
69
70 // Range selector and animatedZooms have a bad interaction. See issue 359.
71 if (this.getOption_('animatedZooms')) {
72 console.warn('Animated zooms and range selector are not compatible; disabling animatedZooms.');
73 this.dygraph_.updateOptions({animatedZooms: false}, true);
74 }
75
76 this.interfaceCreated_ = true;
77 this.addToGraph_();
78 };
79
80 /**
81 * @private
82 * Adds the range selector to the graph.
83 */
84 rangeSelector.prototype.addToGraph_ = function() {
85 var graphDiv = this.graphDiv_ = this.dygraph_.graphDiv;
86 graphDiv.appendChild(this.bgcanvas_);
87 graphDiv.appendChild(this.fgcanvas_);
88 graphDiv.appendChild(this.leftZoomHandle_);
89 graphDiv.appendChild(this.rightZoomHandle_);
90 };
91
92 /**
93 * @private
94 * Removes the range selector from the graph.
95 */
96 rangeSelector.prototype.removeFromGraph_ = function() {
97 var graphDiv = this.graphDiv_;
98 graphDiv.removeChild(this.bgcanvas_);
99 graphDiv.removeChild(this.fgcanvas_);
100 graphDiv.removeChild(this.leftZoomHandle_);
101 graphDiv.removeChild(this.rightZoomHandle_);
102 this.graphDiv_ = null;
103 };
104
105 /**
106 * @private
107 * Called by Layout to allow range selector to reserve its space.
108 */
109 rangeSelector.prototype.reserveSpace_ = function(e) {
110 if (this.getOption_('showRangeSelector')) {
111 e.reserveSpaceBottom(this.getOption_('rangeSelectorHeight') + 4);
112 }
113 };
114
115 /**
116 * @private
117 * Renders the static portion of the range selector at the predraw stage.
118 */
119 rangeSelector.prototype.renderStaticLayer_ = function() {
120 if (!this.updateVisibility_()) {
121 return;
122 }
123 this.resize_();
124 this.drawStaticLayer_();
125 };
126
127 /**
128 * @private
129 * Renders the interactive portion of the range selector after the chart has been drawn.
130 */
131 rangeSelector.prototype.renderInteractiveLayer_ = function() {
132 if (!this.updateVisibility_() || this.isChangingRange_) {
133 return;
134 }
135 this.placeZoomHandles_();
136 this.drawInteractiveLayer_();
137 };
138
139 /**
140 * @private
141 * Check to see if the range selector is enabled/disabled and update visibility accordingly.
142 */
143 rangeSelector.prototype.updateVisibility_ = function() {
144 var enabled = this.getOption_('showRangeSelector');
145 if (enabled) {
146 if (!this.interfaceCreated_) {
147 this.createInterface_();
148 } else if (!this.graphDiv_ || !this.graphDiv_.parentNode) {
149 this.addToGraph_();
150 }
151 } else if (this.graphDiv_) {
152 this.removeFromGraph_();
153 var dygraph = this.dygraph_;
154 setTimeout(function() { dygraph.width_ = 0; dygraph.resize(); }, 1);
155 }
156 return enabled;
157 };
158
159 /**
160 * @private
161 * Resizes the range selector.
162 */
163 rangeSelector.prototype.resize_ = function() {
164 function setElementRect(canvas, context, rect, pixelRatioOption) {
165 var canvasScale = pixelRatioOption || utils.getContextPixelRatio(context);
166
167 canvas.style.top = rect.y + 'px';
168 canvas.style.left = rect.x + 'px';
169 canvas.width = rect.w * canvasScale;
170 canvas.height = rect.h * canvasScale;
171 canvas.style.width = rect.w + 'px';
172 canvas.style.height = rect.h + 'px';
173
174 if(canvasScale != 1) {
175 context.scale(canvasScale, canvasScale);
176 }
177 }
178
179 var plotArea = this.dygraph_.layout_.getPlotArea();
180
181 var xAxisLabelHeight = 0;
182 if (this.dygraph_.getOptionForAxis('drawAxis', 'x')) {
183 xAxisLabelHeight = this.getOption_('xAxisHeight') || (this.getOption_('axisLabelFontSize') + 2 * this.getOption_('axisTickSize'));
184 }
185 this.canvasRect_ = {
186 x: plotArea.x,
187 y: plotArea.y + plotArea.h + xAxisLabelHeight + 4,
188 w: plotArea.w,
189 h: this.getOption_('rangeSelectorHeight')
190 };
191
192 var pixelRatioOption = this.dygraph_.getNumericOption('pixelRatio');
193 setElementRect(this.bgcanvas_, this.bgcanvas_ctx_, this.canvasRect_, pixelRatioOption);
194 setElementRect(this.fgcanvas_, this.fgcanvas_ctx_, this.canvasRect_, pixelRatioOption);
195 };
196
197 /**
198 * @private
199 * Creates the background and foreground canvases.
200 */
201 rangeSelector.prototype.createCanvases_ = function() {
202 this.bgcanvas_ = utils.createCanvas();
203 this.bgcanvas_.className = 'dygraph-rangesel-bgcanvas';
204 this.bgcanvas_.style.position = 'absolute';
205 this.bgcanvas_.style.zIndex = 9;
206 this.bgcanvas_ctx_ = utils.getContext(this.bgcanvas_);
207
208 this.fgcanvas_ = utils.createCanvas();
209 this.fgcanvas_.className = 'dygraph-rangesel-fgcanvas';
210 this.fgcanvas_.style.position = 'absolute';
211 this.fgcanvas_.style.zIndex = 9;
212 this.fgcanvas_.style.cursor = 'default';
213 this.fgcanvas_ctx_ = utils.getContext(this.fgcanvas_);
214 };
215
216 /**
217 * @private
218 * Creates the zoom handle elements.
219 */
220 rangeSelector.prototype.createZoomHandles_ = function() {
221 var img = new Image();
222 img.className = 'dygraph-rangesel-zoomhandle';
223 img.style.position = 'absolute';
224 img.style.zIndex = 10;
225 img.style.visibility = 'hidden'; // Initially hidden so they don't show up in the wrong place.
226 img.style.cursor = 'col-resize';
227 // TODO: change image to more options
228 img.width = 9;
229 img.height = 16;
230 img.src = 'data:image/png;base64,' +
231 'iVBORw0KGgoAAAANSUhEUgAAAAkAAAAQCAYAAADESFVDAAAAAXNSR0IArs4c6QAAAAZiS0dEANAA' +
232 'zwDP4Z7KegAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9sHGw0cMqdt1UwAAAAZdEVYdENv' +
233 'bW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAAaElEQVQoz+3SsRFAQBCF4Z9WJM8KCDVwownl' +
234 '6YXsTmCUsyKGkZzcl7zkz3YLkypgAnreFmDEpHkIwVOMfpdi9CEEN2nGpFdwD03yEqDtOgCaun7s' +
235 'qSTDH32I1pQA2Pb9sZecAxc5r3IAb21d6878xsAAAAAASUVORK5CYII=';
236
237 if (this.isMobileDevice_) {
238 img.width *= 2;
239 img.height *= 2;
240 }
241
242 this.leftZoomHandle_ = img;
243 this.rightZoomHandle_ = img.cloneNode(false);
244 };
245
246 /**
247 * @private
248 * Sets up the interaction for the range selector.
249 */
250 rangeSelector.prototype.initInteraction_ = function() {
251 var self = this;
252 var topElem = document;
253 var clientXLast = 0;
254 var handle = null;
255 var isZooming = false;
256 var isPanning = false;
257 var dynamic = !this.isMobileDevice_;
258
259 // We cover iframes during mouse interactions. See comments in
260 // dygraph-utils.js for more info on why this is a good idea.
261 var tarp = new IFrameTarp();
262
263 // functions, defined below. Defining them this way (rather than with
264 // "function foo() {...}" makes JSHint happy.
265 var toXDataWindow, onZoomStart, onZoom, onZoomEnd, doZoom, isMouseInPanZone,
266 onPanStart, onPan, onPanEnd, doPan, onCanvasHover;
267
268 // Touch event functions
269 var onZoomHandleTouchEvent, onCanvasTouchEvent, addTouchEvents;
270
271 toXDataWindow = function(zoomHandleStatus) {
272 var xDataLimits = self.dygraph_.xAxisExtremes();
273 var fact = (xDataLimits[1] - xDataLimits[0])/self.canvasRect_.w;
274 var xDataMin = xDataLimits[0] + (zoomHandleStatus.leftHandlePos - self.canvasRect_.x)*fact;
275 var xDataMax = xDataLimits[0] + (zoomHandleStatus.rightHandlePos - self.canvasRect_.x)*fact;
276 return [xDataMin, xDataMax];
277 };
278
279 onZoomStart = function(e) {
280 utils.cancelEvent(e);
281 isZooming = true;
282 clientXLast = e.clientX;
283 handle = e.target ? e.target : e.srcElement;
284 if (e.type === 'mousedown' || e.type === 'dragstart') {
285 // These events are removed manually.
286 utils.addEvent(topElem, 'mousemove', onZoom);
287 utils.addEvent(topElem, 'mouseup', onZoomEnd);
288 }
289 self.fgcanvas_.style.cursor = 'col-resize';
290 tarp.cover();
291 return true;
292 };
293
294 onZoom = function(e) {
295 if (!isZooming) {
296 return false;
297 }
298 utils.cancelEvent(e);
299
300 var delX = e.clientX - clientXLast;
301 if (Math.abs(delX) < 4) {
302 return true;
303 }
304 clientXLast = e.clientX;
305
306 // Move handle.
307 var zoomHandleStatus = self.getZoomHandleStatus_();
308 var newPos;
309 if (handle == self.leftZoomHandle_) {
310 newPos = zoomHandleStatus.leftHandlePos + delX;
311 newPos = Math.min(newPos, zoomHandleStatus.rightHandlePos - handle.width - 3);
312 newPos = Math.max(newPos, self.canvasRect_.x);
313 } else {
314 newPos = zoomHandleStatus.rightHandlePos + delX;
315 newPos = Math.min(newPos, self.canvasRect_.x + self.canvasRect_.w);
316 newPos = Math.max(newPos, zoomHandleStatus.leftHandlePos + handle.width + 3);
317 }
318 var halfHandleWidth = handle.width/2;
319 handle.style.left = (newPos - halfHandleWidth) + 'px';
320 self.drawInteractiveLayer_();
321
322 // Zoom on the fly.
323 if (dynamic) {
324 doZoom();
325 }
326 return true;
327 };
328
329 onZoomEnd = function(e) {
330 if (!isZooming) {
331 return false;
332 }
333 isZooming = false;
334 tarp.uncover();
335 utils.removeEvent(topElem, 'mousemove', onZoom);
336 utils.removeEvent(topElem, 'mouseup', onZoomEnd);
337 self.fgcanvas_.style.cursor = 'default';
338
339 // If on a slower device, zoom now.
340 if (!dynamic) {
341 doZoom();
342 }
343 return true;
344 };
345
346 doZoom = function() {
347 try {
348 var zoomHandleStatus = self.getZoomHandleStatus_();
349 self.isChangingRange_ = true;
350 if (!zoomHandleStatus.isZoomed) {
351 self.dygraph_.resetZoom();
352 } else {
353 var xDataWindow = toXDataWindow(zoomHandleStatus);
354 self.dygraph_.doZoomXDates_(xDataWindow[0], xDataWindow[1]);
355 }
356 } finally {
357 self.isChangingRange_ = false;
358 }
359 };
360
361 isMouseInPanZone = function(e) {
362 var rect = self.leftZoomHandle_.getBoundingClientRect();
363 var leftHandleClientX = rect.left + rect.width/2;
364 rect = self.rightZoomHandle_.getBoundingClientRect();
365 var rightHandleClientX = rect.left + rect.width/2;
366 return (e.clientX > leftHandleClientX && e.clientX < rightHandleClientX);
367 };
368
369 onPanStart = function(e) {
370 if (!isPanning && isMouseInPanZone(e) && self.getZoomHandleStatus_().isZoomed) {
371 utils.cancelEvent(e);
372 isPanning = true;
373 clientXLast = e.clientX;
374 if (e.type === 'mousedown') {
375 // These events are removed manually.
376 utils.addEvent(topElem, 'mousemove', onPan);
377 utils.addEvent(topElem, 'mouseup', onPanEnd);
378 }
379 return true;
380 }
381 return false;
382 };
383
384 onPan = function(e) {
385 if (!isPanning) {
386 return false;
387 }
388 utils.cancelEvent(e);
389
390 var delX = e.clientX - clientXLast;
391 if (Math.abs(delX) < 4) {
392 return true;
393 }
394 clientXLast = e.clientX;
395
396 // Move range view
397 var zoomHandleStatus = self.getZoomHandleStatus_();
398 var leftHandlePos = zoomHandleStatus.leftHandlePos;
399 var rightHandlePos = zoomHandleStatus.rightHandlePos;
400 var rangeSize = rightHandlePos - leftHandlePos;
401 if (leftHandlePos + delX <= self.canvasRect_.x) {
402 leftHandlePos = self.canvasRect_.x;
403 rightHandlePos = leftHandlePos + rangeSize;
404 } else if (rightHandlePos + delX >= self.canvasRect_.x + self.canvasRect_.w) {
405 rightHandlePos = self.canvasRect_.x + self.canvasRect_.w;
406 leftHandlePos = rightHandlePos - rangeSize;
407 } else {
408 leftHandlePos += delX;
409 rightHandlePos += delX;
410 }
411 var halfHandleWidth = self.leftZoomHandle_.width/2;
412 self.leftZoomHandle_.style.left = (leftHandlePos - halfHandleWidth) + 'px';
413 self.rightZoomHandle_.style.left = (rightHandlePos - halfHandleWidth) + 'px';
414 self.drawInteractiveLayer_();
415
416 // Do pan on the fly.
417 if (dynamic) {
418 doPan();
419 }
420 return true;
421 };
422
423 onPanEnd = function(e) {
424 if (!isPanning) {
425 return false;
426 }
427 isPanning = false;
428 utils.removeEvent(topElem, 'mousemove', onPan);
429 utils.removeEvent(topElem, 'mouseup', onPanEnd);
430 // If on a slower device, do pan now.
431 if (!dynamic) {
432 doPan();
433 }
434 return true;
435 };
436
437 doPan = function() {
438 try {
439 self.isChangingRange_ = true;
440 self.dygraph_.dateWindow_ = toXDataWindow(self.getZoomHandleStatus_());
441 self.dygraph_.drawGraph_(false);
442 } finally {
443 self.isChangingRange_ = false;
444 }
445 };
446
447 onCanvasHover = function(e) {
448 if (isZooming || isPanning) {
449 return;
450 }
451 var cursor = isMouseInPanZone(e) ? 'move' : 'default';
452 if (cursor != self.fgcanvas_.style.cursor) {
453 self.fgcanvas_.style.cursor = cursor;
454 }
455 };
456
457 onZoomHandleTouchEvent = function(e) {
458 if (e.type == 'touchstart' && e.targetTouches.length == 1) {
459 if (onZoomStart(e.targetTouches[0])) {
460 utils.cancelEvent(e);
461 }
462 } else if (e.type == 'touchmove' && e.targetTouches.length == 1) {
463 if (onZoom(e.targetTouches[0])) {
464 utils.cancelEvent(e);
465 }
466 } else {
467 onZoomEnd(e);
468 }
469 };
470
471 onCanvasTouchEvent = function(e) {
472 if (e.type == 'touchstart' && e.targetTouches.length == 1) {
473 if (onPanStart(e.targetTouches[0])) {
474 utils.cancelEvent(e);
475 }
476 } else if (e.type == 'touchmove' && e.targetTouches.length == 1) {
477 if (onPan(e.targetTouches[0])) {
478 utils.cancelEvent(e);
479 }
480 } else {
481 onPanEnd(e);
482 }
483 };
484
485 addTouchEvents = function(elem, fn) {
486 var types = ['touchstart', 'touchend', 'touchmove', 'touchcancel'];
487 for (var i = 0; i < types.length; i++) {
488 self.dygraph_.addAndTrackEvent(elem, types[i], fn);
489 }
490 };
491
492 this.setDefaultOption_('interactionModel', DygraphInteraction.dragIsPanInteractionModel);
493 this.setDefaultOption_('panEdgeFraction', 0.0001);
494
495 var dragStartEvent = window.opera ? 'mousedown' : 'dragstart';
496 this.dygraph_.addAndTrackEvent(this.leftZoomHandle_, dragStartEvent, onZoomStart);
497 this.dygraph_.addAndTrackEvent(this.rightZoomHandle_, dragStartEvent, onZoomStart);
498
499 this.dygraph_.addAndTrackEvent(this.fgcanvas_, 'mousedown', onPanStart);
500 this.dygraph_.addAndTrackEvent(this.fgcanvas_, 'mousemove', onCanvasHover);
501
502 // Touch events
503 if (this.hasTouchInterface_) {
504 addTouchEvents(this.leftZoomHandle_, onZoomHandleTouchEvent);
505 addTouchEvents(this.rightZoomHandle_, onZoomHandleTouchEvent);
506 addTouchEvents(this.fgcanvas_, onCanvasTouchEvent);
507 }
508 };
509
510 /**
511 * @private
512 * Draws the static layer in the background canvas.
513 */
514 rangeSelector.prototype.drawStaticLayer_ = function() {
515 var ctx = this.bgcanvas_ctx_;
516 ctx.clearRect(0, 0, this.canvasRect_.w, this.canvasRect_.h);
517 try {
518 this.drawMiniPlot_();
519 } catch(ex) {
520 console.warn(ex);
521 }
522
523 var margin = 0.5;
524 this.bgcanvas_ctx_.lineWidth = this.getOption_('rangeSelectorBackgroundLineWidth');
525 ctx.strokeStyle = this.getOption_('rangeSelectorBackgroundStrokeColor');
526 ctx.beginPath();
527 ctx.moveTo(margin, margin);
528 ctx.lineTo(margin, this.canvasRect_.h-margin);
529 ctx.lineTo(this.canvasRect_.w-margin, this.canvasRect_.h-margin);
530 ctx.lineTo(this.canvasRect_.w-margin, margin);
531 ctx.stroke();
532 };
533
534
535 /**
536 * @private
537 * Draws the mini plot in the background canvas.
538 */
539 rangeSelector.prototype.drawMiniPlot_ = function() {
540 var fillStyle = this.getOption_('rangeSelectorPlotFillColor');
541 var fillGradientStyle = this.getOption_('rangeSelectorPlotFillGradientColor');
542 var strokeStyle = this.getOption_('rangeSelectorPlotStrokeColor');
543 if (!fillStyle && !strokeStyle) {
544 return;
545 }
546
547 var stepPlot = this.getOption_('stepPlot');
548
549 var combinedSeriesData = this.computeCombinedSeriesAndLimits_();
550 var yRange = combinedSeriesData.yMax - combinedSeriesData.yMin;
551
552 // Draw the mini plot.
553 var ctx = this.bgcanvas_ctx_;
554 var margin = 0.5;
555
556 var xExtremes = this.dygraph_.xAxisExtremes();
557 var xRange = Math.max(xExtremes[1] - xExtremes[0], 1.e-30);
558 var xFact = (this.canvasRect_.w - margin)/xRange;
559 var yFact = (this.canvasRect_.h - margin)/yRange;
560 var canvasWidth = this.canvasRect_.w - margin;
561 var canvasHeight = this.canvasRect_.h - margin;
562
563 var prevX = null, prevY = null;
564
565 ctx.beginPath();
566 ctx.moveTo(margin, canvasHeight);
567 for (var i = 0; i < combinedSeriesData.data.length; i++) {
568 var dataPoint = combinedSeriesData.data[i];
569 var x = ((dataPoint[0] !== null) ? ((dataPoint[0] - xExtremes[0])*xFact) : NaN);
570 var y = ((dataPoint[1] !== null) ? (canvasHeight - (dataPoint[1] - combinedSeriesData.yMin)*yFact) : NaN);
571
572 // Skip points that don't change the x-value. Overly fine-grained points
573 // can cause major slowdowns with the ctx.fill() call below.
574 if (!stepPlot && prevX !== null && Math.round(x) == Math.round(prevX)) {
575 continue;
576 }
577
578 if (isFinite(x) && isFinite(y)) {
579 if(prevX === null) {
580 ctx.lineTo(x, canvasHeight);
581 }
582 else if (stepPlot) {
583 ctx.lineTo(x, prevY);
584 }
585 ctx.lineTo(x, y);
586 prevX = x;
587 prevY = y;
588 }
589 else {
590 if(prevX !== null) {
591 if (stepPlot) {
592 ctx.lineTo(x, prevY);
593 ctx.lineTo(x, canvasHeight);
594 }
595 else {
596 ctx.lineTo(prevX, canvasHeight);
597 }
598 }
599 prevX = prevY = null;
600 }
601 }
602 ctx.lineTo(canvasWidth, canvasHeight);
603 ctx.closePath();
604
605 if (fillStyle) {
606 var lingrad = this.bgcanvas_ctx_.createLinearGradient(0, 0, 0, canvasHeight);
607 if (fillGradientStyle) {
608 lingrad.addColorStop(0, fillGradientStyle);
609 }
610 lingrad.addColorStop(1, fillStyle);
611 this.bgcanvas_ctx_.fillStyle = lingrad;
612 ctx.fill();
613 }
614
615 if (strokeStyle) {
616 this.bgcanvas_ctx_.strokeStyle = strokeStyle;
617 this.bgcanvas_ctx_.lineWidth = this.getOption_('rangeSelectorPlotLineWidth');
618 ctx.stroke();
619 }
620 };
621
622 /**
623 * @private
624 * Computes and returns the combined series data along with min/max for the mini plot.
625 * The combined series consists of averaged values for all series.
626 * When series have error bars, the error bars are ignored.
627 * @return {Object} An object containing combined series array, ymin, ymax.
628 */
629 rangeSelector.prototype.computeCombinedSeriesAndLimits_ = function() {
630 var g = this.dygraph_;
631 var logscale = this.getOption_('logscale');
632 var i;
633
634 // Select series to combine. By default, all series are combined.
635 var numColumns = g.numColumns();
636 var labels = g.getLabels();
637 var includeSeries = new Array(numColumns);
638 var anySet = false;
639 var visibility = g.visibility();
640 var inclusion = [];
641
642 for (i = 1; i < numColumns; i++) {
643 var include = this.getOption_('showInRangeSelector', labels[i]);
644 inclusion.push(include);
645 if (include !== null) anySet = true; // it's set explicitly for this series
646 }
647
648 if (anySet) {
649 for (i = 1; i < numColumns; i++) {
650 includeSeries[i] = inclusion[i - 1];
651 }
652 } else {
653 for (i = 1; i < numColumns; i++) {
654 includeSeries[i] = visibility[i - 1];
655 }
656 }
657
658 // Create a combined series (average of selected series values).
659 // TODO(danvk): short-circuit if there's only one series.
660 var rolledSeries = [];
661 var dataHandler = g.dataHandler_;
662 var options = g.attributes_;
663 for (i = 1; i < g.numColumns(); i++) {
664 if (!includeSeries[i]) continue;
665 var series = dataHandler.extractSeries(g.rawData_, i, options);
666 if (g.rollPeriod() > 1) {
667 series = dataHandler.rollingAverage(series, g.rollPeriod(), options);
668 }
669
670 rolledSeries.push(series);
671 }
672
673 var combinedSeries = [];
674 for (i = 0; i < rolledSeries[0].length; i++) {
675 var sum = 0;
676 var count = 0;
677 for (var j = 0; j < rolledSeries.length; j++) {
678 var y = rolledSeries[j][i][1];
679 if (y === null || isNaN(y)) continue;
680 count++;
681 sum += y;
682 }
683 combinedSeries.push([rolledSeries[0][i][0], sum / count]);
684 }
685
686 // Compute the y range.
687 var yMin = Number.MAX_VALUE;
688 var yMax = -Number.MAX_VALUE;
689 for (i = 0; i < combinedSeries.length; i++) {
690 var yVal = combinedSeries[i][1];
691 if (yVal !== null && isFinite(yVal) && (!logscale || yVal > 0)) {
692 yMin = Math.min(yMin, yVal);
693 yMax = Math.max(yMax, yVal);
694 }
695 }
696
697 // Convert Y data to log scale if needed.
698 // Also, expand the Y range to compress the mini plot a little.
699 var extraPercent = 0.25;
700 if (logscale) {
701 yMax = utils.log10(yMax);
702 yMax += yMax*extraPercent;
703 yMin = utils.log10(yMin);
704 for (i = 0; i < combinedSeries.length; i++) {
705 combinedSeries[i][1] = utils.log10(combinedSeries[i][1]);
706 }
707 } else {
708 var yExtra;
709 var yRange = yMax - yMin;
710 if (yRange <= Number.MIN_VALUE) {
711 yExtra = yMax*extraPercent;
712 } else {
713 yExtra = yRange*extraPercent;
714 }
715 yMax += yExtra;
716 yMin -= yExtra;
717 }
718
719 return {data: combinedSeries, yMin: yMin, yMax: yMax};
720 };
721
722 /**
723 * @private
724 * Places the zoom handles in the proper position based on the current X data window.
725 */
726 rangeSelector.prototype.placeZoomHandles_ = function() {
727 var xExtremes = this.dygraph_.xAxisExtremes();
728 var xWindowLimits = this.dygraph_.xAxisRange();
729 var xRange = xExtremes[1] - xExtremes[0];
730 var leftPercent = Math.max(0, (xWindowLimits[0] - xExtremes[0])/xRange);
731 var rightPercent = Math.max(0, (xExtremes[1] - xWindowLimits[1])/xRange);
732 var leftCoord = this.canvasRect_.x + this.canvasRect_.w*leftPercent;
733 var rightCoord = this.canvasRect_.x + this.canvasRect_.w*(1 - rightPercent);
734 var handleTop = Math.max(this.canvasRect_.y, this.canvasRect_.y + (this.canvasRect_.h - this.leftZoomHandle_.height)/2);
735 var halfHandleWidth = this.leftZoomHandle_.width/2;
736 this.leftZoomHandle_.style.left = (leftCoord - halfHandleWidth) + 'px';
737 this.leftZoomHandle_.style.top = handleTop + 'px';
738 this.rightZoomHandle_.style.left = (rightCoord - halfHandleWidth) + 'px';
739 this.rightZoomHandle_.style.top = this.leftZoomHandle_.style.top;
740
741 this.leftZoomHandle_.style.visibility = 'visible';
742 this.rightZoomHandle_.style.visibility = 'visible';
743 };
744
745 /**
746 * @private
747 * Draws the interactive layer in the foreground canvas.
748 */
749 rangeSelector.prototype.drawInteractiveLayer_ = function() {
750 var ctx = this.fgcanvas_ctx_;
751 ctx.clearRect(0, 0, this.canvasRect_.w, this.canvasRect_.h);
752 var margin = 1;
753 var width = this.canvasRect_.w - margin;
754 var height = this.canvasRect_.h - margin;
755 var zoomHandleStatus = this.getZoomHandleStatus_();
756
757 ctx.strokeStyle = this.getOption_('rangeSelectorForegroundStrokeColor');
758 ctx.lineWidth = this.getOption_('rangeSelectorForegroundLineWidth');
759 if (!zoomHandleStatus.isZoomed) {
760 ctx.beginPath();
761 ctx.moveTo(margin, margin);
762 ctx.lineTo(margin, height);
763 ctx.lineTo(width, height);
764 ctx.lineTo(width, margin);
765 ctx.stroke();
766 } else {
767 var leftHandleCanvasPos = Math.max(margin, zoomHandleStatus.leftHandlePos - this.canvasRect_.x);
768 var rightHandleCanvasPos = Math.min(width, zoomHandleStatus.rightHandlePos - this.canvasRect_.x);
769
770 ctx.fillStyle = 'rgba(240, 240, 240, ' + this.getOption_('rangeSelectorAlpha').toString() + ')';
771 ctx.fillRect(0, 0, leftHandleCanvasPos, this.canvasRect_.h);
772 ctx.fillRect(rightHandleCanvasPos, 0, this.canvasRect_.w - rightHandleCanvasPos, this.canvasRect_.h);
773
774 ctx.beginPath();
775 ctx.moveTo(margin, margin);
776 ctx.lineTo(leftHandleCanvasPos, margin);
777 ctx.lineTo(leftHandleCanvasPos, height);
778 ctx.lineTo(rightHandleCanvasPos, height);
779 ctx.lineTo(rightHandleCanvasPos, margin);
780 ctx.lineTo(width, margin);
781 ctx.stroke();
782 }
783 };
784
785 /**
786 * @private
787 * Returns the current zoom handle position information.
788 * @return {Object} The zoom handle status.
789 */
790 rangeSelector.prototype.getZoomHandleStatus_ = function() {
791 var halfHandleWidth = this.leftZoomHandle_.width/2;
792 var leftHandlePos = parseFloat(this.leftZoomHandle_.style.left) + halfHandleWidth;
793 var rightHandlePos = parseFloat(this.rightZoomHandle_.style.left) + halfHandleWidth;
794 return {
795 leftHandlePos: leftHandlePos,
796 rightHandlePos: rightHandlePos,
797 isZoomed: (leftHandlePos - 1 > this.canvasRect_.x || rightHandlePos + 1 < this.canvasRect_.x+this.canvasRect_.w)
798 };
799 };
800
801 export default rangeSelector;