| 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_('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 | // functions, defined below. Defining them this way (rather than with |
| 183 | // "function foo() {...}" makes JSHint happy. |
| 184 | var toXDataWindow, onZoomStart, onZoom, onZoomEnd, doZoom, isMouseInPanZone, |
| 185 | onPanStart, onPan, onPanEnd, doPan, onCanvasMouseMove, applyBrowserZoomLevel; |
| 186 | |
| 187 | // Touch event functions |
| 188 | var onZoomHandleTouchEvent, onCanvasTouchEvent, addTouchEvents; |
| 189 | |
| 190 | toXDataWindow = function(zoomHandleStatus) { |
| 191 | var xDataLimits = self.dygraph_.xAxisExtremes(); |
| 192 | var fact = (xDataLimits[1] - xDataLimits[0])/self.canvasRect_.w; |
| 193 | var xDataMin = xDataLimits[0] + (zoomHandleStatus.leftHandlePos - self.canvasRect_.x)*fact; |
| 194 | var xDataMax = xDataLimits[0] + (zoomHandleStatus.rightHandlePos - self.canvasRect_.x)*fact; |
| 195 | return [xDataMin, xDataMax]; |
| 196 | }; |
| 197 | |
| 198 | applyBrowserZoomLevel = function(delX) { |
| 199 | var zoom = window.outerWidth/document.documentElement.clientWidth; |
| 200 | if (!isNaN(zoom)) { |
| 201 | return delX/zoom; |
| 202 | } else { |
| 203 | return delX; |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | onZoomStart = function(e) { |
| 208 | Dygraph.cancelEvent(e); |
| 209 | isZooming = true; |
| 210 | xLast = e.screenX; |
| 211 | handle = e.target ? e.target : e.srcElement; |
| 212 | self.dygraph_.addEvent(topElem, 'mousemove', onZoom); |
| 213 | self.dygraph_.addEvent(topElem, 'mouseup', onZoomEnd); |
| 214 | self.fgcanvas_.style.cursor = 'col-resize'; |
| 215 | return true; |
| 216 | }; |
| 217 | |
| 218 | onZoom = function(e) { |
| 219 | if (!isZooming) { |
| 220 | return false; |
| 221 | } |
| 222 | Dygraph.cancelEvent(e); |
| 223 | var delX = e.screenX - xLast; |
| 224 | if (Math.abs(delX) < 4 || e.screenX == 0) { // First iPad move event seems to have screenX = 0 |
| 225 | return true; |
| 226 | } |
| 227 | xLast = e.screenX; |
| 228 | delX = applyBrowserZoomLevel(delX); |
| 229 | |
| 230 | // Move handle. |
| 231 | var zoomHandleStatus = self.getZoomHandleStatus_(); |
| 232 | var newPos; |
| 233 | if (handle == self.leftZoomHandle_) { |
| 234 | newPos = zoomHandleStatus.leftHandlePos + delX; |
| 235 | newPos = Math.min(newPos, zoomHandleStatus.rightHandlePos - handle.width - 3); |
| 236 | newPos = Math.max(newPos, self.canvasRect_.x); |
| 237 | } else { |
| 238 | newPos = zoomHandleStatus.rightHandlePos + delX; |
| 239 | newPos = Math.min(newPos, self.canvasRect_.x + self.canvasRect_.w); |
| 240 | newPos = Math.max(newPos, zoomHandleStatus.leftHandlePos + handle.width + 3); |
| 241 | } |
| 242 | var halfHandleWidth = handle.width/2; |
| 243 | handle.style.left = (newPos - halfHandleWidth) + 'px'; |
| 244 | self.drawInteractiveLayer_(); |
| 245 | |
| 246 | // Zoom on the fly (if not using excanvas). |
| 247 | if (dynamic) { |
| 248 | doZoom(); |
| 249 | } |
| 250 | return true; |
| 251 | }; |
| 252 | |
| 253 | onZoomEnd = function(e) { |
| 254 | if (!isZooming) { |
| 255 | return false; |
| 256 | } |
| 257 | isZooming = false; |
| 258 | Dygraph.removeEvent(topElem, 'mousemove', onZoom); |
| 259 | Dygraph.removeEvent(topElem, 'mouseup', onZoomEnd); |
| 260 | self.fgcanvas_.style.cursor = 'default'; |
| 261 | |
| 262 | // If using excanvas, Zoom now. |
| 263 | if (!dynamic) { |
| 264 | doZoom(); |
| 265 | } |
| 266 | return true; |
| 267 | }; |
| 268 | |
| 269 | doZoom = function() { |
| 270 | try { |
| 271 | var zoomHandleStatus = self.getZoomHandleStatus_(); |
| 272 | self.isChangingRange_ = true; |
| 273 | if (!zoomHandleStatus.isZoomed) { |
| 274 | self.dygraph_.doUnzoom_(); |
| 275 | } else { |
| 276 | var xDataWindow = toXDataWindow(zoomHandleStatus); |
| 277 | self.dygraph_.doZoomXDates_(xDataWindow[0], xDataWindow[1]); |
| 278 | } |
| 279 | } finally { |
| 280 | self.isChangingRange_ = false; |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | isMouseInPanZone = function(e) { |
| 285 | if (self.isUsingExcanvas_) { |
| 286 | return e.srcElement == self.iePanOverlay_; |
| 287 | } else { |
| 288 | var rect = self.leftZoomHandle_.getBoundingClientRect(); |
| 289 | var leftHandleClientX = rect.left + rect.width/2; |
| 290 | rect = self.rightZoomHandle_.getBoundingClientRect(); |
| 291 | var rightHandleClientX = rect.left + rect.width/2; |
| 292 | return (e.clientX > leftHandleClientX && e.clientX < rightHandleClientX); |
| 293 | } |
| 294 | }; |
| 295 | |
| 296 | onPanStart = function(e) { |
| 297 | if (!isPanning && isMouseInPanZone(e) && self.getZoomHandleStatus_().isZoomed) { |
| 298 | Dygraph.cancelEvent(e); |
| 299 | isPanning = true; |
| 300 | xLast = e.screenX; |
| 301 | self.dygraph_.addEvent(topElem, 'mousemove', onPan); |
| 302 | self.dygraph_.addEvent(topElem, 'mouseup', onPanEnd); |
| 303 | return true; |
| 304 | } |
| 305 | return false; |
| 306 | }; |
| 307 | |
| 308 | onPan = function(e) { |
| 309 | if (!isPanning) { |
| 310 | return false; |
| 311 | } |
| 312 | Dygraph.cancelEvent(e); |
| 313 | |
| 314 | var delX = e.screenX - xLast; |
| 315 | if (Math.abs(delX) < 4) { |
| 316 | return true; |
| 317 | } |
| 318 | xLast = e.screenX; |
| 319 | delX = applyBrowserZoomLevel(delX); |
| 320 | |
| 321 | // Move range view |
| 322 | var zoomHandleStatus = self.getZoomHandleStatus_(); |
| 323 | var leftHandlePos = zoomHandleStatus.leftHandlePos; |
| 324 | var rightHandlePos = zoomHandleStatus.rightHandlePos; |
| 325 | var rangeSize = rightHandlePos - leftHandlePos; |
| 326 | if (leftHandlePos + delX <= self.canvasRect_.x) { |
| 327 | leftHandlePos = self.canvasRect_.x; |
| 328 | rightHandlePos = leftHandlePos + rangeSize; |
| 329 | } else if (rightHandlePos + delX >= self.canvasRect_.x + self.canvasRect_.w) { |
| 330 | rightHandlePos = self.canvasRect_.x + self.canvasRect_.w; |
| 331 | leftHandlePos = rightHandlePos - rangeSize; |
| 332 | } else { |
| 333 | leftHandlePos += delX; |
| 334 | rightHandlePos += delX; |
| 335 | } |
| 336 | var halfHandleWidth = self.leftZoomHandle_.width/2; |
| 337 | self.leftZoomHandle_.style.left = (leftHandlePos - halfHandleWidth) + 'px'; |
| 338 | self.rightZoomHandle_.style.left = (rightHandlePos - halfHandleWidth) + 'px'; |
| 339 | self.drawInteractiveLayer_(); |
| 340 | |
| 341 | // Do pan on the fly (if not using excanvas). |
| 342 | if (dynamic) { |
| 343 | doPan(); |
| 344 | } |
| 345 | return true; |
| 346 | }; |
| 347 | |
| 348 | onPanEnd = function(e) { |
| 349 | if (!isPanning) { |
| 350 | return false; |
| 351 | } |
| 352 | isPanning = false; |
| 353 | Dygraph.removeEvent(topElem, 'mousemove', onPan); |
| 354 | Dygraph.removeEvent(topElem, 'mouseup', onPanEnd); |
| 355 | // If using excanvas, do pan now. |
| 356 | if (!dynamic) { |
| 357 | doPan(); |
| 358 | } |
| 359 | return true; |
| 360 | }; |
| 361 | |
| 362 | doPan = function() { |
| 363 | try { |
| 364 | self.isChangingRange_ = true; |
| 365 | self.dygraph_.dateWindow_ = toXDataWindow(self.getZoomHandleStatus_()); |
| 366 | self.dygraph_.drawGraph_(false); |
| 367 | } finally { |
| 368 | self.isChangingRange_ = false; |
| 369 | } |
| 370 | }; |
| 371 | |
| 372 | onCanvasMouseMove = function(e) { |
| 373 | if (isZooming || isPanning) { |
| 374 | return; |
| 375 | } |
| 376 | var cursor = isMouseInPanZone(e) ? 'move' : 'default'; |
| 377 | if (cursor != self.fgcanvas_.style.cursor) { |
| 378 | self.fgcanvas_.style.cursor = cursor; |
| 379 | } |
| 380 | }; |
| 381 | |
| 382 | onZoomHandleTouchEvent = function(e) { |
| 383 | if (e.type == 'touchstart' && e.targetTouches.length == 1) { |
| 384 | if (onZoomStart(e.targetTouches[0])) { |
| 385 | Dygraph.cancelEvent(e); |
| 386 | } |
| 387 | } else if (e.type == 'touchmove' && e.targetTouches.length == 1) { |
| 388 | if (onZoom(e.targetTouches[0])) { |
| 389 | Dygraph.cancelEvent(e); |
| 390 | } |
| 391 | } else { |
| 392 | onZoomEnd(e); |
| 393 | } |
| 394 | }; |
| 395 | |
| 396 | onCanvasTouchEvent = function(e) { |
| 397 | if (e.type == 'touchstart' && e.targetTouches.length == 1) { |
| 398 | if (onPanStart(e.targetTouches[0])) { |
| 399 | Dygraph.cancelEvent(e); |
| 400 | } |
| 401 | } else if (e.type == 'touchmove' && e.targetTouches.length == 1) { |
| 402 | if (onPan(e.targetTouches[0])) { |
| 403 | Dygraph.cancelEvent(e); |
| 404 | } |
| 405 | } else { |
| 406 | onPanEnd(e); |
| 407 | } |
| 408 | }; |
| 409 | |
| 410 | addTouchEvents = function(elem, fn) { |
| 411 | var types = ['touchstart', 'touchend', 'touchmove', 'touchcancel']; |
| 412 | for (var i = 0; i < types.length; i++) { |
| 413 | self.dygraph_.addEvent(elem, types[i], fn); |
| 414 | } |
| 415 | }; |
| 416 | |
| 417 | this.dygraph_.attrs_.interactionModel = |
| 418 | Dygraph.Interaction.dragIsPanInteractionModel; |
| 419 | this.dygraph_.attrs_.panEdgeFraction = 0.0001; |
| 420 | |
| 421 | var dragStartEvent = window.opera ? 'mousedown' : 'dragstart'; |
| 422 | this.dygraph_.addEvent(this.leftZoomHandle_, dragStartEvent, onZoomStart); |
| 423 | this.dygraph_.addEvent(this.rightZoomHandle_, dragStartEvent, onZoomStart); |
| 424 | |
| 425 | if (this.isUsingExcanvas_) { |
| 426 | this.dygraph_.addEvent(this.iePanOverlay_, 'mousedown', onPanStart); |
| 427 | } else { |
| 428 | this.dygraph_.addEvent(this.fgcanvas_, 'mousedown', onPanStart); |
| 429 | this.dygraph_.addEvent(this.fgcanvas_, 'mousemove', onCanvasMouseMove); |
| 430 | } |
| 431 | |
| 432 | // Touch events |
| 433 | if (this.hasTouchInterface_) { |
| 434 | addTouchEvents(this.leftZoomHandle_, onZoomHandleTouchEvent); |
| 435 | addTouchEvents(this.rightZoomHandle_, onZoomHandleTouchEvent); |
| 436 | addTouchEvents(this.fgcanvas_, onCanvasTouchEvent); |
| 437 | } |
| 438 | }; |
| 439 | |
| 440 | /** |
| 441 | * @private |
| 442 | * Draws the static layer in the background canvas. |
| 443 | */ |
| 444 | DygraphRangeSelector.prototype.drawStaticLayer_ = function() { |
| 445 | var ctx = this.bgcanvas_ctx_; |
| 446 | ctx.clearRect(0, 0, this.canvasRect_.w, this.canvasRect_.h); |
| 447 | try { |
| 448 | this.drawMiniPlot_(); |
| 449 | } catch(ex) { |
| 450 | Dygraph.warn(ex); |
| 451 | } |
| 452 | |
| 453 | var margin = 0.5; |
| 454 | this.bgcanvas_ctx_.lineWidth = 1; |
| 455 | ctx.strokeStyle = 'gray'; |
| 456 | ctx.beginPath(); |
| 457 | ctx.moveTo(margin, margin); |
| 458 | ctx.lineTo(margin, this.canvasRect_.h-margin); |
| 459 | ctx.lineTo(this.canvasRect_.w-margin, this.canvasRect_.h-margin); |
| 460 | ctx.lineTo(this.canvasRect_.w-margin, margin); |
| 461 | ctx.stroke(); |
| 462 | }; |
| 463 | |
| 464 | |
| 465 | /** |
| 466 | * @private |
| 467 | * Draws the mini plot in the background canvas. |
| 468 | */ |
| 469 | DygraphRangeSelector.prototype.drawMiniPlot_ = function() { |
| 470 | var fillStyle = this.attr_('rangeSelectorPlotFillColor'); |
| 471 | var strokeStyle = this.attr_('rangeSelectorPlotStrokeColor'); |
| 472 | if (!fillStyle && !strokeStyle) { |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | var combinedSeriesData = this.computeCombinedSeriesAndLimits_(); |
| 477 | var yRange = combinedSeriesData.yMax - combinedSeriesData.yMin; |
| 478 | |
| 479 | // Draw the mini plot. |
| 480 | var ctx = this.bgcanvas_ctx_; |
| 481 | var margin = 0.5; |
| 482 | |
| 483 | var xExtremes = this.dygraph_.xAxisExtremes(); |
| 484 | var xRange = Math.max(xExtremes[1] - xExtremes[0], 1.e-30); |
| 485 | var xFact = (this.canvasRect_.w - margin)/xRange; |
| 486 | var yFact = (this.canvasRect_.h - margin)/yRange; |
| 487 | var canvasWidth = this.canvasRect_.w - margin; |
| 488 | var canvasHeight = this.canvasRect_.h - margin; |
| 489 | |
| 490 | ctx.beginPath(); |
| 491 | ctx.moveTo(margin, canvasHeight); |
| 492 | for (var i = 0; i < combinedSeriesData.data.length; i++) { |
| 493 | var dataPoint = combinedSeriesData.data[i]; |
| 494 | var x = (dataPoint[0] - xExtremes[0])*xFact; |
| 495 | var y = canvasHeight - (dataPoint[1] - combinedSeriesData.yMin)*yFact; |
| 496 | if (isFinite(x) && isFinite(y)) { |
| 497 | ctx.lineTo(x, y); |
| 498 | } |
| 499 | } |
| 500 | ctx.lineTo(canvasWidth, canvasHeight); |
| 501 | ctx.closePath(); |
| 502 | |
| 503 | if (fillStyle) { |
| 504 | var lingrad = this.bgcanvas_ctx_.createLinearGradient(0, 0, 0, canvasHeight); |
| 505 | lingrad.addColorStop(0, 'white'); |
| 506 | lingrad.addColorStop(1, fillStyle); |
| 507 | this.bgcanvas_ctx_.fillStyle = lingrad; |
| 508 | ctx.fill(); |
| 509 | } |
| 510 | |
| 511 | if (strokeStyle) { |
| 512 | this.bgcanvas_ctx_.strokeStyle = strokeStyle; |
| 513 | this.bgcanvas_ctx_.lineWidth = 1.5; |
| 514 | ctx.stroke(); |
| 515 | } |
| 516 | }; |
| 517 | |
| 518 | /** |
| 519 | * @private |
| 520 | * Computes and returns the combinded series data along with min/max for the mini plot. |
| 521 | * @return {Object} An object containing combinded series array, ymin, ymax. |
| 522 | */ |
| 523 | DygraphRangeSelector.prototype.computeCombinedSeriesAndLimits_ = function() { |
| 524 | var data = this.dygraph_.rawData_; |
| 525 | var logscale = this.attr_('logscale'); |
| 526 | |
| 527 | // Create a combined series (average of all series values). |
| 528 | var combinedSeries = []; |
| 529 | var sum; |
| 530 | var count; |
| 531 | var mutipleValues; |
| 532 | var i, j, k; |
| 533 | var xVal, yVal; |
| 534 | |
| 535 | // Find out if data has multiple values per datapoint. |
| 536 | // Go to first data point that actually has values (see http://code.google.com/p/dygraphs/issues/detail?id=246) |
| 537 | for (i = 0; i < data.length; i++) { |
| 538 | if (data[i].length > 1 && data[i][1] !== null) { |
| 539 | mutipleValues = typeof data[i][1] != 'number'; |
| 540 | if (mutipleValues) { |
| 541 | sum = []; |
| 542 | count = []; |
| 543 | for (k = 0; k < data[i][1].length; k++) { |
| 544 | sum.push(0); |
| 545 | count.push(0); |
| 546 | } |
| 547 | } |
| 548 | break; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | for (i = 0; i < data.length; i++) { |
| 553 | var dataPoint = data[i]; |
| 554 | xVal = dataPoint[0]; |
| 555 | |
| 556 | if (mutipleValues) { |
| 557 | for (k = 0; k < sum.length; k++) { |
| 558 | sum[k] = count[k] = 0; |
| 559 | } |
| 560 | } else { |
| 561 | sum = count = 0; |
| 562 | } |
| 563 | |
| 564 | for (j = 1; j < dataPoint.length; j++) { |
| 565 | if (this.dygraph_.visibility()[j-1]) { |
| 566 | var y; |
| 567 | if (mutipleValues) { |
| 568 | for (k = 0; k < sum.length; k++) { |
| 569 | y = dataPoint[j][k]; |
| 570 | if (y === null || isNaN(y)) continue; |
| 571 | sum[k] += y; |
| 572 | count[k]++; |
| 573 | } |
| 574 | } else { |
| 575 | y = dataPoint[j]; |
| 576 | if (y === null || isNaN(y)) continue; |
| 577 | sum += y; |
| 578 | count++; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | if (mutipleValues) { |
| 584 | for (k = 0; k < sum.length; k++) { |
| 585 | sum[k] /= count[k]; |
| 586 | } |
| 587 | yVal = sum.slice(0); |
| 588 | } else { |
| 589 | yVal = sum/count; |
| 590 | } |
| 591 | |
| 592 | combinedSeries.push([xVal, yVal]); |
| 593 | } |
| 594 | |
| 595 | // Account for roll period, fractions. |
| 596 | combinedSeries = this.dygraph_.rollingAverage(combinedSeries, this.dygraph_.rollPeriod_); |
| 597 | |
| 598 | if (typeof combinedSeries[0][1] != 'number') { |
| 599 | for (i = 0; i < combinedSeries.length; i++) { |
| 600 | yVal = combinedSeries[i][1]; |
| 601 | combinedSeries[i][1] = yVal[0]; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | // Compute the y range. |
| 606 | var yMin = Number.MAX_VALUE; |
| 607 | var yMax = -Number.MAX_VALUE; |
| 608 | for (i = 0; i < combinedSeries.length; i++) { |
| 609 | yVal = combinedSeries[i][1]; |
| 610 | if (yVal !== null && isFinite(yVal) && (!logscale || yVal > 0)) { |
| 611 | yMin = Math.min(yMin, yVal); |
| 612 | yMax = Math.max(yMax, yVal); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | // Convert Y data to log scale if needed. |
| 617 | // Also, expand the Y range to compress the mini plot a little. |
| 618 | var extraPercent = 0.25; |
| 619 | if (logscale) { |
| 620 | yMax = Dygraph.log10(yMax); |
| 621 | yMax += yMax*extraPercent; |
| 622 | yMin = Dygraph.log10(yMin); |
| 623 | for (i = 0; i < combinedSeries.length; i++) { |
| 624 | combinedSeries[i][1] = Dygraph.log10(combinedSeries[i][1]); |
| 625 | } |
| 626 | } else { |
| 627 | var yExtra; |
| 628 | var yRange = yMax - yMin; |
| 629 | if (yRange <= Number.MIN_VALUE) { |
| 630 | yExtra = yMax*extraPercent; |
| 631 | } else { |
| 632 | yExtra = yRange*extraPercent; |
| 633 | } |
| 634 | yMax += yExtra; |
| 635 | yMin -= yExtra; |
| 636 | } |
| 637 | |
| 638 | return {data: combinedSeries, yMin: yMin, yMax: yMax}; |
| 639 | }; |
| 640 | |
| 641 | /** |
| 642 | * @private |
| 643 | * Places the zoom handles in the proper position based on the current X data window. |
| 644 | */ |
| 645 | DygraphRangeSelector.prototype.placeZoomHandles_ = function() { |
| 646 | var xExtremes = this.dygraph_.xAxisExtremes(); |
| 647 | var xWindowLimits = this.dygraph_.xAxisRange(); |
| 648 | var xRange = xExtremes[1] - xExtremes[0]; |
| 649 | var leftPercent = Math.max(0, (xWindowLimits[0] - xExtremes[0])/xRange); |
| 650 | var rightPercent = Math.max(0, (xExtremes[1] - xWindowLimits[1])/xRange); |
| 651 | var leftCoord = this.canvasRect_.x + this.canvasRect_.w*leftPercent; |
| 652 | var rightCoord = this.canvasRect_.x + this.canvasRect_.w*(1 - rightPercent); |
| 653 | var handleTop = Math.max(this.canvasRect_.y, this.canvasRect_.y + (this.canvasRect_.h - this.leftZoomHandle_.height)/2); |
| 654 | var halfHandleWidth = this.leftZoomHandle_.width/2; |
| 655 | this.leftZoomHandle_.style.left = (leftCoord - halfHandleWidth) + 'px'; |
| 656 | this.leftZoomHandle_.style.top = handleTop + 'px'; |
| 657 | this.rightZoomHandle_.style.left = (rightCoord - halfHandleWidth) + 'px'; |
| 658 | this.rightZoomHandle_.style.top = this.leftZoomHandle_.style.top; |
| 659 | |
| 660 | this.leftZoomHandle_.style.visibility = 'visible'; |
| 661 | this.rightZoomHandle_.style.visibility = 'visible'; |
| 662 | }; |
| 663 | |
| 664 | /** |
| 665 | * @private |
| 666 | * Draws the interactive layer in the foreground canvas. |
| 667 | */ |
| 668 | DygraphRangeSelector.prototype.drawInteractiveLayer_ = function() { |
| 669 | var ctx = this.fgcanvas_ctx_; |
| 670 | ctx.clearRect(0, 0, this.canvasRect_.w, this.canvasRect_.h); |
| 671 | var margin = 1; |
| 672 | var width = this.canvasRect_.w - margin; |
| 673 | var height = this.canvasRect_.h - margin; |
| 674 | var zoomHandleStatus = this.getZoomHandleStatus_(); |
| 675 | |
| 676 | ctx.strokeStyle = 'black'; |
| 677 | if (!zoomHandleStatus.isZoomed) { |
| 678 | ctx.beginPath(); |
| 679 | ctx.moveTo(margin, margin); |
| 680 | ctx.lineTo(margin, height); |
| 681 | ctx.lineTo(width, height); |
| 682 | ctx.lineTo(width, margin); |
| 683 | ctx.stroke(); |
| 684 | if (this.iePanOverlay_) { |
| 685 | this.iePanOverlay_.style.display = 'none'; |
| 686 | } |
| 687 | } else { |
| 688 | var leftHandleCanvasPos = Math.max(margin, zoomHandleStatus.leftHandlePos - this.canvasRect_.x); |
| 689 | var rightHandleCanvasPos = Math.min(width, zoomHandleStatus.rightHandlePos - this.canvasRect_.x); |
| 690 | |
| 691 | ctx.fillStyle = 'rgba(240, 240, 240, 0.6)'; |
| 692 | ctx.fillRect(0, 0, leftHandleCanvasPos, this.canvasRect_.h); |
| 693 | ctx.fillRect(rightHandleCanvasPos, 0, this.canvasRect_.w - rightHandleCanvasPos, this.canvasRect_.h); |
| 694 | |
| 695 | ctx.beginPath(); |
| 696 | ctx.moveTo(margin, margin); |
| 697 | ctx.lineTo(leftHandleCanvasPos, margin); |
| 698 | ctx.lineTo(leftHandleCanvasPos, height); |
| 699 | ctx.lineTo(rightHandleCanvasPos, height); |
| 700 | ctx.lineTo(rightHandleCanvasPos, margin); |
| 701 | ctx.lineTo(width, margin); |
| 702 | ctx.stroke(); |
| 703 | |
| 704 | if (this.isUsingExcanvas_) { |
| 705 | this.iePanOverlay_.style.width = (rightHandleCanvasPos - leftHandleCanvasPos) + 'px'; |
| 706 | this.iePanOverlay_.style.left = leftHandleCanvasPos + 'px'; |
| 707 | this.iePanOverlay_.style.height = height + 'px'; |
| 708 | this.iePanOverlay_.style.display = 'inline'; |
| 709 | } |
| 710 | } |
| 711 | }; |
| 712 | |
| 713 | /** |
| 714 | * @private |
| 715 | * Returns the current zoom handle position information. |
| 716 | * @return {Object} The zoom handle status. |
| 717 | */ |
| 718 | DygraphRangeSelector.prototype.getZoomHandleStatus_ = function() { |
| 719 | var halfHandleWidth = this.leftZoomHandle_.width/2; |
| 720 | var leftHandlePos = parseInt(this.leftZoomHandle_.style.left, 10) + halfHandleWidth; |
| 721 | var rightHandlePos = parseInt(this.rightZoomHandle_.style.left, 10) + halfHandleWidth; |
| 722 | return { |
| 723 | leftHandlePos: leftHandlePos, |
| 724 | rightHandlePos: rightHandlePos, |
| 725 | isZoomed: (leftHandlePos - 1 > this.canvasRect_.x || rightHandlePos + 1 < this.canvasRect_.x+this.canvasRect_.w) |
| 726 | }; |
| 727 | }; |