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