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