| 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2006 Dan Vanderkam (danvdk@gmail.com) |
| 4 | * MIT-licensed (http://opensource.org/licenses/MIT) |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @fileoverview Based on PlotKit.CanvasRenderer, but modified to meet the |
| 9 | * needs of dygraphs. |
| 10 | * |
| 11 | * In particular, support for: |
| 12 | * - grid overlays |
| 13 | * - error bars |
| 14 | * - dygraphs attribute system |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * The DygraphCanvasRenderer class does the actual rendering of the chart onto |
| 19 | * a canvas. It's based on PlotKit.CanvasRenderer. |
| 20 | * @param {Object} element The canvas to attach to |
| 21 | * @param {Object} elementContext The 2d context of the canvas (injected so it |
| 22 | * can be mocked for testing.) |
| 23 | * @param {Layout} layout The DygraphLayout object for this graph. |
| 24 | * @constructor |
| 25 | */ |
| 26 | DygraphCanvasRenderer = function(dygraph, element, elementContext, layout) { |
| 27 | this.dygraph_ = dygraph; |
| 28 | |
| 29 | this.layout = layout; |
| 30 | this.element = element; |
| 31 | this.elementContext = elementContext; |
| 32 | this.container = this.element.parentNode; |
| 33 | |
| 34 | this.height = this.element.height; |
| 35 | this.width = this.element.width; |
| 36 | |
| 37 | // --- check whether everything is ok before we return |
| 38 | if (!this.isIE && !(DygraphCanvasRenderer.isSupported(this.element))) |
| 39 | throw "Canvas is not supported."; |
| 40 | |
| 41 | // internal state |
| 42 | this.xlabels = new Array(); |
| 43 | this.ylabels = new Array(); |
| 44 | this.annotations = new Array(); |
| 45 | this.chartLabels = {}; |
| 46 | |
| 47 | this.area = layout.getPlotArea(); |
| 48 | this.container.style.position = "relative"; |
| 49 | this.container.style.width = this.width + "px"; |
| 50 | |
| 51 | // Set up a clipping area for the canvas (and the interaction canvas). |
| 52 | // This ensures that we don't overdraw. |
| 53 | if (this.dygraph_.isUsingExcanvas_) { |
| 54 | this._createIEClipArea(); |
| 55 | } else { |
| 56 | var ctx = this.dygraph_.canvas_ctx_; |
| 57 | ctx.beginPath(); |
| 58 | ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); |
| 59 | ctx.clip(); |
| 60 | |
| 61 | ctx = this.dygraph_.hidden_ctx_; |
| 62 | ctx.beginPath(); |
| 63 | ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); |
| 64 | ctx.clip(); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | DygraphCanvasRenderer.prototype.attr_ = function(x) { |
| 69 | return this.dygraph_.attr_(x); |
| 70 | }; |
| 71 | |
| 72 | DygraphCanvasRenderer.prototype.clear = function() { |
| 73 | if (this.isIE) { |
| 74 | // VML takes a while to start up, so we just poll every this.IEDelay |
| 75 | try { |
| 76 | if (this.clearDelay) { |
| 77 | this.clearDelay.cancel(); |
| 78 | this.clearDelay = null; |
| 79 | } |
| 80 | var context = this.elementContext; |
| 81 | } |
| 82 | catch (e) { |
| 83 | // TODO(danvk): this is broken, since MochiKit.Async is gone. |
| 84 | this.clearDelay = MochiKit.Async.wait(this.IEDelay); |
| 85 | this.clearDelay.addCallback(bind(this.clear, this)); |
| 86 | return; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | var context = this.elementContext; |
| 91 | context.clearRect(0, 0, this.width, this.height); |
| 92 | |
| 93 | for (var i = 0; i < this.xlabels.length; i++) { |
| 94 | var el = this.xlabels[i]; |
| 95 | if (el.parentNode) el.parentNode.removeChild(el); |
| 96 | } |
| 97 | for (var i = 0; i < this.ylabels.length; i++) { |
| 98 | var el = this.ylabels[i]; |
| 99 | if (el.parentNode) el.parentNode.removeChild(el); |
| 100 | } |
| 101 | for (var i = 0; i < this.annotations.length; i++) { |
| 102 | var el = this.annotations[i]; |
| 103 | if (el.parentNode) el.parentNode.removeChild(el); |
| 104 | } |
| 105 | for (var k in this.chartLabels) { |
| 106 | if (!this.chartLabels.hasOwnProperty(k)) continue; |
| 107 | var el = this.chartLabels[k]; |
| 108 | if (el.parentNode) el.parentNode.removeChild(el); |
| 109 | } |
| 110 | this.xlabels = new Array(); |
| 111 | this.ylabels = new Array(); |
| 112 | this.annotations = new Array(); |
| 113 | this.chartLabels = {}; |
| 114 | }; |
| 115 | |
| 116 | |
| 117 | DygraphCanvasRenderer.isSupported = function(canvasName) { |
| 118 | var canvas = null; |
| 119 | try { |
| 120 | if (typeof(canvasName) == 'undefined' || canvasName == null) |
| 121 | canvas = document.createElement("canvas"); |
| 122 | else |
| 123 | canvas = canvasName; |
| 124 | var context = canvas.getContext("2d"); |
| 125 | } |
| 126 | catch (e) { |
| 127 | var ie = navigator.appVersion.match(/MSIE (\d\.\d)/); |
| 128 | var opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1); |
| 129 | if ((!ie) || (ie[1] < 6) || (opera)) |
| 130 | return false; |
| 131 | return true; |
| 132 | } |
| 133 | return true; |
| 134 | }; |
| 135 | |
| 136 | /** |
| 137 | * @param { [String] } colors Array of color strings. Should have one entry for |
| 138 | * each series to be rendered. |
| 139 | */ |
| 140 | DygraphCanvasRenderer.prototype.setColors = function(colors) { |
| 141 | this.colorScheme_ = colors; |
| 142 | }; |
| 143 | |
| 144 | /** |
| 145 | * Draw an X/Y grid on top of the existing plot |
| 146 | */ |
| 147 | DygraphCanvasRenderer.prototype.render = function() { |
| 148 | // Draw the new X/Y grid. Lines appear crisper when pixels are rounded to |
| 149 | // half-integers. This prevents them from drawing in two rows/cols. |
| 150 | var ctx = this.elementContext; |
| 151 | function halfUp(x){return Math.round(x)+0.5}; |
| 152 | function halfDown(y){return Math.round(y)-0.5}; |
| 153 | |
| 154 | if (this.attr_('underlayCallback')) { |
| 155 | // NOTE: we pass the dygraph object to this callback twice to avoid breaking |
| 156 | // users who expect a deprecated form of this callback. |
| 157 | this.attr_('underlayCallback')(ctx, this.area, this.dygraph_, this.dygraph_); |
| 158 | } |
| 159 | |
| 160 | if (this.attr_('drawYGrid')) { |
| 161 | var ticks = this.layout.yticks; |
| 162 | // TODO(konigsberg): I don't think these calls to save() have a corresponding restore(). |
| 163 | ctx.save(); |
| 164 | ctx.strokeStyle = this.attr_('gridLineColor'); |
| 165 | ctx.lineWidth = this.attr_('gridLineWidth'); |
| 166 | for (var i = 0; i < ticks.length; i++) { |
| 167 | // TODO(danvk): allow secondary axes to draw a grid, too. |
| 168 | if (ticks[i][0] != 0) continue; |
| 169 | var x = halfUp(this.area.x); |
| 170 | var y = halfDown(this.area.y + ticks[i][1] * this.area.h); |
| 171 | ctx.beginPath(); |
| 172 | ctx.moveTo(x, y); |
| 173 | ctx.lineTo(x + this.area.w, y); |
| 174 | ctx.closePath(); |
| 175 | ctx.stroke(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (this.attr_('drawXGrid')) { |
| 180 | var ticks = this.layout.xticks; |
| 181 | ctx.save(); |
| 182 | ctx.strokeStyle = this.attr_('gridLineColor'); |
| 183 | ctx.lineWidth = this.attr_('gridLineWidth'); |
| 184 | for (var i=0; i<ticks.length; i++) { |
| 185 | var x = halfUp(this.area.x + ticks[i][0] * this.area.w); |
| 186 | var y = halfDown(this.area.y + this.area.h); |
| 187 | ctx.beginPath(); |
| 188 | ctx.moveTo(x, y); |
| 189 | ctx.lineTo(x, this.area.y); |
| 190 | ctx.closePath(); |
| 191 | ctx.stroke(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Do the ordinary rendering, as before |
| 196 | this._renderLineChart(); |
| 197 | this._renderAxis(); |
| 198 | this._renderChartLabels(); |
| 199 | this._renderAnnotations(); |
| 200 | }; |
| 201 | |
| 202 | DygraphCanvasRenderer.prototype._createIEClipArea = function() { |
| 203 | var className = 'dygraph-clip-div'; |
| 204 | var graphDiv = this.dygraph_.graphDiv; |
| 205 | |
| 206 | // Remove old clip divs. |
| 207 | for (var i = graphDiv.childNodes.length-1; i >= 0; i--) { |
| 208 | if (graphDiv.childNodes[i].className == className) { |
| 209 | graphDiv.removeChild(graphDiv.childNodes[i]); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // Determine background color to give clip divs. |
| 214 | var backgroundColor = document.bgColor; |
| 215 | var element = this.dygraph_.graphDiv; |
| 216 | while (element != document) { |
| 217 | var bgcolor = element.currentStyle.backgroundColor; |
| 218 | if (bgcolor && bgcolor != 'transparent') { |
| 219 | backgroundColor = bgcolor; |
| 220 | break; |
| 221 | } |
| 222 | element = element.parentNode; |
| 223 | } |
| 224 | |
| 225 | function createClipDiv(area) { |
| 226 | if (area.w == 0 || area.h == 0) { |
| 227 | return; |
| 228 | } |
| 229 | var elem = document.createElement('div'); |
| 230 | elem.className = className; |
| 231 | elem.style.backgroundColor = backgroundColor; |
| 232 | elem.style.position = 'absolute'; |
| 233 | elem.style.left = area.x + 'px'; |
| 234 | elem.style.top = area.y + 'px'; |
| 235 | elem.style.width = area.w + 'px'; |
| 236 | elem.style.height = area.h + 'px'; |
| 237 | graphDiv.appendChild(elem); |
| 238 | } |
| 239 | |
| 240 | var plotArea = this.area; |
| 241 | // Left side |
| 242 | createClipDiv({x:0, y:0, w:plotArea.x, h:this.height}); |
| 243 | // Top |
| 244 | createClipDiv({x:plotArea.x, y:0, w:this.width-plotArea.x, h:plotArea.y}); |
| 245 | // Right side |
| 246 | createClipDiv({x:plotArea.x+plotArea.w, y:0, w:this.width-plotArea.x-plotArea.w, h:this.height}); |
| 247 | // Bottom |
| 248 | createClipDiv({x:plotArea.x, y:plotArea.y+plotArea.h, w:this.width-plotArea.x, h:this.height-plotArea.h-plotArea.y}); |
| 249 | } |
| 250 | |
| 251 | DygraphCanvasRenderer.prototype._renderAxis = function() { |
| 252 | if (!this.attr_('drawXAxis') && !this.attr_('drawYAxis')) return; |
| 253 | |
| 254 | // Round pixels to half-integer boundaries for crisper drawing. |
| 255 | function halfUp(x){return Math.round(x)+0.5}; |
| 256 | function halfDown(y){return Math.round(y)-0.5}; |
| 257 | |
| 258 | var context = this.elementContext; |
| 259 | |
| 260 | var labelStyle = { |
| 261 | position: "absolute", |
| 262 | fontSize: this.attr_('axisLabelFontSize') + "px", |
| 263 | zIndex: 10, |
| 264 | color: this.attr_('axisLabelColor'), |
| 265 | width: this.attr_('axisLabelWidth') + "px", |
| 266 | // height: this.attr_('axisLabelFontSize') + 2 + "px", |
| 267 | lineHeight: "normal", // Something other than "normal" line-height screws up label positioning. |
| 268 | overflow: "hidden" |
| 269 | }; |
| 270 | var makeDiv = function(txt, axis, prec_axis) { |
| 271 | var div = document.createElement("div"); |
| 272 | for (var name in labelStyle) { |
| 273 | if (labelStyle.hasOwnProperty(name)) { |
| 274 | div.style[name] = labelStyle[name]; |
| 275 | } |
| 276 | } |
| 277 | var inner_div = document.createElement("div"); |
| 278 | inner_div.className = 'dygraph-axis-label' + |
| 279 | ' dygraph-axis-label-' + axis + |
| 280 | (prec_axis ? ' dygraph-axis-label-' + prec_axis : ''); |
| 281 | inner_div.appendChild(document.createTextNode(txt)); |
| 282 | div.appendChild(inner_div); |
| 283 | return div; |
| 284 | }; |
| 285 | |
| 286 | // axis lines |
| 287 | context.save(); |
| 288 | context.strokeStyle = this.attr_('axisLineColor'); |
| 289 | context.lineWidth = this.attr_('axisLineWidth'); |
| 290 | |
| 291 | if (this.attr_('drawYAxis')) { |
| 292 | if (this.layout.yticks && this.layout.yticks.length > 0) { |
| 293 | var num_axes = this.dygraph_.numAxes(); |
| 294 | for (var i = 0; i < this.layout.yticks.length; i++) { |
| 295 | var tick = this.layout.yticks[i]; |
| 296 | if (typeof(tick) == "function") return; |
| 297 | var x = this.area.x; |
| 298 | var sgn = 1; |
| 299 | var prec_axis = 'y1'; |
| 300 | if (tick[0] == 1) { // right-side y-axis |
| 301 | x = this.area.x + this.area.w; |
| 302 | sgn = -1; |
| 303 | prec_axis = 'y2'; |
| 304 | } |
| 305 | var y = this.area.y + tick[1] * this.area.h; |
| 306 | |
| 307 | /* Tick marks are currently clipped, so don't bother drawing them. |
| 308 | context.beginPath(); |
| 309 | context.moveTo(halfUp(x), halfDown(y)); |
| 310 | context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize')), halfDown(y)); |
| 311 | context.closePath(); |
| 312 | context.stroke(); |
| 313 | */ |
| 314 | |
| 315 | var label = makeDiv(tick[2], 'y', num_axes == 2 ? prec_axis : null); |
| 316 | var top = (y - this.attr_('axisLabelFontSize') / 2); |
| 317 | if (top < 0) top = 0; |
| 318 | |
| 319 | if (top + this.attr_('axisLabelFontSize') + 3 > this.height) { |
| 320 | label.style.bottom = "0px"; |
| 321 | } else { |
| 322 | label.style.top = top + "px"; |
| 323 | } |
| 324 | if (tick[0] == 0) { |
| 325 | label.style.left = (this.area.x - this.attr_('yAxisLabelWidth') - this.attr_('axisTickSize')) + "px"; |
| 326 | label.style.textAlign = "right"; |
| 327 | } else if (tick[0] == 1) { |
| 328 | label.style.left = (this.area.x + this.area.w + |
| 329 | this.attr_('axisTickSize')) + "px"; |
| 330 | label.style.textAlign = "left"; |
| 331 | } |
| 332 | label.style.width = this.attr_('yAxisLabelWidth') + "px"; |
| 333 | this.container.appendChild(label); |
| 334 | this.ylabels.push(label); |
| 335 | } |
| 336 | |
| 337 | // The lowest tick on the y-axis often overlaps with the leftmost |
| 338 | // tick on the x-axis. Shift the bottom tick up a little bit to |
| 339 | // compensate if necessary. |
| 340 | var bottomTick = this.ylabels[0]; |
| 341 | var fontSize = this.attr_('axisLabelFontSize'); |
| 342 | var bottom = parseInt(bottomTick.style.top) + fontSize; |
| 343 | if (bottom > this.height - fontSize) { |
| 344 | bottomTick.style.top = (parseInt(bottomTick.style.top) - |
| 345 | fontSize / 2) + "px"; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // draw a vertical line on the left to separate the chart from the labels. |
| 350 | context.beginPath(); |
| 351 | context.moveTo(halfUp(this.area.x), halfDown(this.area.y)); |
| 352 | context.lineTo(halfUp(this.area.x), halfDown(this.area.y + this.area.h)); |
| 353 | context.closePath(); |
| 354 | context.stroke(); |
| 355 | |
| 356 | // if there's a secondary y-axis, draw a vertical line for that, too. |
| 357 | if (this.dygraph_.numAxes() == 2) { |
| 358 | context.beginPath(); |
| 359 | context.moveTo(halfDown(this.area.x + this.area.w), halfDown(this.area.y)); |
| 360 | context.lineTo(halfDown(this.area.x + this.area.w), halfDown(this.area.y + this.area.h)); |
| 361 | context.closePath(); |
| 362 | context.stroke(); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | if (this.attr_('drawXAxis')) { |
| 367 | if (this.layout.xticks) { |
| 368 | for (var i = 0; i < this.layout.xticks.length; i++) { |
| 369 | var tick = this.layout.xticks[i]; |
| 370 | if (typeof(dataset) == "function") return; |
| 371 | |
| 372 | var x = this.area.x + tick[0] * this.area.w; |
| 373 | var y = this.area.y + this.area.h; |
| 374 | |
| 375 | /* Tick marks are currently clipped, so don't bother drawing them. |
| 376 | context.beginPath(); |
| 377 | context.moveTo(halfUp(x), halfDown(y)); |
| 378 | context.lineTo(halfUp(x), halfDown(y + this.attr_('axisTickSize'))); |
| 379 | context.closePath(); |
| 380 | context.stroke(); |
| 381 | */ |
| 382 | |
| 383 | var label = makeDiv(tick[1], 'x'); |
| 384 | label.style.textAlign = "center"; |
| 385 | label.style.top = (y + this.attr_('axisTickSize')) + 'px'; |
| 386 | |
| 387 | var left = (x - this.attr_('axisLabelWidth')/2); |
| 388 | if (left + this.attr_('axisLabelWidth') > this.width) { |
| 389 | left = this.width - this.attr_('xAxisLabelWidth'); |
| 390 | label.style.textAlign = "right"; |
| 391 | } |
| 392 | if (left < 0) { |
| 393 | left = 0; |
| 394 | label.style.textAlign = "left"; |
| 395 | } |
| 396 | |
| 397 | label.style.left = left + "px"; |
| 398 | label.style.width = this.attr_('xAxisLabelWidth') + "px"; |
| 399 | this.container.appendChild(label); |
| 400 | this.xlabels.push(label); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | context.beginPath(); |
| 405 | context.moveTo(halfUp(this.area.x), halfDown(this.area.y + this.area.h)); |
| 406 | context.lineTo(halfUp(this.area.x + this.area.w), halfDown(this.area.y + this.area.h)); |
| 407 | context.closePath(); |
| 408 | context.stroke(); |
| 409 | } |
| 410 | |
| 411 | context.restore(); |
| 412 | }; |
| 413 | |
| 414 | |
| 415 | DygraphCanvasRenderer.prototype._renderChartLabels = function() { |
| 416 | // Generate divs for the chart title, xlabel and ylabel. |
| 417 | // Space for these divs has already been taken away from the charting area in |
| 418 | // the DygraphCanvasRenderer constructor. |
| 419 | if (this.attr_('title')) { |
| 420 | var div = document.createElement("div"); |
| 421 | div.style.position = 'absolute'; |
| 422 | div.style.top = '0px'; |
| 423 | div.style.left = this.area.x + 'px'; |
| 424 | div.style.width = this.area.w + 'px'; |
| 425 | div.style.height = this.attr_('titleHeight') + 'px'; |
| 426 | div.style.textAlign = 'center'; |
| 427 | div.style.fontSize = (this.attr_('titleHeight') - 8) + 'px'; |
| 428 | div.style.fontWeight = 'bold'; |
| 429 | var class_div = document.createElement("div"); |
| 430 | class_div.className = 'dygraph-label dygraph-title'; |
| 431 | class_div.innerHTML = this.attr_('title'); |
| 432 | div.appendChild(class_div); |
| 433 | this.container.appendChild(div); |
| 434 | this.chartLabels.title = div; |
| 435 | } |
| 436 | |
| 437 | if (this.attr_('xlabel')) { |
| 438 | var div = document.createElement("div"); |
| 439 | div.style.position = 'absolute'; |
| 440 | div.style.bottom = 0; // TODO(danvk): this is lazy. Calculate style.top. |
| 441 | div.style.left = this.area.x + 'px'; |
| 442 | div.style.width = this.area.w + 'px'; |
| 443 | div.style.height = this.attr_('xLabelHeight') + 'px'; |
| 444 | div.style.textAlign = 'center'; |
| 445 | div.style.fontSize = (this.attr_('xLabelHeight') - 2) + 'px'; |
| 446 | |
| 447 | var class_div = document.createElement("div"); |
| 448 | class_div.className = 'dygraph-label dygraph-xlabel'; |
| 449 | class_div.innerHTML = this.attr_('xlabel'); |
| 450 | div.appendChild(class_div); |
| 451 | this.container.appendChild(div); |
| 452 | this.chartLabels.xlabel = div; |
| 453 | } |
| 454 | |
| 455 | if (this.attr_('ylabel')) { |
| 456 | var box = { |
| 457 | left: 0, |
| 458 | top: this.area.y, |
| 459 | width: this.attr_('yLabelWidth'), |
| 460 | height: this.area.h |
| 461 | }; |
| 462 | // TODO(danvk): is this outer div actually necessary? |
| 463 | var div = document.createElement("div"); |
| 464 | div.style.position = 'absolute'; |
| 465 | div.style.left = box.left; |
| 466 | div.style.top = box.top + 'px'; |
| 467 | div.style.width = box.width + 'px'; |
| 468 | div.style.height = box.height + 'px'; |
| 469 | div.style.fontSize = (this.attr_('yLabelWidth') - 2) + 'px'; |
| 470 | |
| 471 | var inner_div = document.createElement("div"); |
| 472 | inner_div.style.position = 'absolute'; |
| 473 | inner_div.style.width = box.height + 'px'; |
| 474 | inner_div.style.height = box.width + 'px'; |
| 475 | inner_div.style.top = (box.height / 2 - box.width / 2) + 'px'; |
| 476 | inner_div.style.left = (box.width / 2 - box.height / 2) + 'px'; |
| 477 | inner_div.style.textAlign = 'center'; |
| 478 | |
| 479 | // CSS rotation is an HTML5 feature which is not standardized. Hence every |
| 480 | // browser has its own name for the CSS style. |
| 481 | inner_div.style.transform = 'rotate(-90deg)'; // HTML5 |
| 482 | inner_div.style.WebkitTransform = 'rotate(-90deg)'; // Safari/Chrome |
| 483 | inner_div.style.MozTransform = 'rotate(-90deg)'; // Firefox |
| 484 | inner_div.style.OTransform = 'rotate(-90deg)'; // Opera |
| 485 | inner_div.style.msTransform = 'rotate(-90deg)'; // IE9 |
| 486 | |
| 487 | if (typeof(document.documentMode) !== 'undefined' && |
| 488 | document.documentMode < 9) { |
| 489 | // We're dealing w/ an old version of IE, so we have to rotate the text |
| 490 | // using a BasicImage transform. This uses a different origin of rotation |
| 491 | // than HTML5 rotation (top left of div vs. its center). |
| 492 | inner_div.style.filter = |
| 493 | 'progid:DXImageTransform.Microsoft.BasicImage(rotation=3)'; |
| 494 | inner_div.style.left = '0px'; |
| 495 | inner_div.style.top = '0px'; |
| 496 | } |
| 497 | |
| 498 | var class_div = document.createElement("div"); |
| 499 | class_div.className = 'dygraph-label dygraph-ylabel'; |
| 500 | class_div.innerHTML = this.attr_('ylabel'); |
| 501 | |
| 502 | inner_div.appendChild(class_div); |
| 503 | div.appendChild(inner_div); |
| 504 | this.container.appendChild(div); |
| 505 | this.chartLabels.ylabel = div; |
| 506 | } |
| 507 | }; |
| 508 | |
| 509 | |
| 510 | DygraphCanvasRenderer.prototype._renderAnnotations = function() { |
| 511 | var annotationStyle = { |
| 512 | "position": "absolute", |
| 513 | "fontSize": this.attr_('axisLabelFontSize') + "px", |
| 514 | "zIndex": 10, |
| 515 | "overflow": "hidden" |
| 516 | }; |
| 517 | |
| 518 | var bindEvt = function(eventName, classEventName, p, self) { |
| 519 | return function(e) { |
| 520 | var a = p.annotation; |
| 521 | if (a.hasOwnProperty(eventName)) { |
| 522 | a[eventName](a, p, self.dygraph_, e); |
| 523 | } else if (self.dygraph_.attr_(classEventName)) { |
| 524 | self.dygraph_.attr_(classEventName)(a, p, self.dygraph_,e ); |
| 525 | } |
| 526 | }; |
| 527 | } |
| 528 | |
| 529 | // Get a list of point with annotations. |
| 530 | var points = this.layout.annotated_points; |
| 531 | for (var i = 0; i < points.length; i++) { |
| 532 | var p = points[i]; |
| 533 | if (p.canvasx < this.area.x || p.canvasx > this.area.x + this.area.w) { |
| 534 | continue; |
| 535 | } |
| 536 | |
| 537 | var a = p.annotation; |
| 538 | var tick_height = 6; |
| 539 | if (a.hasOwnProperty("tickHeight")) { |
| 540 | tick_height = a.tickHeight; |
| 541 | } |
| 542 | |
| 543 | var div = document.createElement("div"); |
| 544 | for (var name in annotationStyle) { |
| 545 | if (annotationStyle.hasOwnProperty(name)) { |
| 546 | div.style[name] = annotationStyle[name]; |
| 547 | } |
| 548 | } |
| 549 | if (!a.hasOwnProperty('icon')) { |
| 550 | div.className = "dygraphDefaultAnnotation"; |
| 551 | } |
| 552 | if (a.hasOwnProperty('cssClass')) { |
| 553 | div.className += " " + a.cssClass; |
| 554 | } |
| 555 | |
| 556 | var width = a.hasOwnProperty('width') ? a.width : 16; |
| 557 | var height = a.hasOwnProperty('height') ? a.height : 16; |
| 558 | if (a.hasOwnProperty('icon')) { |
| 559 | var img = document.createElement("img"); |
| 560 | img.src = a.icon; |
| 561 | img.width = width; |
| 562 | img.height = height; |
| 563 | div.appendChild(img); |
| 564 | } else if (p.annotation.hasOwnProperty('shortText')) { |
| 565 | div.appendChild(document.createTextNode(p.annotation.shortText)); |
| 566 | } |
| 567 | div.style.left = (p.canvasx - width / 2) + "px"; |
| 568 | if (a.attachAtBottom) { |
| 569 | div.style.top = (this.area.h - height - tick_height) + "px"; |
| 570 | } else { |
| 571 | div.style.top = (p.canvasy - height - tick_height) + "px"; |
| 572 | } |
| 573 | div.style.width = width + "px"; |
| 574 | div.style.height = height + "px"; |
| 575 | div.title = p.annotation.text; |
| 576 | div.style.color = this.colors[p.name]; |
| 577 | div.style.borderColor = this.colors[p.name]; |
| 578 | a.div = div; |
| 579 | |
| 580 | Dygraph.addEvent(div, 'click', |
| 581 | bindEvt('clickHandler', 'annotationClickHandler', p, this)); |
| 582 | Dygraph.addEvent(div, 'mouseover', |
| 583 | bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p, this)); |
| 584 | Dygraph.addEvent(div, 'mouseout', |
| 585 | bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p, this)); |
| 586 | Dygraph.addEvent(div, 'dblclick', |
| 587 | bindEvt('dblClickHandler', 'annotationDblClickHandler', p, this)); |
| 588 | |
| 589 | this.container.appendChild(div); |
| 590 | this.annotations.push(div); |
| 591 | |
| 592 | var ctx = this.elementContext; |
| 593 | ctx.strokeStyle = this.colors[p.name]; |
| 594 | ctx.beginPath(); |
| 595 | if (!a.attachAtBottom) { |
| 596 | ctx.moveTo(p.canvasx, p.canvasy); |
| 597 | ctx.lineTo(p.canvasx, p.canvasy - 2 - tick_height); |
| 598 | } else { |
| 599 | ctx.moveTo(p.canvasx, this.area.h); |
| 600 | ctx.lineTo(p.canvasx, this.area.h - 2 - tick_height); |
| 601 | } |
| 602 | ctx.closePath(); |
| 603 | ctx.stroke(); |
| 604 | } |
| 605 | }; |
| 606 | |
| 607 | |
| 608 | /** |
| 609 | * Overrides the CanvasRenderer method to draw error bars |
| 610 | */ |
| 611 | DygraphCanvasRenderer.prototype._renderLineChart = function() { |
| 612 | var isNullOrNaN = function(x) { |
| 613 | return (x === null || isNaN(x)); |
| 614 | }; |
| 615 | |
| 616 | // TODO(danvk): use this.attr_ for many of these. |
| 617 | var context = this.elementContext; |
| 618 | var fillAlpha = this.attr_('fillAlpha'); |
| 619 | var errorBars = this.attr_("errorBars") || this.attr_("customBars"); |
| 620 | var fillGraph = this.attr_("fillGraph"); |
| 621 | var stackedGraph = this.attr_("stackedGraph"); |
| 622 | var stepPlot = this.attr_("stepPlot"); |
| 623 | var points = this.layout.points; |
| 624 | var pointsLength = points.length; |
| 625 | |
| 626 | var setNames = []; |
| 627 | for (var name in this.layout.datasets) { |
| 628 | if (this.layout.datasets.hasOwnProperty(name)) { |
| 629 | setNames.push(name); |
| 630 | } |
| 631 | } |
| 632 | var setCount = setNames.length; |
| 633 | |
| 634 | // TODO(danvk): Move this mapping into Dygraph and get it out of here. |
| 635 | this.colors = {} |
| 636 | for (var i = 0; i < setCount; i++) { |
| 637 | this.colors[setNames[i]] = this.colorScheme_[i % this.colorScheme_.length]; |
| 638 | } |
| 639 | |
| 640 | // Update Points |
| 641 | // TODO(danvk): here |
| 642 | for (var i = pointsLength; i--;) { |
| 643 | var point = points[i]; |
| 644 | point.canvasx = this.area.w * point.x + this.area.x; |
| 645 | point.canvasy = this.area.h * point.y + this.area.y; |
| 646 | } |
| 647 | |
| 648 | // create paths |
| 649 | var ctx = context; |
| 650 | if (errorBars) { |
| 651 | if (fillGraph) { |
| 652 | this.dygraph_.warn("Can't use fillGraph option with error bars"); |
| 653 | } |
| 654 | |
| 655 | for (var i = 0; i < setCount; i++) { |
| 656 | var setName = setNames[i]; |
| 657 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
| 658 | var color = this.colors[setName]; |
| 659 | |
| 660 | // setup graphics context |
| 661 | ctx.save(); |
| 662 | var prevX = NaN; |
| 663 | var prevY = NaN; |
| 664 | var prevYs = [-1, -1]; |
| 665 | var yscale = axis.yscale; |
| 666 | // should be same color as the lines but only 15% opaque. |
| 667 | var rgb = new RGBColor(color); |
| 668 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
| 669 | fillAlpha + ')'; |
| 670 | ctx.fillStyle = err_color; |
| 671 | ctx.beginPath(); |
| 672 | for (var j = 0; j < pointsLength; j++) { |
| 673 | var point = points[j]; |
| 674 | if (point.name == setName) { |
| 675 | if (!Dygraph.isOK(point.y)) { |
| 676 | prevX = NaN; |
| 677 | continue; |
| 678 | } |
| 679 | |
| 680 | // TODO(danvk): here |
| 681 | if (stepPlot) { |
| 682 | var newYs = [ point.y_bottom, point.y_top ]; |
| 683 | prevY = point.y; |
| 684 | } else { |
| 685 | var newYs = [ point.y_bottom, point.y_top ]; |
| 686 | } |
| 687 | newYs[0] = this.area.h * newYs[0] + this.area.y; |
| 688 | newYs[1] = this.area.h * newYs[1] + this.area.y; |
| 689 | if (!isNaN(prevX)) { |
| 690 | if (stepPlot) { |
| 691 | ctx.moveTo(prevX, newYs[0]); |
| 692 | } else { |
| 693 | ctx.moveTo(prevX, prevYs[0]); |
| 694 | } |
| 695 | ctx.lineTo(point.canvasx, newYs[0]); |
| 696 | ctx.lineTo(point.canvasx, newYs[1]); |
| 697 | if (stepPlot) { |
| 698 | ctx.lineTo(prevX, newYs[1]); |
| 699 | } else { |
| 700 | ctx.lineTo(prevX, prevYs[1]); |
| 701 | } |
| 702 | ctx.closePath(); |
| 703 | } |
| 704 | prevYs = newYs; |
| 705 | prevX = point.canvasx; |
| 706 | } |
| 707 | } |
| 708 | ctx.fill(); |
| 709 | } |
| 710 | } else if (fillGraph) { |
| 711 | var baseline = [] // for stacked graphs: baseline for filling |
| 712 | |
| 713 | // process sets in reverse order (needed for stacked graphs) |
| 714 | for (var i = setCount - 1; i >= 0; i--) { |
| 715 | var setName = setNames[i]; |
| 716 | var color = this.colors[setName]; |
| 717 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
| 718 | var axisY = 1.0 + axis.minyval * axis.yscale; |
| 719 | if (axisY < 0.0) axisY = 0.0; |
| 720 | else if (axisY > 1.0) axisY = 1.0; |
| 721 | axisY = this.area.h * axisY + this.area.y; |
| 722 | |
| 723 | // setup graphics context |
| 724 | ctx.save(); |
| 725 | var prevX = NaN; |
| 726 | var prevYs = [-1, -1]; |
| 727 | var yscale = axis.yscale; |
| 728 | // should be same color as the lines but only 15% opaque. |
| 729 | var rgb = new RGBColor(color); |
| 730 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
| 731 | fillAlpha + ')'; |
| 732 | ctx.fillStyle = err_color; |
| 733 | ctx.beginPath(); |
| 734 | for (var j = 0; j < pointsLength; j++) { |
| 735 | var point = points[j]; |
| 736 | if (point.name == setName) { |
| 737 | if (!Dygraph.isOK(point.y)) { |
| 738 | prevX = NaN; |
| 739 | continue; |
| 740 | } |
| 741 | var newYs; |
| 742 | if (stackedGraph) { |
| 743 | lastY = baseline[point.canvasx]; |
| 744 | if (lastY === undefined) lastY = axisY; |
| 745 | baseline[point.canvasx] = point.canvasy; |
| 746 | newYs = [ point.canvasy, lastY ]; |
| 747 | } else { |
| 748 | newYs = [ point.canvasy, axisY ]; |
| 749 | } |
| 750 | if (!isNaN(prevX)) { |
| 751 | ctx.moveTo(prevX, prevYs[0]); |
| 752 | if (stepPlot) { |
| 753 | ctx.lineTo(point.canvasx, prevYs[0]); |
| 754 | } else { |
| 755 | ctx.lineTo(point.canvasx, newYs[0]); |
| 756 | } |
| 757 | ctx.lineTo(point.canvasx, newYs[1]); |
| 758 | ctx.lineTo(prevX, prevYs[1]); |
| 759 | ctx.closePath(); |
| 760 | } |
| 761 | prevYs = newYs; |
| 762 | prevX = point.canvasx; |
| 763 | } |
| 764 | } |
| 765 | ctx.fill(); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | // Drawing the lines. |
| 770 | var firstIndexInSet = 0; |
| 771 | var afterLastIndexInSet = 0; |
| 772 | var setLength = 0; |
| 773 | for (var i = 0; i < setCount; i += 1) { |
| 774 | setLength = this.layout.setPointsLengths[i]; |
| 775 | afterLastIndexInSet += setLength; |
| 776 | var setName = setNames[i]; |
| 777 | var color = this.colors[setName]; |
| 778 | var strokeWidth = this.dygraph_.attr_("strokeWidth", setName); |
| 779 | |
| 780 | // setup graphics context |
| 781 | context.save(); |
| 782 | var pointSize = this.dygraph_.attr_("pointSize", setName); |
| 783 | var prevX = null, prevY = null; |
| 784 | var drawPoints = this.dygraph_.attr_("drawPoints", setName); |
| 785 | for (var j = firstIndexInSet; j < afterLastIndexInSet; j++) { |
| 786 | var point = points[j]; |
| 787 | if (isNullOrNaN(point.canvasy)) { |
| 788 | if (stepPlot && prevX != null) { |
| 789 | // Draw a horizontal line to the start of the missing data |
| 790 | ctx.beginPath(); |
| 791 | ctx.strokeStyle = color; |
| 792 | ctx.lineWidth = this.attr_('strokeWidth'); |
| 793 | ctx.moveTo(prevX, prevY); |
| 794 | ctx.lineTo(point.canvasx, prevY); |
| 795 | ctx.stroke(); |
| 796 | } |
| 797 | // this will make us move to the next point, not draw a line to it. |
| 798 | prevX = prevY = null; |
| 799 | } else { |
| 800 | // A point is "isolated" if it is non-null but both the previous |
| 801 | // and next points are null. |
| 802 | var isIsolated = (!prevX && (j == points.length - 1 || |
| 803 | isNullOrNaN(points[j+1].canvasy))); |
| 804 | if (prevX === null) { |
| 805 | prevX = point.canvasx; |
| 806 | prevY = point.canvasy; |
| 807 | } else { |
| 808 | // Skip over points that will be drawn in the same pixel. |
| 809 | if (Math.round(prevX) == Math.round(point.canvasx) && |
| 810 | Math.round(prevY) == Math.round(point.canvasy)) { |
| 811 | continue; |
| 812 | } |
| 813 | // TODO(antrob): skip over points that lie on a line that is already |
| 814 | // going to be drawn. There is no need to have more than 2 |
| 815 | // consecutive points that are collinear. |
| 816 | if (strokeWidth) { |
| 817 | ctx.beginPath(); |
| 818 | ctx.strokeStyle = color; |
| 819 | ctx.lineWidth = strokeWidth; |
| 820 | ctx.moveTo(prevX, prevY); |
| 821 | if (stepPlot) { |
| 822 | ctx.lineTo(point.canvasx, prevY); |
| 823 | } |
| 824 | prevX = point.canvasx; |
| 825 | prevY = point.canvasy; |
| 826 | ctx.lineTo(prevX, prevY); |
| 827 | ctx.stroke(); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | if (drawPoints || isIsolated) { |
| 832 | ctx.beginPath(); |
| 833 | ctx.fillStyle = color; |
| 834 | ctx.arc(point.canvasx, point.canvasy, pointSize, |
| 835 | 0, 2 * Math.PI, false); |
| 836 | ctx.fill(); |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | firstIndexInSet = afterLastIndexInSet; |
| 841 | } |
| 842 | |
| 843 | context.restore(); |
| 844 | }; |