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