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 | ||
0d216a60 PF |
13 | Dygraph.Plugins.RangeSelector = (function() { |
14 | ||
758a629f | 15 | /*global Dygraph:false */ |
c0f54d4f DV |
16 | "use strict"; |
17 | ||
0d216a60 | 18 | var rangeSelector = function() { |
ccd9d7c2 | 19 | this.isIE_ = /MSIE/.test(navigator.userAgent) && !window.opera; |
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 AK |
163 | function setElementRect(canvas, context, rect) { |
164 | var canvasScale = Dygraph.getContextPixelRatio(context); | |
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() { |
ccd9d7c2 | 200 | this.bgcanvas_ = Dygraph.createCanvas(); |
88c4a47e | 201 | this.bgcanvas_.className = 'dygraph-rangesel-bgcanvas'; |
ccd9d7c2 | 202 | this.bgcanvas_.style.position = 'absolute'; |
920208fb | 203 | this.bgcanvas_.style.zIndex = 9; |
ccd9d7c2 PF |
204 | this.bgcanvas_ctx_ = Dygraph.getContext(this.bgcanvas_); |
205 | ||
206 | this.fgcanvas_ = Dygraph.createCanvas(); | |
88c4a47e | 207 | this.fgcanvas_.className = 'dygraph-rangesel-fgcanvas'; |
ccd9d7c2 | 208 | this.fgcanvas_.style.position = 'absolute'; |
920208fb | 209 | this.fgcanvas_.style.zIndex = 9; |
ccd9d7c2 PF |
210 | this.fgcanvas_.style.cursor = 'default'; |
211 | this.fgcanvas_ctx_ = Dygraph.getContext(this.fgcanvas_); | |
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'; | |
b77d7a56 | 225 | //TODO: change image to more options |
920208fb | 226 | if (/MSIE 7/.test(navigator.userAgent)) { // IE7 doesn't support embedded src data. |
73c5deff PF |
227 | img.width = 7; |
228 | img.height = 14; | |
229 | img.style.backgroundColor = 'white'; | |
230 | img.style.border = '1px solid #333333'; // Just show box in IE7. | |
920208fb | 231 | } else { |
73c5deff PF |
232 | img.width = 9; |
233 | img.height = 16; | |
234 | img.src = 'data:image/png;base64,' + | |
758a629f DV |
235 | 'iVBORw0KGgoAAAANSUhEUgAAAAkAAAAQCAYAAADESFVDAAAAAXNSR0IArs4c6QAAAAZiS0dEANAA' + |
236 | 'zwDP4Z7KegAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9sHGw0cMqdt1UwAAAAZdEVYdENv' + | |
237 | 'bW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAAaElEQVQoz+3SsRFAQBCF4Z9WJM8KCDVwownl' + | |
238 | '6YXsTmCUsyKGkZzcl7zkz3YLkypgAnreFmDEpHkIwVOMfpdi9CEEN2nGpFdwD03yEqDtOgCaun7s' + | |
239 | 'qSTDH32I1pQA2Pb9sZecAxc5r3IAb21d6878xsAAAAAASUVORK5CYII='; | |
920208fb | 240 | } |
ccd9d7c2 | 241 | |
0c76da8d | 242 | if (this.isMobileDevice_) { |
73c5deff PF |
243 | img.width *= 2; |
244 | img.height *= 2; | |
245 | } | |
246 | ||
ccd9d7c2 PF |
247 | this.leftZoomHandle_ = img; |
248 | this.rightZoomHandle_ = img.cloneNode(false); | |
249 | }; | |
250 | ||
251 | /** | |
252 | * @private | |
253 | * Sets up the interaction for the range selector. | |
254 | */ | |
0d216a60 | 255 | rangeSelector.prototype.initInteraction_ = function() { |
ccd9d7c2 | 256 | var self = this; |
bcf7f810 | 257 | var topElem = document; |
2871344a | 258 | var clientXLast = 0; |
ccd9d7c2 PF |
259 | var handle = null; |
260 | var isZooming = false; | |
261 | var isPanning = false; | |
9901b0c1 | 262 | var dynamic = !this.isMobileDevice_; |
ccd9d7c2 | 263 | |
2bad4d92 DV |
264 | // We cover iframes during mouse interactions. See comments in |
265 | // dygraph-utils.js for more info on why this is a good idea. | |
266 | var tarp = new Dygraph.IFrameTarp(); | |
267 | ||
758a629f DV |
268 | // functions, defined below. Defining them this way (rather than with |
269 | // "function foo() {...}" makes JSHint happy. | |
270 | var toXDataWindow, onZoomStart, onZoom, onZoomEnd, doZoom, isMouseInPanZone, | |
2871344a | 271 | onPanStart, onPan, onPanEnd, doPan, onCanvasHover; |
758a629f | 272 | |
73c5deff PF |
273 | // Touch event functions |
274 | var onZoomHandleTouchEvent, onCanvasTouchEvent, addTouchEvents; | |
275 | ||
758a629f | 276 | toXDataWindow = function(zoomHandleStatus) { |
ccd9d7c2 PF |
277 | var xDataLimits = self.dygraph_.xAxisExtremes(); |
278 | var fact = (xDataLimits[1] - xDataLimits[0])/self.canvasRect_.w; | |
279 | var xDataMin = xDataLimits[0] + (zoomHandleStatus.leftHandlePos - self.canvasRect_.x)*fact; | |
280 | var xDataMax = xDataLimits[0] + (zoomHandleStatus.rightHandlePos - self.canvasRect_.x)*fact; | |
281 | return [xDataMin, xDataMax]; | |
282 | }; | |
283 | ||
758a629f | 284 | onZoomStart = function(e) { |
ccd9d7c2 PF |
285 | Dygraph.cancelEvent(e); |
286 | isZooming = true; | |
2871344a | 287 | clientXLast = e.clientX; |
ccd9d7c2 | 288 | handle = e.target ? e.target : e.srcElement; |
6835cab8 | 289 | if (e.type === 'mousedown' || e.type === 'dragstart') { |
4f41df5a | 290 | // These events are removed manually. |
6835cab8 PF |
291 | Dygraph.addEvent(topElem, 'mousemove', onZoom); |
292 | Dygraph.addEvent(topElem, 'mouseup', onZoomEnd); | |
293 | } | |
ccd9d7c2 | 294 | self.fgcanvas_.style.cursor = 'col-resize'; |
2bad4d92 | 295 | tarp.cover(); |
0c76da8d | 296 | return true; |
ccd9d7c2 PF |
297 | }; |
298 | ||
758a629f | 299 | onZoom = function(e) { |
ccd9d7c2 | 300 | if (!isZooming) { |
0c76da8d | 301 | return false; |
ccd9d7c2 | 302 | } |
0c76da8d | 303 | Dygraph.cancelEvent(e); |
6835cab8 | 304 | |
2871344a | 305 | var delX = e.clientX - clientXLast; |
6835cab8 | 306 | if (Math.abs(delX) < 4) { |
0c76da8d | 307 | return true; |
ccd9d7c2 | 308 | } |
2871344a | 309 | clientXLast = e.clientX; |
0c76da8d PF |
310 | |
311 | // Move handle. | |
ccd9d7c2 | 312 | var zoomHandleStatus = self.getZoomHandleStatus_(); |
758a629f | 313 | var newPos; |
ccd9d7c2 | 314 | if (handle == self.leftZoomHandle_) { |
2871344a | 315 | newPos = zoomHandleStatus.leftHandlePos + delX; |
ccd9d7c2 PF |
316 | newPos = Math.min(newPos, zoomHandleStatus.rightHandlePos - handle.width - 3); |
317 | newPos = Math.max(newPos, self.canvasRect_.x); | |
318 | } else { | |
2871344a | 319 | newPos = zoomHandleStatus.rightHandlePos + delX; |
ccd9d7c2 PF |
320 | newPos = Math.min(newPos, self.canvasRect_.x + self.canvasRect_.w); |
321 | newPos = Math.max(newPos, zoomHandleStatus.leftHandlePos + handle.width + 3); | |
322 | } | |
920208fb | 323 | var halfHandleWidth = handle.width/2; |
ccd9d7c2 PF |
324 | handle.style.left = (newPos - halfHandleWidth) + 'px'; |
325 | self.drawInteractiveLayer_(); | |
326 | ||
9901b0c1 | 327 | // Zoom on the fly. |
73c5deff | 328 | if (dynamic) { |
ccd9d7c2 PF |
329 | doZoom(); |
330 | } | |
0c76da8d | 331 | return true; |
ccd9d7c2 PF |
332 | }; |
333 | ||
758a629f | 334 | onZoomEnd = function(e) { |
ccd9d7c2 | 335 | if (!isZooming) { |
0c76da8d | 336 | return false; |
ccd9d7c2 PF |
337 | } |
338 | isZooming = false; | |
2bad4d92 | 339 | tarp.uncover(); |
ccd9d7c2 PF |
340 | Dygraph.removeEvent(topElem, 'mousemove', onZoom); |
341 | Dygraph.removeEvent(topElem, 'mouseup', onZoomEnd); | |
342 | self.fgcanvas_.style.cursor = 'default'; | |
343 | ||
9901b0c1 | 344 | // If on a slower device, zoom now. |
73c5deff | 345 | if (!dynamic) { |
ccd9d7c2 PF |
346 | doZoom(); |
347 | } | |
0c76da8d | 348 | return true; |
ccd9d7c2 PF |
349 | }; |
350 | ||
758a629f | 351 | doZoom = function() { |
ccd9d7c2 PF |
352 | try { |
353 | var zoomHandleStatus = self.getZoomHandleStatus_(); | |
354 | self.isChangingRange_ = true; | |
355 | if (!zoomHandleStatus.isZoomed) { | |
e4f6e11a | 356 | self.dygraph_.resetZoom(); |
ccd9d7c2 PF |
357 | } else { |
358 | var xDataWindow = toXDataWindow(zoomHandleStatus); | |
359 | self.dygraph_.doZoomXDates_(xDataWindow[0], xDataWindow[1]); | |
360 | } | |
361 | } finally { | |
362 | self.isChangingRange_ = false; | |
363 | } | |
364 | }; | |
365 | ||
758a629f | 366 | isMouseInPanZone = function(e) { |
9901b0c1 DV |
367 | var rect = self.leftZoomHandle_.getBoundingClientRect(); |
368 | var leftHandleClientX = rect.left + rect.width/2; | |
369 | rect = self.rightZoomHandle_.getBoundingClientRect(); | |
370 | var rightHandleClientX = rect.left + rect.width/2; | |
371 | return (e.clientX > leftHandleClientX && e.clientX < rightHandleClientX); | |
ccd9d7c2 PF |
372 | }; |
373 | ||
758a629f | 374 | onPanStart = function(e) { |
ccd9d7c2 PF |
375 | if (!isPanning && isMouseInPanZone(e) && self.getZoomHandleStatus_().isZoomed) { |
376 | Dygraph.cancelEvent(e); | |
377 | isPanning = true; | |
2871344a | 378 | clientXLast = e.clientX; |
6835cab8 | 379 | if (e.type === 'mousedown') { |
4f41df5a | 380 | // These events are removed manually. |
6835cab8 PF |
381 | Dygraph.addEvent(topElem, 'mousemove', onPan); |
382 | Dygraph.addEvent(topElem, 'mouseup', onPanEnd); | |
383 | } | |
0c76da8d | 384 | return true; |
ccd9d7c2 | 385 | } |
0c76da8d | 386 | return false; |
ccd9d7c2 PF |
387 | }; |
388 | ||
758a629f | 389 | onPan = function(e) { |
ccd9d7c2 | 390 | if (!isPanning) { |
0c76da8d | 391 | return false; |
ccd9d7c2 | 392 | } |
920208fb | 393 | Dygraph.cancelEvent(e); |
ccd9d7c2 | 394 | |
2871344a | 395 | var delX = e.clientX - clientXLast; |
ccd9d7c2 | 396 | if (Math.abs(delX) < 4) { |
0c76da8d | 397 | return true; |
ccd9d7c2 | 398 | } |
2871344a | 399 | clientXLast = e.clientX; |
ccd9d7c2 PF |
400 | |
401 | // Move range view | |
402 | var zoomHandleStatus = self.getZoomHandleStatus_(); | |
403 | var leftHandlePos = zoomHandleStatus.leftHandlePos; | |
404 | var rightHandlePos = zoomHandleStatus.rightHandlePos; | |
405 | var rangeSize = rightHandlePos - leftHandlePos; | |
406 | if (leftHandlePos + delX <= self.canvasRect_.x) { | |
407 | leftHandlePos = self.canvasRect_.x; | |
408 | rightHandlePos = leftHandlePos + rangeSize; | |
409 | } else if (rightHandlePos + delX >= self.canvasRect_.x + self.canvasRect_.w) { | |
410 | rightHandlePos = self.canvasRect_.x + self.canvasRect_.w; | |
411 | leftHandlePos = rightHandlePos - rangeSize; | |
412 | } else { | |
413 | leftHandlePos += delX; | |
414 | rightHandlePos += delX; | |
415 | } | |
920208fb | 416 | var halfHandleWidth = self.leftZoomHandle_.width/2; |
ccd9d7c2 PF |
417 | self.leftZoomHandle_.style.left = (leftHandlePos - halfHandleWidth) + 'px'; |
418 | self.rightZoomHandle_.style.left = (rightHandlePos - halfHandleWidth) + 'px'; | |
419 | self.drawInteractiveLayer_(); | |
420 | ||
9901b0c1 | 421 | // Do pan on the fly. |
73c5deff | 422 | if (dynamic) { |
ccd9d7c2 PF |
423 | doPan(); |
424 | } | |
0c76da8d | 425 | return true; |
ccd9d7c2 PF |
426 | }; |
427 | ||
758a629f | 428 | onPanEnd = function(e) { |
ccd9d7c2 | 429 | if (!isPanning) { |
0c76da8d | 430 | return false; |
ccd9d7c2 PF |
431 | } |
432 | isPanning = false; | |
433 | Dygraph.removeEvent(topElem, 'mousemove', onPan); | |
434 | Dygraph.removeEvent(topElem, 'mouseup', onPanEnd); | |
9901b0c1 | 435 | // If on a slower device, do pan now. |
73c5deff | 436 | if (!dynamic) { |
ccd9d7c2 PF |
437 | doPan(); |
438 | } | |
0c76da8d | 439 | return true; |
ccd9d7c2 PF |
440 | }; |
441 | ||
758a629f | 442 | doPan = function() { |
ccd9d7c2 PF |
443 | try { |
444 | self.isChangingRange_ = true; | |
445 | self.dygraph_.dateWindow_ = toXDataWindow(self.getZoomHandleStatus_()); | |
446 | self.dygraph_.drawGraph_(false); | |
447 | } finally { | |
448 | self.isChangingRange_ = false; | |
449 | } | |
450 | }; | |
451 | ||
6835cab8 | 452 | onCanvasHover = function(e) { |
ccd9d7c2 PF |
453 | if (isZooming || isPanning) { |
454 | return; | |
455 | } | |
456 | var cursor = isMouseInPanZone(e) ? 'move' : 'default'; | |
457 | if (cursor != self.fgcanvas_.style.cursor) { | |
458 | self.fgcanvas_.style.cursor = cursor; | |
459 | } | |
460 | }; | |
461 | ||
73c5deff | 462 | onZoomHandleTouchEvent = function(e) { |
0c76da8d PF |
463 | if (e.type == 'touchstart' && e.targetTouches.length == 1) { |
464 | if (onZoomStart(e.targetTouches[0])) { | |
465 | Dygraph.cancelEvent(e); | |
466 | } | |
467 | } else if (e.type == 'touchmove' && e.targetTouches.length == 1) { | |
468 | if (onZoom(e.targetTouches[0])) { | |
469 | Dygraph.cancelEvent(e); | |
470 | } | |
73c5deff PF |
471 | } else { |
472 | onZoomEnd(e); | |
473 | } | |
474 | }; | |
475 | ||
476 | onCanvasTouchEvent = function(e) { | |
0c76da8d PF |
477 | if (e.type == 'touchstart' && e.targetTouches.length == 1) { |
478 | if (onPanStart(e.targetTouches[0])) { | |
479 | Dygraph.cancelEvent(e); | |
480 | } | |
481 | } else if (e.type == 'touchmove' && e.targetTouches.length == 1) { | |
482 | if (onPan(e.targetTouches[0])) { | |
483 | Dygraph.cancelEvent(e); | |
484 | } | |
73c5deff PF |
485 | } else { |
486 | onPanEnd(e); | |
487 | } | |
488 | }; | |
489 | ||
490 | addTouchEvents = function(elem, fn) { | |
6a4587ac RK |
491 | var types = ['touchstart', 'touchend', 'touchmove', 'touchcancel']; |
492 | for (var i = 0; i < types.length; i++) { | |
aeca29ac | 493 | self.dygraph_.addAndTrackEvent(elem, types[i], fn); |
6a4587ac | 494 | } |
73c5deff PF |
495 | }; |
496 | ||
ec475f44 PF |
497 | this.setDefaultOption_('interactionModel', Dygraph.Interaction.dragIsPanInteractionModel); |
498 | this.setDefaultOption_('panEdgeFraction', 0.0001); | |
ccd9d7c2 | 499 | |
920208fb | 500 | var dragStartEvent = window.opera ? 'mousedown' : 'dragstart'; |
aeca29ac RK |
501 | this.dygraph_.addAndTrackEvent(this.leftZoomHandle_, dragStartEvent, onZoomStart); |
502 | this.dygraph_.addAndTrackEvent(this.rightZoomHandle_, dragStartEvent, onZoomStart); | |
920208fb | 503 | |
9901b0c1 DV |
504 | this.dygraph_.addAndTrackEvent(this.fgcanvas_, 'mousedown', onPanStart); |
505 | this.dygraph_.addAndTrackEvent(this.fgcanvas_, 'mousemove', onCanvasHover); | |
73c5deff PF |
506 | |
507 | // Touch events | |
508 | if (this.hasTouchInterface_) { | |
509 | addTouchEvents(this.leftZoomHandle_, onZoomHandleTouchEvent); | |
510 | addTouchEvents(this.rightZoomHandle_, onZoomHandleTouchEvent); | |
511 | addTouchEvents(this.fgcanvas_, onCanvasTouchEvent); | |
512 | } | |
ccd9d7c2 PF |
513 | }; |
514 | ||
515 | /** | |
516 | * @private | |
517 | * Draws the static layer in the background canvas. | |
518 | */ | |
0d216a60 | 519 | rangeSelector.prototype.drawStaticLayer_ = function() { |
ccd9d7c2 PF |
520 | var ctx = this.bgcanvas_ctx_; |
521 | ctx.clearRect(0, 0, this.canvasRect_.w, this.canvasRect_.h); | |
db6a9f5a | 522 | try { |
ccd9d7c2 | 523 | this.drawMiniPlot_(); |
db6a9f5a | 524 | } catch(ex) { |
8a68db7d | 525 | console.warn(ex); |
db6a9f5a | 526 | } |
920208fb | 527 | |
758a629f | 528 | var margin = 0.5; |
b77d7a56 | 529 | this.bgcanvas_ctx_.lineWidth = this.getOption_('rangeSelectorBGLineWidth'); |
530 | ctx.strokeStyle = this.getOption_('rangeSelectorBGStrokeColor'); | |
920208fb PF |
531 | ctx.beginPath(); |
532 | ctx.moveTo(margin, margin); | |
533 | ctx.lineTo(margin, this.canvasRect_.h-margin); | |
534 | ctx.lineTo(this.canvasRect_.w-margin, this.canvasRect_.h-margin); | |
535 | ctx.lineTo(this.canvasRect_.w-margin, margin); | |
536 | ctx.stroke(); | |
ccd9d7c2 PF |
537 | }; |
538 | ||
539 | ||
540 | /** | |
541 | * @private | |
542 | * Draws the mini plot in the background canvas. | |
543 | */ | |
0d216a60 | 544 | rangeSelector.prototype.drawMiniPlot_ = function() { |
ec475f44 PF |
545 | var fillStyle = this.getOption_('rangeSelectorPlotFillColor'); |
546 | var strokeStyle = this.getOption_('rangeSelectorPlotStrokeColor'); | |
ccd9d7c2 PF |
547 | if (!fillStyle && !strokeStyle) { |
548 | return; | |
549 | } | |
b77d7a56 | 550 | var gradientFillStyle = this.getOption_('rangeSelectorPlotGradientColor'); |
551 | if (!gradientFillStyle) { gradientFillStyle = fillStyle; } | |
ccd9d7c2 | 552 | |
ec475f44 | 553 | var stepPlot = this.getOption_('stepPlot'); |
e03d443c | 554 | |
ccd9d7c2 PF |
555 | var combinedSeriesData = this.computeCombinedSeriesAndLimits_(); |
556 | var yRange = combinedSeriesData.yMax - combinedSeriesData.yMin; | |
557 | ||
558 | // Draw the mini plot. | |
559 | var ctx = this.bgcanvas_ctx_; | |
758a629f | 560 | var margin = 0.5; |
ccd9d7c2 PF |
561 | |
562 | var xExtremes = this.dygraph_.xAxisExtremes(); | |
563 | var xRange = Math.max(xExtremes[1] - xExtremes[0], 1.e-30); | |
564 | var xFact = (this.canvasRect_.w - margin)/xRange; | |
565 | var yFact = (this.canvasRect_.h - margin)/yRange; | |
566 | var canvasWidth = this.canvasRect_.w - margin; | |
567 | var canvasHeight = this.canvasRect_.h - margin; | |
568 | ||
e03d443c WB |
569 | var prevX = null, prevY = null; |
570 | ||
ccd9d7c2 PF |
571 | ctx.beginPath(); |
572 | ctx.moveTo(margin, canvasHeight); | |
573 | for (var i = 0; i < combinedSeriesData.data.length; i++) { | |
574 | var dataPoint = combinedSeriesData.data[i]; | |
e03d443c WB |
575 | var x = ((dataPoint[0] !== null) ? ((dataPoint[0] - xExtremes[0])*xFact) : NaN); |
576 | var y = ((dataPoint[1] !== null) ? (canvasHeight - (dataPoint[1] - combinedSeriesData.yMin)*yFact) : NaN); | |
5575fa36 DV |
577 | |
578 | // Skip points that don't change the x-value. Overly fine-grained points | |
579 | // can cause major slowdowns with the ctx.fill() call below. | |
69c9b9f2 | 580 | if (!stepPlot && prevX !== null && Math.round(x) == Math.round(prevX)) { |
5575fa36 DV |
581 | continue; |
582 | } | |
583 | ||
ccd9d7c2 | 584 | if (isFinite(x) && isFinite(y)) { |
e03d443c | 585 | if(prevX === null) { |
87196f1b WB |
586 | ctx.lineTo(x, canvasHeight); |
587 | } | |
588 | else if (stepPlot) { | |
589 | ctx.lineTo(x, prevY); | |
590 | } | |
ccd9d7c2 | 591 | ctx.lineTo(x, y); |
e03d443c | 592 | prevX = x; |
87196f1b | 593 | prevY = y; |
ccd9d7c2 | 594 | } |
e03d443c | 595 | else { |
87196f1b WB |
596 | if(prevX !== null) { |
597 | if (stepPlot) { | |
598 | ctx.lineTo(x, prevY); | |
599 | ctx.lineTo(x, canvasHeight); | |
600 | } | |
601 | else { | |
602 | ctx.lineTo(prevX, canvasHeight); | |
603 | } | |
604 | } | |
605 | prevX = prevY = null; | |
606 | } | |
ccd9d7c2 PF |
607 | } |
608 | ctx.lineTo(canvasWidth, canvasHeight); | |
609 | ctx.closePath(); | |
610 | ||
611 | if (fillStyle) { | |
612 | var lingrad = this.bgcanvas_ctx_.createLinearGradient(0, 0, 0, canvasHeight); | |
b77d7a56 | 613 | lingrad.addColorStop(0, gradientFillStyle); |
ccd9d7c2 PF |
614 | lingrad.addColorStop(1, fillStyle); |
615 | this.bgcanvas_ctx_.fillStyle = lingrad; | |
616 | ctx.fill(); | |
617 | } | |
618 | ||
619 | if (strokeStyle) { | |
620 | this.bgcanvas_ctx_.strokeStyle = strokeStyle; | |
b77d7a56 | 621 | this.bgcanvas_ctx_.lineWidth = this.getOption_('rangeSelectorPlotLineWidth'); |
ccd9d7c2 PF |
622 | ctx.stroke(); |
623 | } | |
624 | }; | |
625 | ||
626 | /** | |
627 | * @private | |
dcebad80 DV |
628 | * Computes and returns the combined series data along with min/max for the mini plot. |
629 | * The combined series consists of averaged values for all series. | |
630 | * When series have error bars, the error bars are ignored. | |
631 | * @return {Object} An object containing combined series array, ymin, ymax. | |
ccd9d7c2 | 632 | */ |
0d216a60 | 633 | rangeSelector.prototype.computeCombinedSeriesAndLimits_ = function() { |
dcebad80 | 634 | var g = this.dygraph_; |
ec475f44 | 635 | var logscale = this.getOption_('logscale'); |
bcf7f810 | 636 | var i; |
ccd9d7c2 | 637 | |
0a0885d1 DV |
638 | // Select series to combine. By default, all series are combined. |
639 | var numColumns = g.numColumns(); | |
640 | var labels = g.getLabels(); | |
641 | var includeSeries = new Array(numColumns); | |
642 | var anySet = false; | |
643 | for (i = 1; i < numColumns; i++) { | |
644 | var include = this.getOption_('showInRangeSelector', labels[i]); | |
645 | includeSeries[i] = include; | |
646 | if (include !== null) anySet = true; // it's set explicitly for this series | |
647 | } | |
648 | if (!anySet) { | |
649 | for (i = 0; i < includeSeries.length; i++) includeSeries[i] = true; | |
1085dbce PP |
650 | } |
651 | ||
0a0885d1 | 652 | // Create a combined series (average of selected series values). |
dcebad80 DV |
653 | // TODO(danvk): short-circuit if there's only one series. |
654 | var rolledSeries = []; | |
655 | var dataHandler = g.dataHandler_; | |
656 | var options = g.attributes_; | |
0a0885d1 DV |
657 | for (i = 1; i < g.numColumns(); i++) { |
658 | if (!includeSeries[i]) continue; | |
659 | var series = dataHandler.extractSeries(g.rawData_, i, options); | |
dcebad80 DV |
660 | if (g.rollPeriod() > 1) { |
661 | series = dataHandler.rollingAverage(series, g.rollPeriod(), options); | |
ccd9d7c2 | 662 | } |
b77d7a56 | 663 | |
dcebad80 | 664 | rolledSeries.push(series); |
ccd9d7c2 PF |
665 | } |
666 | ||
dcebad80 DV |
667 | var combinedSeries = []; |
668 | for (i = 0; i < rolledSeries[0].length; i++) { | |
669 | var sum = 0; | |
670 | var count = 0; | |
bcf7f810 | 671 | for (var j = 0; j < rolledSeries.length; j++) { |
dcebad80 DV |
672 | var y = rolledSeries[j][i][1]; |
673 | if (y === null || isNaN(y)) continue; | |
674 | count++; | |
675 | sum += y; | |
ccd9d7c2 | 676 | } |
dcebad80 | 677 | combinedSeries.push([rolledSeries[0][i][0], sum / count]); |
ccd9d7c2 PF |
678 | } |
679 | ||
680 | // Compute the y range. | |
681 | var yMin = Number.MAX_VALUE; | |
682 | var yMax = -Number.MAX_VALUE; | |
758a629f | 683 | for (i = 0; i < combinedSeries.length; i++) { |
c0430a23 | 684 | var yVal = combinedSeries[i][1]; |
758a629f | 685 | if (yVal !== null && isFinite(yVal) && (!logscale || yVal > 0)) { |
ccd9d7c2 PF |
686 | yMin = Math.min(yMin, yVal); |
687 | yMax = Math.max(yMax, yVal); | |
688 | } | |
689 | } | |
690 | ||
691 | // Convert Y data to log scale if needed. | |
692 | // Also, expand the Y range to compress the mini plot a little. | |
758a629f | 693 | var extraPercent = 0.25; |
ccd9d7c2 PF |
694 | if (logscale) { |
695 | yMax = Dygraph.log10(yMax); | |
696 | yMax += yMax*extraPercent; | |
697 | yMin = Dygraph.log10(yMin); | |
758a629f | 698 | for (i = 0; i < combinedSeries.length; i++) { |
ccd9d7c2 PF |
699 | combinedSeries[i][1] = Dygraph.log10(combinedSeries[i][1]); |
700 | } | |
701 | } else { | |
702 | var yExtra; | |
5061b42f | 703 | var yRange = yMax - yMin; |
ccd9d7c2 PF |
704 | if (yRange <= Number.MIN_VALUE) { |
705 | yExtra = yMax*extraPercent; | |
706 | } else { | |
707 | yExtra = yRange*extraPercent; | |
708 | } | |
709 | yMax += yExtra; | |
710 | yMin -= yExtra; | |
711 | } | |
712 | ||
713 | return {data: combinedSeries, yMin: yMin, yMax: yMax}; | |
714 | }; | |
715 | ||
716 | /** | |
717 | * @private | |
718 | * Places the zoom handles in the proper position based on the current X data window. | |
719 | */ | |
0d216a60 | 720 | rangeSelector.prototype.placeZoomHandles_ = function() { |
ccd9d7c2 PF |
721 | var xExtremes = this.dygraph_.xAxisExtremes(); |
722 | var xWindowLimits = this.dygraph_.xAxisRange(); | |
723 | var xRange = xExtremes[1] - xExtremes[0]; | |
724 | var leftPercent = Math.max(0, (xWindowLimits[0] - xExtremes[0])/xRange); | |
725 | var rightPercent = Math.max(0, (xExtremes[1] - xWindowLimits[1])/xRange); | |
726 | var leftCoord = this.canvasRect_.x + this.canvasRect_.w*leftPercent; | |
727 | var rightCoord = this.canvasRect_.x + this.canvasRect_.w*(1 - rightPercent); | |
920208fb PF |
728 | var handleTop = Math.max(this.canvasRect_.y, this.canvasRect_.y + (this.canvasRect_.h - this.leftZoomHandle_.height)/2); |
729 | var halfHandleWidth = this.leftZoomHandle_.width/2; | |
730 | this.leftZoomHandle_.style.left = (leftCoord - halfHandleWidth) + 'px'; | |
ccd9d7c2 | 731 | this.leftZoomHandle_.style.top = handleTop + 'px'; |
920208fb | 732 | this.rightZoomHandle_.style.left = (rightCoord - halfHandleWidth) + 'px'; |
ccd9d7c2 PF |
733 | this.rightZoomHandle_.style.top = this.leftZoomHandle_.style.top; |
734 | ||
735 | this.leftZoomHandle_.style.visibility = 'visible'; | |
736 | this.rightZoomHandle_.style.visibility = 'visible'; | |
737 | }; | |
738 | ||
739 | /** | |
740 | * @private | |
741 | * Draws the interactive layer in the foreground canvas. | |
742 | */ | |
0d216a60 | 743 | rangeSelector.prototype.drawInteractiveLayer_ = function() { |
ccd9d7c2 PF |
744 | var ctx = this.fgcanvas_ctx_; |
745 | ctx.clearRect(0, 0, this.canvasRect_.w, this.canvasRect_.h); | |
746 | var margin = 1; | |
747 | var width = this.canvasRect_.w - margin; | |
748 | var height = this.canvasRect_.h - margin; | |
749 | var zoomHandleStatus = this.getZoomHandleStatus_(); | |
750 | ||
b77d7a56 | 751 | ctx.strokeStyle = this.getOption_('rangeSelectorFGStrokeColor'); |
752 | ctx.lineWidth = this.getOption_('rangeSelectorFGLineWidth'); | |
ccd9d7c2 PF |
753 | if (!zoomHandleStatus.isZoomed) { |
754 | ctx.beginPath(); | |
755 | ctx.moveTo(margin, margin); | |
756 | ctx.lineTo(margin, height); | |
757 | ctx.lineTo(width, height); | |
758 | ctx.lineTo(width, margin); | |
759 | ctx.stroke(); | |
760 | } else { | |
5061b42f DV |
761 | var leftHandleCanvasPos = Math.max(margin, zoomHandleStatus.leftHandlePos - this.canvasRect_.x); |
762 | var rightHandleCanvasPos = Math.min(width, zoomHandleStatus.rightHandlePos - this.canvasRect_.x); | |
ccd9d7c2 | 763 | |
b77d7a56 | 764 | ctx.fillStyle = 'rgba(240, 240, 240, ' + this.getOption_('rangeSelectorAlpha').toString() + ')'; |
920208fb PF |
765 | ctx.fillRect(0, 0, leftHandleCanvasPos, this.canvasRect_.h); |
766 | ctx.fillRect(rightHandleCanvasPos, 0, this.canvasRect_.w - rightHandleCanvasPos, this.canvasRect_.h); | |
ccd9d7c2 PF |
767 | |
768 | ctx.beginPath(); | |
769 | ctx.moveTo(margin, margin); | |
770 | ctx.lineTo(leftHandleCanvasPos, margin); | |
771 | ctx.lineTo(leftHandleCanvasPos, height); | |
772 | ctx.lineTo(rightHandleCanvasPos, height); | |
773 | ctx.lineTo(rightHandleCanvasPos, margin); | |
774 | ctx.lineTo(width, margin); | |
775 | ctx.stroke(); | |
776 | } | |
777 | }; | |
778 | ||
779 | /** | |
780 | * @private | |
781 | * Returns the current zoom handle position information. | |
782 | * @return {Object} The zoom handle status. | |
783 | */ | |
0d216a60 | 784 | rangeSelector.prototype.getZoomHandleStatus_ = function() { |
920208fb | 785 | var halfHandleWidth = this.leftZoomHandle_.width/2; |
088be40b PF |
786 | var leftHandlePos = parseFloat(this.leftZoomHandle_.style.left) + halfHandleWidth; |
787 | var rightHandlePos = parseFloat(this.rightZoomHandle_.style.left) + halfHandleWidth; | |
ccd9d7c2 PF |
788 | return { |
789 | leftHandlePos: leftHandlePos, | |
790 | rightHandlePos: rightHandlePos, | |
791 | isZoomed: (leftHandlePos - 1 > this.canvasRect_.x || rightHandlePos + 1 < this.canvasRect_.x+this.canvasRect_.w) | |
792 | }; | |
793 | }; | |
0d216a60 PF |
794 | |
795 | return rangeSelector; | |
796 | ||
797 | })(); |