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