| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 3 Use of this source code is governed by a BSD-style license that can be | |
| 4 found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 // Collection of classes used to plot data in a <canvas>. Create a Plotter() | |
| 8 // to generate a plot. | |
| 9 | |
| 10 // Vertical marker for columns. | |
| 11 function Marker(color) { | |
| 12 var m = document.createElement("DIV"); | |
| 13 m.setAttribute("class", "plot-cursor"); | |
| 14 m.style.backgroundColor = color; | |
| 15 m.style.opacity = "0.3"; | |
| 16 m.style.position = "absolute"; | |
| 17 m.style.left = "-2px"; | |
| 18 m.style.top = "-2px"; | |
| 19 m.style.width = "0px"; | |
| 20 m.style.height = "0px"; | |
| 21 return m; | |
| 22 } | |
| 23 | |
| 24 /** | |
| 25 * Adds commas to |number|. | |
| 26 * | |
| 27 * Examples: | |
| 28 * 1234.56 => "1,234.56" | |
| 29 * "99999" => "99,999" | |
| 30 * | |
| 31 * @param number {string|number} The number to format. | |
| 32 * @return {string} String representation of |number| with commas every | |
| 33 * three places. | |
| 34 */ | |
| 35 function addCommas(number) { | |
| 36 number += ''; // Convert number to string if not already. | |
| 37 var numberParts = number.split('.'); | |
| 38 var integralPart = numberParts[0]; | |
| 39 var fractionalPart = numberParts.length > 1 ? '.' + numberParts[1] : ''; | |
| 40 var reThreeDigits = /(\d+)(\d{3})/; | |
| 41 while (reThreeDigits.test(integralPart)) { | |
| 42 integralPart = integralPart.replace(reThreeDigits, '$1' + ',' + '$2'); | |
| 43 } | |
| 44 return integralPart + fractionalPart; | |
| 45 } | |
| 46 | |
| 47 /** | |
| 48 * HorizontalMarker class | |
| 49 * Create a horizontal marker at the indicated mouse location. | |
| 50 * @constructor | |
| 51 * | |
| 52 * @param canvasRect {Object} The canvas bounds (in client coords). | |
| 53 * @param clientY {Number} The vertical mouse click location that spawned | |
| 54 * the marker, in the client coordinate space. | |
| 55 * @param yValue {Number} The plotted value corresponding to the clientY | |
| 56 * click location. | |
| 57 */ | |
| 58 function HorizontalMarker(canvasRect, clientY, yValue) { | |
| 59 // Add a horizontal line to the graph. | |
| 60 var m = document.createElement("DIV"); | |
| 61 m.setAttribute("class", "plot-baseline"); | |
| 62 m.style.backgroundColor = HorizontalMarker.COLOR; | |
| 63 m.style.opacity = "0.3"; | |
| 64 m.style.position = "absolute"; | |
| 65 m.style.left = canvasRect.offsetLeft; | |
| 66 var h = HorizontalMarker.HEIGHT; | |
| 67 m.style.top = (clientY - h/2).toFixed(0) + "px"; | |
| 68 m.style.width = canvasRect.offsetWidth + "px"; | |
| 69 m.style.height = h + "px"; | |
| 70 this.markerDiv_ = m; | |
| 71 | |
| 72 this.value = yValue; | |
| 73 } | |
| 74 | |
| 75 HorizontalMarker.HEIGHT = 5; | |
| 76 HorizontalMarker.COLOR = "rgb(0,100,100)"; | |
| 77 | |
| 78 // Remove the horizontal line from the graph. | |
| 79 HorizontalMarker.prototype.remove_ = function() { | |
| 80 this.markerDiv_.parentNode.removeChild(this.markerDiv_); | |
| 81 }; | |
| 82 | |
| 83 | |
| 84 /** | |
| 85 * Plotter class | |
| 86 * @constructor | |
| 87 * | |
| 88 * Draws a chart using CANVAS element. Takes array of lines to draw with | |
| 89 * deviations values for each data sample. | |
| 90 * | |
| 91 * @param {Array} clNumbers list of clNumbers for each data sample. | |
| 92 * @param {Array} plotData list of arrays that represent individual lines. | |
| 93 * The line itself is an Array of value and stdd. | |
| 94 * @param {Array} dataDescription list of data description for each line | |
| 95 * in plotData. | |
| 96 * @param {string} units name of measurement used to describe plotted data. | |
| 97 * | |
| 98 * Example of the plotData: | |
| 99 * [ | |
| 100 * [line 1 data], | |
| 101 * [line 2 data] | |
| 102 * ]. | |
| 103 * Line data looks like [[point one], [point two]]. | |
| 104 * And individual points are [value, deviation value] | |
| 105 */ | |
| 106 function Plotter(clNumbers, plotData, dataDescription, units, resultNode, | |
| 107 width, height) { | |
| 108 this.clNumbers_ = clNumbers; | |
| 109 this.plotData_ = plotData; | |
| 110 this.dataDescription_ = dataDescription; | |
| 111 this.dataColors_ = []; | |
| 112 this.dataIndexByName_ = {}; | |
| 113 this.resultNode_ = resultNode; | |
| 114 this.units_ = units; | |
| 115 this.selectedTraces_ = []; | |
| 116 this.imageCache_ = null; | |
| 117 this.enableMouseScroll = true; | |
| 118 this.coordinates = new Coordinates(plotData, width, height); | |
| 119 if (isNaN(width)) | |
| 120 this.width = this.coordinates.widthMax; | |
| 121 else | |
| 122 this.width = width; | |
| 123 | |
| 124 this.plotPane_ = null; | |
| 125 | |
| 126 // A color palette that's unambigous for normal and color-deficient viewers. | |
| 127 // Values are (red, green, blue) on a scale of 255. | |
| 128 // Taken from http://jfly.iam.u-tokyo.ac.jp/html/manuals/pdf/color_blind.pdf | |
| 129 this.colors = [[0, 114, 178], // blue | |
| 130 [230, 159, 0], // orange | |
| 131 [0, 158, 115], // green | |
| 132 [204, 121, 167], // purplish pink | |
| 133 [86, 180, 233], // sky blue | |
| 134 [213, 94, 0], // dark orange | |
| 135 [0, 0, 0], // black | |
| 136 [240, 228, 66] // yellow | |
| 137 ]; | |
| 138 | |
| 139 var categoryColors = {}; | |
| 140 var colorIndex = 0; | |
| 141 for (var i = 0; i < this.dataDescription_.length; i++) { | |
| 142 this.dataIndexByName_[this.dataDescription_[i]] = i; | |
| 143 var category = this.dataDescription_[i].replace(/-.*/, ""); | |
| 144 if (this.dataDescription_[i].indexOf("ref") == -1) { | |
| 145 category += "-ref"; | |
| 146 } | |
| 147 if (!categoryColors[category]) { | |
| 148 categoryColors[category] = this.makeColor(colorIndex++); | |
| 149 } | |
| 150 this.dataColors_[i] = categoryColors[category]; | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 /** | |
| 155 * Does the actual plotting. | |
| 156 */ | |
| 157 Plotter.prototype.plot = function() { | |
| 158 this.canvas_elt_ = this.canvas(); | |
| 159 this.coordinates_div_ = this.coordinates_(); | |
| 160 this.ruler_div_ = this.ruler(); | |
| 161 // marker for the result-point that the mouse is currently over | |
| 162 this.cursor_div_ = new Marker("rgb(100,80,240)"); | |
| 163 // marker for the result-point for which details are shown | |
| 164 this.marker_div_ = new Marker("rgb(100,100,100)"); | |
| 165 | |
| 166 this.plotPane_ = document.createElement('div'); | |
| 167 this.plotPane_.setAttribute('class', 'plot'); | |
| 168 this.plotPane_.setAttribute('style', 'position: relative'); | |
| 169 this.resultNode_.appendChild(this.plotPane_); | |
| 170 this.plotPane_.appendChild(this.canvas_elt_); | |
| 171 this.plotPane_.appendChild(this.ruler_div_); | |
| 172 this.plotPane_.appendChild(this.cursor_div_); | |
| 173 this.plotPane_.appendChild(this.marker_div_); | |
| 174 | |
| 175 this.resultNode_.appendChild(this.coordinates_div_); | |
| 176 this.attachEventListeners(this.canvas_elt_); | |
| 177 | |
| 178 this.redraw(); | |
| 179 | |
| 180 this.graduation_divs_ = this.graduations(); | |
| 181 for (var i = 0; i < this.graduation_divs_.length; i++) | |
| 182 this.plotPane_.appendChild(this.graduation_divs_[i]); | |
| 183 }; | |
| 184 | |
| 185 /** | |
| 186 * Redraws the canvas with selected traces highlighted. | |
| 187 */ | |
| 188 Plotter.prototype.redraw = function() { | |
| 189 var ctx = this.canvas_elt_.getContext("2d"); | |
| 190 var doDrawImage = this.selectedTraces_.length || this.imageCache_; | |
| 191 // Drawing all lines can take a few seconds on large graphs, so use a cache. | |
| 192 // After the initial render, the cache draws quickly on Firefox and Chrome. | |
| 193 if (!this.imageCache_) { | |
| 194 // Clear it with white: otherwise canvas will draw on top of existing data. | |
| 195 ctx.clearRect(0, 0, this.canvas_elt_.width, this.canvas_elt_.height); | |
| 196 // Draw all data lines. | |
| 197 for (var i = 0; i < this.plotData_.length; i++) | |
| 198 this.plotLine_(ctx, this.getDataColor(i), this.plotData_[i]); | |
| 199 // Here we convert the canvas to an image by making a new Image with | |
| 200 // src set to the canvas's Data URL. | |
| 201 var imageDataURL = this.canvas_elt_.toDataURL(); | |
| 202 this.imageCache_ = new Image; | |
| 203 this.imageCache_.src = imageDataURL; | |
| 204 } | |
| 205 if (doDrawImage) { | |
| 206 // Clear it again so we don't draw on top of the old line. | |
| 207 ctx.clearRect(0, 0, this.canvas_elt_.width, this.canvas_elt_.height); | |
| 208 // If we have selections, dim the other traces by first setting low alpha. | |
| 209 if (this.selectedTraces_.length) | |
| 210 ctx.globalAlpha = 0.2; | |
| 211 // Draw the cached image. | |
| 212 ctx.drawImage(this.imageCache_, 0, 0); | |
| 213 // Restore the alpha so we can draw selected lines in full opacity. | |
| 214 ctx.globalAlpha = 1; | |
| 215 } | |
| 216 // Now draw all selected traces in order of selection. | |
| 217 for (var i = 0; i < this.selectedTraces_.length; i++) { | |
| 218 var index = this.selectedTraces_[i]; | |
| 219 this.plotLine_(ctx, this.getDataColor(index), this.plotData_[index]); | |
| 220 } | |
| 221 }; | |
| 222 | |
| 223 /** | |
| 224 * Sets the selected state of a given trace. | |
| 225 * @param {number} trace_index | |
| 226 * @return {boolean} true if the trace has been selected, false if deselected. | |
| 227 */ | |
| 228 Plotter.prototype.toggleSelection = function(trace_index) { | |
| 229 var i = this.selectedTraces_.indexOf(trace_index); | |
| 230 var ret = (i == -1); | |
| 231 if (ret) | |
| 232 this.selectedTraces_.push(trace_index); | |
| 233 else | |
| 234 this.selectedTraces_.splice(i, 1); | |
| 235 this.redraw(); | |
| 236 return ret; | |
| 237 }; | |
| 238 | |
| 239 Plotter.prototype.drawDeviationBar_ = function(context, strokeStyles, x, | |
| 240 y_errLow, y_errHigh) { | |
| 241 context.strokeStyle = strokeStyles; | |
| 242 context.lineWidth = 1.0; | |
| 243 context.beginPath(); | |
| 244 context.moveTo(x, y_errHigh); | |
| 245 context.lineTo(x, y_errLow); | |
| 246 context.closePath(); | |
| 247 context.stroke(); | |
| 248 }; | |
| 249 | |
| 250 Plotter.prototype.plotLine_ = function(ctx, strokeStyles, data) { | |
| 251 ctx.strokeStyle = strokeStyles; | |
| 252 ctx.lineWidth = 2.0; | |
| 253 ctx.beginPath(); | |
| 254 var initial = true; | |
| 255 var deviationData = []; | |
| 256 for (var i = 0; i < data.length; i++) { | |
| 257 var x = this.coordinates.xPoints(i); | |
| 258 var value = parseFloat(data[i][0]); | |
| 259 var stdd = parseFloat(data[i][1]); | |
| 260 var y = 0.0; | |
| 261 var y_errLow = 0.0; | |
| 262 var y_errHigh = 0.0; | |
| 263 if (isNaN(value)) { | |
| 264 // Re-set 'initial' if we're at a gap in the data. | |
| 265 initial = true; | |
| 266 } else { | |
| 267 y = this.coordinates.yPoints(value); | |
| 268 // We assume that the stdd will only be NaN (missing) when the value is. | |
| 269 if (value != 0.0) { | |
| 270 y_errLow = this.coordinates.yPoints(value - stdd); | |
| 271 y_errHigh = this.coordinates.yPoints(value + stdd); | |
| 272 } | |
| 273 if (initial) | |
| 274 initial = false; | |
| 275 else | |
| 276 ctx.lineTo(x, y); | |
| 277 } | |
| 278 | |
| 279 ctx.moveTo(x, y); | |
| 280 deviationData.push([x, y_errLow, y_errHigh]); | |
| 281 } | |
| 282 ctx.closePath(); | |
| 283 ctx.stroke(); | |
| 284 | |
| 285 for (var i = 0; i < deviationData.length; i++) { | |
| 286 this.drawDeviationBar_(ctx, strokeStyles, deviationData[i][0], | |
| 287 deviationData[i][1], deviationData[i][2]); | |
| 288 } | |
| 289 }; | |
| 290 | |
| 291 Plotter.prototype.attachEventListeners = function(canvas) { | |
| 292 var self = this; | |
| 293 if (this.enableMouseScroll) { | |
| 294 canvas.parentNode.addEventListener( | |
| 295 "mousewheel", function(evt) { self.onMouseScroll_(evt); }, false); | |
| 296 canvas.parentNode.addEventListener( | |
| 297 "DOMMouseScroll", function(evt) { self.onMouseScroll_(evt); }, false); | |
| 298 } | |
| 299 canvas.parentNode.addEventListener( | |
| 300 "mousemove", function(evt) { self.onMouseMove_(evt); }, false); | |
| 301 this.cursor_div_.addEventListener( | |
| 302 "click", function(evt) { self.onMouseClick_(evt); }, false); | |
| 303 }; | |
| 304 | |
| 305 Plotter.prototype.onMouseScroll_ = function(evt) { | |
| 306 // Chrome uses wheelDelta and Mozilla uses detail with opposite sign values. | |
| 307 var zoom = evt.wheelDelta ? evt.wheelDelta : -evt.detail; | |
| 308 zoom = zoom < 0 ? -1 : 1; | |
| 309 // Zoom less if the shift key is held. | |
| 310 if (evt.shiftKey) | |
| 311 zoom /= 10; | |
| 312 | |
| 313 var obj = this.canvas_elt_; | |
| 314 var offsetTop = 0; | |
| 315 do { | |
| 316 offsetTop += obj.offsetTop; | |
| 317 } while (obj = obj.offsetParent); | |
| 318 var positionY = evt.clientY + document.body.scrollTop - offsetTop; | |
| 319 var yValue = this.coordinates.yValue(positionY); | |
| 320 var yTopToMouse = this.coordinates.yMaxValue - (this.coordinates.yMaxValue + | |
| 321 yValue) / 2; | |
| 322 var yBottomToMouse = (yValue + this.coordinates.yMinValue) / 2 - | |
| 323 this.coordinates.yMinValue; | |
| 324 | |
| 325 this.coordinates.yMinValue += yBottomToMouse * zoom; | |
| 326 this.coordinates.yMaxValue -= yTopToMouse * zoom; | |
| 327 this.imageCache_ = null; | |
| 328 if(this.horizontal_marker_) { | |
| 329 this.horizontal_marker_.remove_(); | |
| 330 this.horizontal_marker_ = null; | |
| 331 } | |
| 332 for (var i = 0; i < this.graduation_divs_.length; i++) | |
| 333 this.plotPane_.removeChild(this.graduation_divs_[i]); | |
| 334 this.graduation_divs_ = this.graduations(); | |
| 335 for (var i = 0; i < this.graduation_divs_.length; i++) | |
| 336 this.plotPane_.appendChild(this.graduation_divs_[i]); | |
| 337 this.redraw(); | |
| 338 }; | |
| 339 | |
| 340 Plotter.prototype.updateRuler_ = function(evt) { | |
| 341 var r = this.ruler_div_; | |
| 342 var obj = this.canvas_elt_; | |
| 343 var offsetTop = 0; | |
| 344 do { | |
| 345 offsetTop += obj.offsetTop; | |
| 346 } while (obj = obj.offsetParent); | |
| 347 r.style.left = this.canvas_elt_.offsetLeft + "px"; | |
| 348 r.style.top = this.canvas_elt_.offsetTop + "px"; | |
| 349 r.style.width = this.canvas_elt_.offsetWidth + "px"; | |
| 350 var h = evt.clientY + document.body.scrollTop - offsetTop; | |
| 351 if (h > this.canvas_elt_.offsetHeight) | |
| 352 h = this.canvas_elt_.offsetHeight; | |
| 353 r.style.height = h + "px"; | |
| 354 }; | |
| 355 | |
| 356 Plotter.prototype.updateCursor_ = function() { | |
| 357 var c = this.cursor_div_; | |
| 358 c.style.top = this.canvas_elt_.offsetTop + "px"; | |
| 359 c.style.height = this.canvas_elt_.offsetHeight + "px"; | |
| 360 var w = this.canvas_elt_.offsetWidth / this.clNumbers_.length; | |
| 361 var x = (this.canvas_elt_.offsetLeft + w * this.current_index_).toFixed(0); | |
| 362 c.style.left = x + "px"; | |
| 363 c.style.width = w + "px"; | |
| 364 }; | |
| 365 | |
| 366 Plotter.prototype.chromiumCLNumber_ = function(index) { | |
| 367 // CL number entries are either revisions or objects of the form | |
| 368 // {chromium: revision, webkit: revision} | |
| 369 return this.clNumbers_[index].chromium || this.clNumbers_[index]; | |
| 370 }; | |
| 371 | |
| 372 Plotter.prototype.onMouseMove_ = function(evt) { | |
| 373 var obj = this.canvas_elt_; | |
| 374 var offsetTop = 0; | |
| 375 var offsetLeft = 0; | |
| 376 do { | |
| 377 offsetTop += obj.offsetTop; | |
| 378 offsetLeft += obj.offsetLeft; | |
| 379 } while (obj = obj.offsetParent); | |
| 380 | |
| 381 var canvas = evt.currentTarget.firstChild; | |
| 382 var positionX = evt.clientX + document.body.scrollLeft - offsetLeft; | |
| 383 var positionY = evt.clientY + document.body.scrollTop - offsetTop; | |
| 384 | |
| 385 this.current_index_ = this.coordinates.dataSampleIndex(positionX); | |
| 386 var yValue = this.coordinates.yValue(positionY); | |
| 387 | |
| 388 var html = ""; | |
| 389 if (!isNaN(this.chromiumCLNumber_(0))) | |
| 390 html = "r"; | |
| 391 html += this.chromiumCLNumber_(this.current_index_); | |
| 392 var webkitCLNumber = this.clNumbers_[this.current_index_].webkit; | |
| 393 if (webkitCLNumber) { | |
| 394 html += ", webkit "; | |
| 395 if (!isNaN(webkitCLNumber)) | |
| 396 html += "r"; | |
| 397 html += webkitCLNumber; | |
| 398 } | |
| 399 | |
| 400 html += ": " + | |
| 401 addCommas(this.plotData_[0][this.current_index_][0].toFixed(2)) + | |
| 402 " " + this.units_ + " +/- " + | |
| 403 addCommas(this.plotData_[0][this.current_index_][1].toFixed(2)) + | |
| 404 " " + addCommas(yValue.toFixed(2)) + " " + this.units_; | |
| 405 | |
| 406 this.coordinates_td_.innerHTML = html; | |
| 407 | |
| 408 // If there is a horizontal marker, also display deltas relative to it. | |
| 409 if (this.horizontal_marker_) { | |
| 410 var baseline = this.horizontal_marker_.value; | |
| 411 var delta = yValue - baseline; | |
| 412 var fraction = delta / baseline; // allow division by 0 | |
| 413 | |
| 414 var deltaStr = (delta >= 0 ? "+" : "") + delta.toFixed(0) + " " + | |
| 415 this.units_; | |
| 416 var percentStr = (fraction >= 0 ? "+" : "") + | |
| 417 (fraction * 100).toFixed(3) + "%"; | |
| 418 | |
| 419 this.baseline_deltas_td_.innerHTML = deltaStr + ": " + percentStr; | |
| 420 } | |
| 421 | |
| 422 this.updateRuler_(evt); | |
| 423 this.updateCursor_(); | |
| 424 }; | |
| 425 | |
| 426 Plotter.incrementNumericCLNumber = function(value) { | |
| 427 if (isNaN(value)) | |
| 428 return value; | |
| 429 return value + 1; | |
| 430 }; | |
| 431 | |
| 432 Plotter.prototype.onMouseClick_ = function(evt) { | |
| 433 // Shift-click controls the horizontal reference line. | |
| 434 if (evt.shiftKey) { | |
| 435 if (this.horizontal_marker_) { | |
| 436 this.horizontal_marker_.remove_(); | |
| 437 } | |
| 438 var obj = this.canvas_elt_; | |
| 439 var offsetTop = 0; | |
| 440 do { | |
| 441 offsetTop += obj.offsetTop; | |
| 442 } while (obj = obj.offsetParent); | |
| 443 var canvasY = evt.clientY + document.body.scrollTop - offsetTop; | |
| 444 this.horizontal_marker_ = new HorizontalMarker( | |
| 445 this.canvas_elt_, evt.clientY + document.body.scrollTop - offsetTop, | |
| 446 this.coordinates.yValue(canvasY)); | |
| 447 | |
| 448 // Insert before cursor node, otherwise it catches clicks. | |
| 449 this.cursor_div_.parentNode.insertBefore( | |
| 450 this.horizontal_marker_.markerDiv_, this.cursor_div_); | |
| 451 } else { | |
| 452 var index = this.current_index_; | |
| 453 var m = this.marker_div_; | |
| 454 var c = this.cursor_div_; | |
| 455 m.style.top = c.style.top; | |
| 456 m.style.left = c.style.left; | |
| 457 m.style.width = c.style.width; | |
| 458 m.style.height = c.style.height; | |
| 459 if ("onclick" in this) { | |
| 460 var this_x = this.clNumbers_[index]; | |
| 461 // TODO(tonyg): When the clNumber is not numeric, the range includes one | |
| 462 // too many revisions on the starting side. | |
| 463 var prev_x = this_x; | |
| 464 if (index > 0) { | |
| 465 prev_x_source = this.clNumbers_[index-1]; | |
| 466 if (typeof prev_x_source == 'object') { | |
| 467 prev_x = {}; | |
| 468 for (var key in prev_x_source) { | |
| 469 prev_x[key] = Plotter.incrementNumericCLNumber(prev_x_source[key]); | |
| 470 } | |
| 471 } else { | |
| 472 prev_x = Plotter.incrementNumericCLNumber(prev_x_source); | |
| 473 } | |
| 474 } | |
| 475 this.onclick(prev_x, this_x); | |
| 476 } | |
| 477 } | |
| 478 }; | |
| 479 | |
| 480 Plotter.prototype.canvas = function() { | |
| 481 var canvas = document.createElement("CANVAS"); | |
| 482 canvas.setAttribute("class", "plot"); | |
| 483 canvas.setAttribute("width", this.coordinates.widthMax); | |
| 484 canvas.setAttribute("height", this.coordinates.heightMax); | |
| 485 canvas.plotter = this; | |
| 486 return canvas; | |
| 487 }; | |
| 488 | |
| 489 Plotter.prototype.ruler = function() { | |
| 490 var ruler = document.createElement("DIV"); | |
| 491 ruler.setAttribute("class", "plot-ruler"); | |
| 492 ruler.style.borderBottom = "1px dotted black"; | |
| 493 ruler.style.position = "absolute"; | |
| 494 ruler.style.left = "-2px"; | |
| 495 ruler.style.top = "-2px"; | |
| 496 ruler.style.width = "0px"; | |
| 497 ruler.style.height = "0px"; | |
| 498 return ruler; | |
| 499 }; | |
| 500 | |
| 501 Plotter.prototype.graduations = function() { | |
| 502 // Don't allow a graduation in the bottom 5% of the chart | |
| 503 // or the number label would overflow the chart bounds. | |
| 504 var yMin = this.coordinates.yMinValue + .05 * this.coordinates.yValueRange(); | |
| 505 var yRange = this.coordinates.yMaxValue - yMin; | |
| 506 | |
| 507 // Use the largest scale that fits 3 or more graduations. | |
| 508 // We allow scales of [...,500, 250, 100, 50, 25, 10,...]. | |
| 509 var scale = 5000000000; | |
| 510 while (scale) { | |
| 511 if (Math.floor(yRange / scale) > 2) break; // 5s | |
| 512 scale /= 2; | |
| 513 if (Math.floor(yRange / scale) > 2) break; // 2.5s | |
| 514 scale /= 2.5; | |
| 515 if (Math.floor(yRange / scale) > 2) break; // 1s | |
| 516 scale /= 2; | |
| 517 } | |
| 518 | |
| 519 var graduationPosition = yMin + (scale - yMin % scale); | |
| 520 var graduationDivs = []; | |
| 521 while (graduationPosition < this.coordinates.yMaxValue) { | |
| 522 var graduation = document.createElement("DIV"); | |
| 523 var canvasPosition = this.coordinates.yPoints(graduationPosition); | |
| 524 graduation.style.borderTop = "1px dashed rgba(0,0,0,.08)"; | |
| 525 graduation.style.position = "absolute"; | |
| 526 graduation.style.left = this.canvas_elt_.offsetLeft + "px"; | |
| 527 graduation.style.top = canvasPosition + this.canvas_elt_.offsetTop + "px"; | |
| 528 graduation.style.width = | |
| 529 this.canvas_elt_.offsetWidth - 4 + "px"; | |
| 530 graduation.style.paddingLeft = "4px"; | |
| 531 graduation.style.color = "rgba(0,0,0,.4)"; | |
| 532 graduation.style.fontSize = "9px"; | |
| 533 graduation.style.paddingTop = "0"; | |
| 534 graduation.style.zIndex = "-1"; | |
| 535 graduation.innerHTML = addCommas(graduationPosition); | |
| 536 graduationDivs.push(graduation); | |
| 537 graduationPosition += scale; | |
| 538 } | |
| 539 return graduationDivs; | |
| 540 }; | |
| 541 | |
| 542 Plotter.prototype.coordinates_ = function() { | |
| 543 var coordinatesDiv = document.createElement("DIV"); | |
| 544 var fontSize = Math.max(0.8, this.width / 400 * 0.75); | |
| 545 fontSize = Math.min(1.0, fontSize); | |
| 546 | |
| 547 var table = document.createElement("table"); | |
| 548 coordinatesDiv.appendChild(table); | |
| 549 table.style.cssText = "border=0; width=100%; font-size:" + fontSize + "em;"; | |
| 550 var tr = document.createElement("tr"); | |
| 551 table.appendChild(tr); | |
| 552 var td = document.createElement("td"); | |
| 553 tr.appendChild(td); | |
| 554 td.className = "legend"; | |
| 555 td.innerHTML = "Legend: "; | |
| 556 | |
| 557 for (var i = 0; i < this.dataDescription_.length; i++) { | |
| 558 if (i > 0) | |
| 559 td.appendChild(document.createTextNode(", ")); | |
| 560 var legendItem = document.createElement("a"); | |
| 561 td.appendChild(legendItem); | |
| 562 legendItem.className = "legend_item"; | |
| 563 legendItem.href = "#"; | |
| 564 legendItem.style.cssText = "text-decoration: none; color: " + | |
| 565 this.getDataColor(i); | |
| 566 var obj = this; | |
| 567 legendItem.onclick = ( | |
| 568 function(){ | |
| 569 var index = i; | |
| 570 return function() { | |
| 571 this.style.fontWeight = (obj.toggleSelection(index)) ? | |
| 572 "bold" : "normal"; | |
| 573 return false; | |
| 574 }; | |
| 575 })(); | |
| 576 legendItem.innerHTML = this.dataDescription_[i]; | |
| 577 } | |
| 578 | |
| 579 this.coordinates_td_ = document.createElement("td"); | |
| 580 this.coordinates_td_.innerHTML = "<i>move mouse over graph</i>"; | |
| 581 tr.appendChild(this.coordinates_td_); | |
| 582 | |
| 583 this.baseline_deltas_td_ = document.createElement("td"); | |
| 584 this.baseline_deltas_td_.style.color = HorizontalMarker.COLOR; | |
| 585 tr.appendChild(this.baseline_deltas_td_); | |
| 586 | |
| 587 return coordinatesDiv; | |
| 588 }; | |
| 589 | |
| 590 Plotter.prototype.makeColor = function(i) { | |
| 591 var index = i % this.colors.length; | |
| 592 return "rgb(" + this.colors[index][0] + "," + | |
| 593 this.colors[index][1] + "," + | |
| 594 this.colors[index][2] + ")"; | |
| 595 }; | |
| 596 | |
| 597 Plotter.prototype.getDataColor = function(i) { | |
| 598 if (this.dataColors_[i]) { | |
| 599 return this.dataColors_[i]; | |
| 600 } else { | |
| 601 return this.makeColor(i); | |
| 602 } | |
| 603 }; | |
| 604 | |
| 605 Plotter.prototype.log = function(val) { | |
| 606 document.getElementById('log').appendChild( | |
| 607 document.createTextNode(val + '\n')); | |
| 608 }; | |
| OLD | NEW |