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