fix all lint errors
[dygraphs.git] / dygraph-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
7 /**
8 * @fileoverview This file contains the DygraphRangeSelector class used to provide
9 * a timeline range selector widget for dygraphs.
10 */
11
12 /*jshint globalstrict: true */
13 /*global Dygraph:false */
14 "use strict";
15
16 /**
17 * The DygraphRangeSelector class provides a timeline range selector widget.
18 * @param {Dygraph} dygraph The dygraph object
19 * @constructor
20 */
21 var DygraphRangeSelector = function(dygraph) {
22 this.isIE_ = /MSIE/.test(navigator.userAgent) && !window.opera;
23 this.isUsingExcanvas_ = dygraph.isUsingExcanvas_;
24 this.dygraph_ = dygraph;
25 this.hasTouchInterface_ = typeof(TouchEvent) != 'undefined';
26 this.isMobileDevice_ = /mobile|android/gi.test(navigator.appVersion);
27 this.createCanvases_();
28 if (this.isUsingExcanvas_) {
29 this.createIEPanOverlay_();
30 }
31 this.createZoomHandles_();
32 this.initInteraction_();
33 };
34
35 /**
36 * Adds the range selector to the dygraph.
37 * @param {Object} graphDiv The container div for the range selector.
38 * @param {DygraphLayout} layout The DygraphLayout object for this graph.
39 */
40 DygraphRangeSelector.prototype.addToGraph = function(graphDiv, layout) {
41 this.layout_ = layout;
42 this.resize_();
43 graphDiv.appendChild(this.bgcanvas_);
44 graphDiv.appendChild(this.fgcanvas_);
45 graphDiv.appendChild(this.leftZoomHandle_);
46 graphDiv.appendChild(this.rightZoomHandle_);
47 };
48
49 /**
50 * Renders the static background portion of the range selector.
51 */
52 DygraphRangeSelector.prototype.renderStaticLayer = function() {
53 this.resize_();
54 this.drawStaticLayer_();
55 };
56
57 /**
58 * Renders the interactive foreground portion of the range selector.
59 */
60 DygraphRangeSelector.prototype.renderInteractiveLayer = function() {
61 if (this.isChangingRange_) {
62 return;
63 }
64 this.placeZoomHandles_();
65 this.drawInteractiveLayer_();
66 };
67
68 /**
69 * @private
70 * Resizes the range selector.
71 */
72 DygraphRangeSelector.prototype.resize_ = function() {
73 function setElementRect(canvas, rect) {
74 canvas.style.top = rect.y + 'px';
75 canvas.style.left = rect.x + 'px';
76 canvas.width = rect.w;
77 canvas.height = rect.h;
78 canvas.style.width = canvas.width + 'px'; // for IE
79 canvas.style.height = canvas.height + 'px'; // for IE
80 }
81
82 var plotArea = this.layout_.getPlotArea();
83 var xAxisLabelHeight = this.attr_('xAxisHeight') || (this.attr_('axisLabelFontSize') + 2 * this.attr_('axisTickSize'));
84 this.canvasRect_ = {
85 x: plotArea.x,
86 y: plotArea.y + plotArea.h + xAxisLabelHeight + 4,
87 w: plotArea.w,
88 h: this.attr_('rangeSelectorHeight')
89 };
90
91 setElementRect(this.bgcanvas_, this.canvasRect_);
92 setElementRect(this.fgcanvas_, this.canvasRect_);
93 };
94
95 DygraphRangeSelector.prototype.attr_ = function(name) {
96 return this.dygraph_.attr_(name);
97 };
98
99 /**
100 * @private
101 * Creates the background and foreground canvases.
102 */
103 DygraphRangeSelector.prototype.createCanvases_ = function() {
104 this.bgcanvas_ = Dygraph.createCanvas();
105 this.bgcanvas_.className = 'dygraph-rangesel-bgcanvas';
106 this.bgcanvas_.style.position = 'absolute';
107 this.bgcanvas_.style.zIndex = 9;
108 this.bgcanvas_ctx_ = Dygraph.getContext(this.bgcanvas_);
109
110 this.fgcanvas_ = Dygraph.createCanvas();
111 this.fgcanvas_.className = 'dygraph-rangesel-fgcanvas';
112 this.fgcanvas_.style.position = 'absolute';
113 this.fgcanvas_.style.zIndex = 9;
114 this.fgcanvas_.style.cursor = 'default';
115 this.fgcanvas_ctx_ = Dygraph.getContext(this.fgcanvas_);
116 };
117
118 /**
119 * @private
120 * Creates overlay divs for IE/Excanvas so that mouse events are handled properly.
121 */
122 DygraphRangeSelector.prototype.createIEPanOverlay_ = function() {
123 this.iePanOverlay_ = document.createElement("div");
124 this.iePanOverlay_.style.position = 'absolute';
125 this.iePanOverlay_.style.backgroundColor = 'white';
126 this.iePanOverlay_.style.filter = 'alpha(opacity=0)';
127 this.iePanOverlay_.style.display = 'none';
128 this.iePanOverlay_.style.cursor = 'move';
129 this.fgcanvas_.appendChild(this.iePanOverlay_);
130 };
131
132 /**
133 * @private
134 * Creates the zoom handle elements.
135 */
136 DygraphRangeSelector.prototype.createZoomHandles_ = function() {
137 var img = new Image();
138 img.className = 'dygraph-rangesel-zoomhandle';
139 img.style.position = 'absolute';
140 img.style.zIndex = 10;
141 img.style.visibility = 'hidden'; // Initially hidden so they don't show up in the wrong place.
142 img.style.cursor = 'col-resize';
143
144 if (/MSIE 7/.test(navigator.userAgent)) { // IE7 doesn't support embedded src data.
145 img.width = 7;
146 img.height = 14;
147 img.style.backgroundColor = 'white';
148 img.style.border = '1px solid #333333'; // Just show box in IE7.
149 } else {
150 img.width = 9;
151 img.height = 16;
152 img.src = 'data:image/png;base64,' +
153 'iVBORw0KGgoAAAANSUhEUgAAAAkAAAAQCAYAAADESFVDAAAAAXNSR0IArs4c6QAAAAZiS0dEANAA' +
154 'zwDP4Z7KegAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9sHGw0cMqdt1UwAAAAZdEVYdENv' +
155 'bW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAAaElEQVQoz+3SsRFAQBCF4Z9WJM8KCDVwownl' +
156 '6YXsTmCUsyKGkZzcl7zkz3YLkypgAnreFmDEpHkIwVOMfpdi9CEEN2nGpFdwD03yEqDtOgCaun7s' +
157 'qSTDH32I1pQA2Pb9sZecAxc5r3IAb21d6878xsAAAAAASUVORK5CYII=';
158 }
159
160 if (this.isMobileDevice_) {
161 img.width *= 2;
162 img.height *= 2;
163 }
164
165 this.leftZoomHandle_ = img;
166 this.rightZoomHandle_ = img.cloneNode(false);
167 };
168
169 /**
170 * @private
171 * Sets up the interaction for the range selector.
172 */
173 DygraphRangeSelector.prototype.initInteraction_ = function() {
174 var self = this;
175 var topElem = this.isIE_ ? document : window;
176 var xLast = 0;
177 var handle = null;
178 var isZooming = false;
179 var isPanning = false;
180 var dynamic = !this.isMobileDevice_ && !this.isUsingExcanvas_;
181
182 // functions, defined below. Defining them this way (rather than with
183 // "function foo() {...}" makes JSHint happy.
184 var toXDataWindow, onZoomStart, onZoom, onZoomEnd, doZoom, isMouseInPanZone,
185 onPanStart, onPan, onPanEnd, doPan, onCanvasMouseMove, applyBrowserZoomLevel;
186
187 // Touch event functions
188 var onZoomHandleTouchEvent, onCanvasTouchEvent, addTouchEvents;
189
190 toXDataWindow = function(zoomHandleStatus) {
191 var xDataLimits = self.dygraph_.xAxisExtremes();
192 var fact = (xDataLimits[1] - xDataLimits[0])/self.canvasRect_.w;
193 var xDataMin = xDataLimits[0] + (zoomHandleStatus.leftHandlePos - self.canvasRect_.x)*fact;
194 var xDataMax = xDataLimits[0] + (zoomHandleStatus.rightHandlePos - self.canvasRect_.x)*fact;
195 return [xDataMin, xDataMax];
196 };
197
198 applyBrowserZoomLevel = function(delX) {
199 var zoom = window.outerWidth/document.documentElement.clientWidth;
200 if (!isNaN(zoom)) {
201 return delX/zoom;
202 } else {
203 return delX;
204 }
205 };
206
207 onZoomStart = function(e) {
208 Dygraph.cancelEvent(e);
209 isZooming = true;
210 xLast = e.screenX;
211 handle = e.target ? e.target : e.srcElement;
212 self.dygraph_.addEvent(topElem, 'mousemove', onZoom);
213 self.dygraph_.addEvent(topElem, 'mouseup', onZoomEnd);
214 self.fgcanvas_.style.cursor = 'col-resize';
215 return true;
216 };
217
218 onZoom = function(e) {
219 if (!isZooming) {
220 return false;
221 }
222 Dygraph.cancelEvent(e);
223 var delX = e.screenX - xLast;
224 if (Math.abs(delX) < 4 || e.screenX === 0) {
225 // First iPad move event seems to have screenX = 0
226 return true;
227 }
228 xLast = e.screenX;
229 delX = applyBrowserZoomLevel(delX);
230
231 // Move handle.
232 var zoomHandleStatus = self.getZoomHandleStatus_();
233 var newPos;
234 if (handle == self.leftZoomHandle_) {
235 newPos = zoomHandleStatus.leftHandlePos + delX;
236 newPos = Math.min(newPos, zoomHandleStatus.rightHandlePos - handle.width - 3);
237 newPos = Math.max(newPos, self.canvasRect_.x);
238 } else {
239 newPos = zoomHandleStatus.rightHandlePos + delX;
240 newPos = Math.min(newPos, self.canvasRect_.x + self.canvasRect_.w);
241 newPos = Math.max(newPos, zoomHandleStatus.leftHandlePos + handle.width + 3);
242 }
243 var halfHandleWidth = handle.width/2;
244 handle.style.left = (newPos - halfHandleWidth) + 'px';
245 self.drawInteractiveLayer_();
246
247 // Zoom on the fly (if not using excanvas).
248 if (dynamic) {
249 doZoom();
250 }
251 return true;
252 };
253
254 onZoomEnd = function(e) {
255 if (!isZooming) {
256 return false;
257 }
258 isZooming = false;
259 Dygraph.removeEvent(topElem, 'mousemove', onZoom);
260 Dygraph.removeEvent(topElem, 'mouseup', onZoomEnd);
261 self.fgcanvas_.style.cursor = 'default';
262
263 // If using excanvas, Zoom now.
264 if (!dynamic) {
265 doZoom();
266 }
267 return true;
268 };
269
270 doZoom = function() {
271 try {
272 var zoomHandleStatus = self.getZoomHandleStatus_();
273 self.isChangingRange_ = true;
274 if (!zoomHandleStatus.isZoomed) {
275 self.dygraph_.doUnzoom_();
276 } else {
277 var xDataWindow = toXDataWindow(zoomHandleStatus);
278 self.dygraph_.doZoomXDates_(xDataWindow[0], xDataWindow[1]);
279 }
280 } finally {
281 self.isChangingRange_ = false;
282 }
283 };
284
285 isMouseInPanZone = function(e) {
286 if (self.isUsingExcanvas_) {
287 return e.srcElement == self.iePanOverlay_;
288 } else {
289 var rect = self.leftZoomHandle_.getBoundingClientRect();
290 var leftHandleClientX = rect.left + rect.width/2;
291 rect = self.rightZoomHandle_.getBoundingClientRect();
292 var rightHandleClientX = rect.left + rect.width/2;
293 return (e.clientX > leftHandleClientX && e.clientX < rightHandleClientX);
294 }
295 };
296
297 onPanStart = function(e) {
298 if (!isPanning && isMouseInPanZone(e) && self.getZoomHandleStatus_().isZoomed) {
299 Dygraph.cancelEvent(e);
300 isPanning = true;
301 xLast = e.screenX;
302 self.dygraph_.addEvent(topElem, 'mousemove', onPan);
303 self.dygraph_.addEvent(topElem, 'mouseup', onPanEnd);
304 return true;
305 }
306 return false;
307 };
308
309 onPan = function(e) {
310 if (!isPanning) {
311 return false;
312 }
313 Dygraph.cancelEvent(e);
314
315 var delX = e.screenX - xLast;
316 if (Math.abs(delX) < 4) {
317 return true;
318 }
319 xLast = e.screenX;
320 delX = applyBrowserZoomLevel(delX);
321
322 // Move range view
323 var zoomHandleStatus = self.getZoomHandleStatus_();
324 var leftHandlePos = zoomHandleStatus.leftHandlePos;
325 var rightHandlePos = zoomHandleStatus.rightHandlePos;
326 var rangeSize = rightHandlePos - leftHandlePos;
327 if (leftHandlePos + delX <= self.canvasRect_.x) {
328 leftHandlePos = self.canvasRect_.x;
329 rightHandlePos = leftHandlePos + rangeSize;
330 } else if (rightHandlePos + delX >= self.canvasRect_.x + self.canvasRect_.w) {
331 rightHandlePos = self.canvasRect_.x + self.canvasRect_.w;
332 leftHandlePos = rightHandlePos - rangeSize;
333 } else {
334 leftHandlePos += delX;
335 rightHandlePos += delX;
336 }
337 var halfHandleWidth = self.leftZoomHandle_.width/2;
338 self.leftZoomHandle_.style.left = (leftHandlePos - halfHandleWidth) + 'px';
339 self.rightZoomHandle_.style.left = (rightHandlePos - halfHandleWidth) + 'px';
340 self.drawInteractiveLayer_();
341
342 // Do pan on the fly (if not using excanvas).
343 if (dynamic) {
344 doPan();
345 }
346 return true;
347 };
348
349 onPanEnd = function(e) {
350 if (!isPanning) {
351 return false;
352 }
353 isPanning = false;
354 Dygraph.removeEvent(topElem, 'mousemove', onPan);
355 Dygraph.removeEvent(topElem, 'mouseup', onPanEnd);
356 // If using excanvas, do pan now.
357 if (!dynamic) {
358 doPan();
359 }
360 return true;
361 };
362
363 doPan = function() {
364 try {
365 self.isChangingRange_ = true;
366 self.dygraph_.dateWindow_ = toXDataWindow(self.getZoomHandleStatus_());
367 self.dygraph_.drawGraph_(false);
368 } finally {
369 self.isChangingRange_ = false;
370 }
371 };
372
373 onCanvasMouseMove = function(e) {
374 if (isZooming || isPanning) {
375 return;
376 }
377 var cursor = isMouseInPanZone(e) ? 'move' : 'default';
378 if (cursor != self.fgcanvas_.style.cursor) {
379 self.fgcanvas_.style.cursor = cursor;
380 }
381 };
382
383 onZoomHandleTouchEvent = function(e) {
384 if (e.type == 'touchstart' && e.targetTouches.length == 1) {
385 if (onZoomStart(e.targetTouches[0])) {
386 Dygraph.cancelEvent(e);
387 }
388 } else if (e.type == 'touchmove' && e.targetTouches.length == 1) {
389 if (onZoom(e.targetTouches[0])) {
390 Dygraph.cancelEvent(e);
391 }
392 } else {
393 onZoomEnd(e);
394 }
395 };
396
397 onCanvasTouchEvent = function(e) {
398 if (e.type == 'touchstart' && e.targetTouches.length == 1) {
399 if (onPanStart(e.targetTouches[0])) {
400 Dygraph.cancelEvent(e);
401 }
402 } else if (e.type == 'touchmove' && e.targetTouches.length == 1) {
403 if (onPan(e.targetTouches[0])) {
404 Dygraph.cancelEvent(e);
405 }
406 } else {
407 onPanEnd(e);
408 }
409 };
410
411 addTouchEvents = function(elem, fn) {
412 var types = ['touchstart', 'touchend', 'touchmove', 'touchcancel'];
413 for (var i = 0; i < types.length; i++) {
414 self.dygraph_.addEvent(elem, types[i], fn);
415 }
416 };
417
418 this.dygraph_.attrs_.interactionModel =
419 Dygraph.Interaction.dragIsPanInteractionModel;
420 this.dygraph_.attrs_.panEdgeFraction = 0.0001;
421
422 var dragStartEvent = window.opera ? 'mousedown' : 'dragstart';
423 this.dygraph_.addEvent(this.leftZoomHandle_, dragStartEvent, onZoomStart);
424 this.dygraph_.addEvent(this.rightZoomHandle_, dragStartEvent, onZoomStart);
425
426 if (this.isUsingExcanvas_) {
427 this.dygraph_.addEvent(this.iePanOverlay_, 'mousedown', onPanStart);
428 } else {
429 this.dygraph_.addEvent(this.fgcanvas_, 'mousedown', onPanStart);
430 this.dygraph_.addEvent(this.fgcanvas_, 'mousemove', onCanvasMouseMove);
431 }
432
433 // Touch events
434 if (this.hasTouchInterface_) {
435 addTouchEvents(this.leftZoomHandle_, onZoomHandleTouchEvent);
436 addTouchEvents(this.rightZoomHandle_, onZoomHandleTouchEvent);
437 addTouchEvents(this.fgcanvas_, onCanvasTouchEvent);
438 }
439 };
440
441 /**
442 * @private
443 * Draws the static layer in the background canvas.
444 */
445 DygraphRangeSelector.prototype.drawStaticLayer_ = function() {
446 var ctx = this.bgcanvas_ctx_;
447 ctx.clearRect(0, 0, this.canvasRect_.w, this.canvasRect_.h);
448 try {
449 this.drawMiniPlot_();
450 } catch(ex) {
451 Dygraph.warn(ex);
452 }
453
454 var margin = 0.5;
455 this.bgcanvas_ctx_.lineWidth = 1;
456 ctx.strokeStyle = 'gray';
457 ctx.beginPath();
458 ctx.moveTo(margin, margin);
459 ctx.lineTo(margin, this.canvasRect_.h-margin);
460 ctx.lineTo(this.canvasRect_.w-margin, this.canvasRect_.h-margin);
461 ctx.lineTo(this.canvasRect_.w-margin, margin);
462 ctx.stroke();
463 };
464
465
466 /**
467 * @private
468 * Draws the mini plot in the background canvas.
469 */
470 DygraphRangeSelector.prototype.drawMiniPlot_ = function() {
471 var fillStyle = this.attr_('rangeSelectorPlotFillColor');
472 var strokeStyle = this.attr_('rangeSelectorPlotStrokeColor');
473 if (!fillStyle && !strokeStyle) {
474 return;
475 }
476
477 var combinedSeriesData = this.computeCombinedSeriesAndLimits_();
478 var yRange = combinedSeriesData.yMax - combinedSeriesData.yMin;
479
480 // Draw the mini plot.
481 var ctx = this.bgcanvas_ctx_;
482 var margin = 0.5;
483
484 var xExtremes = this.dygraph_.xAxisExtremes();
485 var xRange = Math.max(xExtremes[1] - xExtremes[0], 1.e-30);
486 var xFact = (this.canvasRect_.w - margin)/xRange;
487 var yFact = (this.canvasRect_.h - margin)/yRange;
488 var canvasWidth = this.canvasRect_.w - margin;
489 var canvasHeight = this.canvasRect_.h - margin;
490
491 ctx.beginPath();
492 ctx.moveTo(margin, canvasHeight);
493 for (var i = 0; i < combinedSeriesData.data.length; i++) {
494 var dataPoint = combinedSeriesData.data[i];
495 var x = (dataPoint[0] - xExtremes[0])*xFact;
496 var y = canvasHeight - (dataPoint[1] - combinedSeriesData.yMin)*yFact;
497 if (isFinite(x) && isFinite(y)) {
498 ctx.lineTo(x, y);
499 }
500 }
501 ctx.lineTo(canvasWidth, canvasHeight);
502 ctx.closePath();
503
504 if (fillStyle) {
505 var lingrad = this.bgcanvas_ctx_.createLinearGradient(0, 0, 0, canvasHeight);
506 lingrad.addColorStop(0, 'white');
507 lingrad.addColorStop(1, fillStyle);
508 this.bgcanvas_ctx_.fillStyle = lingrad;
509 ctx.fill();
510 }
511
512 if (strokeStyle) {
513 this.bgcanvas_ctx_.strokeStyle = strokeStyle;
514 this.bgcanvas_ctx_.lineWidth = 1.5;
515 ctx.stroke();
516 }
517 };
518
519 /**
520 * @private
521 * Computes and returns the combinded series data along with min/max for the mini plot.
522 * @return {Object} An object containing combinded series array, ymin, ymax.
523 */
524 DygraphRangeSelector.prototype.computeCombinedSeriesAndLimits_ = function() {
525 var data = this.dygraph_.rawData_;
526 var logscale = this.attr_('logscale');
527
528 // Create a combined series (average of all series values).
529 var combinedSeries = [];
530 var sum;
531 var count;
532 var mutipleValues;
533 var i, j, k;
534 var xVal, yVal;
535
536 // Find out if data has multiple values per datapoint.
537 // Go to first data point that actually has values (see http://code.google.com/p/dygraphs/issues/detail?id=246)
538 for (i = 0; i < data.length; i++) {
539 if (data[i].length > 1 && data[i][1] !== null) {
540 mutipleValues = typeof data[i][1] != 'number';
541 if (mutipleValues) {
542 sum = [];
543 count = [];
544 for (k = 0; k < data[i][1].length; k++) {
545 sum.push(0);
546 count.push(0);
547 }
548 }
549 break;
550 }
551 }
552
553 for (i = 0; i < data.length; i++) {
554 var dataPoint = data[i];
555 xVal = dataPoint[0];
556
557 if (mutipleValues) {
558 for (k = 0; k < sum.length; k++) {
559 sum[k] = count[k] = 0;
560 }
561 } else {
562 sum = count = 0;
563 }
564
565 for (j = 1; j < dataPoint.length; j++) {
566 if (this.dygraph_.visibility()[j-1]) {
567 var y;
568 if (mutipleValues) {
569 for (k = 0; k < sum.length; k++) {
570 y = dataPoint[j][k];
571 if (y === null || isNaN(y)) continue;
572 sum[k] += y;
573 count[k]++;
574 }
575 } else {
576 y = dataPoint[j];
577 if (y === null || isNaN(y)) continue;
578 sum += y;
579 count++;
580 }
581 }
582 }
583
584 if (mutipleValues) {
585 for (k = 0; k < sum.length; k++) {
586 sum[k] /= count[k];
587 }
588 yVal = sum.slice(0);
589 } else {
590 yVal = sum/count;
591 }
592
593 combinedSeries.push([xVal, yVal]);
594 }
595
596 // Account for roll period, fractions.
597 combinedSeries = this.dygraph_.rollingAverage(combinedSeries, this.dygraph_.rollPeriod_);
598
599 if (typeof combinedSeries[0][1] != 'number') {
600 for (i = 0; i < combinedSeries.length; i++) {
601 yVal = combinedSeries[i][1];
602 combinedSeries[i][1] = yVal[0];
603 }
604 }
605
606 // Compute the y range.
607 var yMin = Number.MAX_VALUE;
608 var yMax = -Number.MAX_VALUE;
609 for (i = 0; i < combinedSeries.length; i++) {
610 yVal = combinedSeries[i][1];
611 if (yVal !== null && isFinite(yVal) && (!logscale || yVal > 0)) {
612 yMin = Math.min(yMin, yVal);
613 yMax = Math.max(yMax, yVal);
614 }
615 }
616
617 // Convert Y data to log scale if needed.
618 // Also, expand the Y range to compress the mini plot a little.
619 var extraPercent = 0.25;
620 if (logscale) {
621 yMax = Dygraph.log10(yMax);
622 yMax += yMax*extraPercent;
623 yMin = Dygraph.log10(yMin);
624 for (i = 0; i < combinedSeries.length; i++) {
625 combinedSeries[i][1] = Dygraph.log10(combinedSeries[i][1]);
626 }
627 } else {
628 var yExtra;
629 var yRange = yMax - yMin;
630 if (yRange <= Number.MIN_VALUE) {
631 yExtra = yMax*extraPercent;
632 } else {
633 yExtra = yRange*extraPercent;
634 }
635 yMax += yExtra;
636 yMin -= yExtra;
637 }
638
639 return {data: combinedSeries, yMin: yMin, yMax: yMax};
640 };
641
642 /**
643 * @private
644 * Places the zoom handles in the proper position based on the current X data window.
645 */
646 DygraphRangeSelector.prototype.placeZoomHandles_ = function() {
647 var xExtremes = this.dygraph_.xAxisExtremes();
648 var xWindowLimits = this.dygraph_.xAxisRange();
649 var xRange = xExtremes[1] - xExtremes[0];
650 var leftPercent = Math.max(0, (xWindowLimits[0] - xExtremes[0])/xRange);
651 var rightPercent = Math.max(0, (xExtremes[1] - xWindowLimits[1])/xRange);
652 var leftCoord = this.canvasRect_.x + this.canvasRect_.w*leftPercent;
653 var rightCoord = this.canvasRect_.x + this.canvasRect_.w*(1 - rightPercent);
654 var handleTop = Math.max(this.canvasRect_.y, this.canvasRect_.y + (this.canvasRect_.h - this.leftZoomHandle_.height)/2);
655 var halfHandleWidth = this.leftZoomHandle_.width/2;
656 this.leftZoomHandle_.style.left = (leftCoord - halfHandleWidth) + 'px';
657 this.leftZoomHandle_.style.top = handleTop + 'px';
658 this.rightZoomHandle_.style.left = (rightCoord - halfHandleWidth) + 'px';
659 this.rightZoomHandle_.style.top = this.leftZoomHandle_.style.top;
660
661 this.leftZoomHandle_.style.visibility = 'visible';
662 this.rightZoomHandle_.style.visibility = 'visible';
663 };
664
665 /**
666 * @private
667 * Draws the interactive layer in the foreground canvas.
668 */
669 DygraphRangeSelector.prototype.drawInteractiveLayer_ = function() {
670 var ctx = this.fgcanvas_ctx_;
671 ctx.clearRect(0, 0, this.canvasRect_.w, this.canvasRect_.h);
672 var margin = 1;
673 var width = this.canvasRect_.w - margin;
674 var height = this.canvasRect_.h - margin;
675 var zoomHandleStatus = this.getZoomHandleStatus_();
676
677 ctx.strokeStyle = 'black';
678 if (!zoomHandleStatus.isZoomed) {
679 ctx.beginPath();
680 ctx.moveTo(margin, margin);
681 ctx.lineTo(margin, height);
682 ctx.lineTo(width, height);
683 ctx.lineTo(width, margin);
684 ctx.stroke();
685 if (this.iePanOverlay_) {
686 this.iePanOverlay_.style.display = 'none';
687 }
688 } else {
689 var leftHandleCanvasPos = Math.max(margin, zoomHandleStatus.leftHandlePos - this.canvasRect_.x);
690 var rightHandleCanvasPos = Math.min(width, zoomHandleStatus.rightHandlePos - this.canvasRect_.x);
691
692 ctx.fillStyle = 'rgba(240, 240, 240, 0.6)';
693 ctx.fillRect(0, 0, leftHandleCanvasPos, this.canvasRect_.h);
694 ctx.fillRect(rightHandleCanvasPos, 0, this.canvasRect_.w - rightHandleCanvasPos, this.canvasRect_.h);
695
696 ctx.beginPath();
697 ctx.moveTo(margin, margin);
698 ctx.lineTo(leftHandleCanvasPos, margin);
699 ctx.lineTo(leftHandleCanvasPos, height);
700 ctx.lineTo(rightHandleCanvasPos, height);
701 ctx.lineTo(rightHandleCanvasPos, margin);
702 ctx.lineTo(width, margin);
703 ctx.stroke();
704
705 if (this.isUsingExcanvas_) {
706 this.iePanOverlay_.style.width = (rightHandleCanvasPos - leftHandleCanvasPos) + 'px';
707 this.iePanOverlay_.style.left = leftHandleCanvasPos + 'px';
708 this.iePanOverlay_.style.height = height + 'px';
709 this.iePanOverlay_.style.display = 'inline';
710 }
711 }
712 };
713
714 /**
715 * @private
716 * Returns the current zoom handle position information.
717 * @return {Object} The zoom handle status.
718 */
719 DygraphRangeSelector.prototype.getZoomHandleStatus_ = function() {
720 var halfHandleWidth = this.leftZoomHandle_.width/2;
721 var leftHandlePos = parseInt(this.leftZoomHandle_.style.left, 10) + halfHandleWidth;
722 var rightHandlePos = parseInt(this.rightZoomHandle_.style.left, 10) + halfHandleWidth;
723 return {
724 leftHandlePos: leftHandlePos,
725 rightHandlePos: rightHandlePos,
726 isZoomed: (leftHandlePos - 1 > this.canvasRect_.x || rightHandlePos + 1 < this.canvasRect_.x+this.canvasRect_.w)
727 };
728 };