1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 /** 6 * A TimelineGraphView displays a timeline graph on a canvas element. 7 */ 8 var TimelineGraphView = (function() { 9 'use strict'; 10 11 // Default starting scale factor, in terms of milliseconds per pixel. 12 var DEFAULT_SCALE = 1000; 13 14 // Maximum number of labels placed vertically along the sides of the graph. 15 var MAX_VERTICAL_LABELS = 6; 16 17 // Vertical spacing between labels and between the graph and labels. 18 var LABEL_VERTICAL_SPACING = 4; 19 // Horizontal spacing between vertically placed labels and the edges of the 20 // graph. 21 var LABEL_HORIZONTAL_SPACING = 3; 22 // Horizintal spacing between two horitonally placed labels along the bottom 23 // of the graph. 24 var LABEL_LABEL_HORIZONTAL_SPACING = 25; 25 26 // Length of ticks, in pixels, next to y-axis labels. The x-axis only has 27 // one set of labels, so it can use lines instead. 28 var Y_AXIS_TICK_LENGTH = 10; 29 30 var GRID_COLOR = '#CCC'; 31 var TEXT_COLOR = '#000'; 32 var BACKGROUND_COLOR = '#FFF'; 33 34 /** 35 * @constructor 36 */ 37 function TimelineGraphView(divId, canvasId) { 38 this.scrollbar_ = {position_: 0, range_: 0}; 39 40 this.graphDiv_ = $(divId); 41 this.canvas_ = $(canvasId); 42 43 // Set the range and scale of the graph. Times are in milliseconds since 44 // the Unix epoch. 45 46 // All measurements we have must be after this time. 47 this.startTime_ = 0; 48 // The current rightmost position of the graph is always at most this. 49 this.endTime_ = 1; 50 51 this.graph_ = null; 52 53 // Initialize the scrollbar. 54 this.updateScrollbarRange_(true); 55 } 56 57 TimelineGraphView.prototype = { 58 // Returns the total length of the graph, in pixels. 59 getLength_: function() { 60 var timeRange = this.endTime_ - this.startTime_; 61 // Math.floor is used to ignore the last partial area, of length less 62 // than DEFAULT_SCALE. 63 return Math.floor(timeRange / DEFAULT_SCALE); 64 }, 65 66 /** 67 * Returns true if the graph is scrolled all the way to the right. 68 */ 69 graphScrolledToRightEdge_: function() { 70 return this.scrollbar_.position_ == this.scrollbar_.range_; 71 }, 72 73 /** 74 * Update the range of the scrollbar. If |resetPosition| is true, also 75 * sets the slider to point at the rightmost position and triggers a 76 * repaint. 77 */ 78 updateScrollbarRange_: function(resetPosition) { 79 var scrollbarRange = this.getLength_() - this.canvas_.width; 80 if (scrollbarRange < 0) 81 scrollbarRange = 0; 82 83 // If we've decreased the range to less than the current scroll position, 84 // we need to move the scroll position. 85 if (this.scrollbar_.position_ > scrollbarRange) 86 resetPosition = true; 87 88 this.scrollbar_.range_ = scrollbarRange; 89 if (resetPosition) { 90 this.scrollbar_.position_ = scrollbarRange; 91 this.repaint(); 92 } 93 }, 94 95 /** 96 * Sets the date range displayed on the graph, switches to the default 97 * scale factor, and moves the scrollbar all the way to the right. 98 */ 99 setDateRange: function(startDate, endDate) { 100 this.startTime_ = startDate.getTime(); 101 this.endTime_ = endDate.getTime(); 102 103 // Safety check. 104 if (this.endTime_ <= this.startTime_) 105 this.startTime_ = this.endTime_ - 1; 106 107 this.updateScrollbarRange_(true); 108 }, 109 110 /** 111 * Updates the end time at the right of the graph to be the current time. 112 * Specifically, updates the scrollbar's range, and if the scrollbar is 113 * all the way to the right, keeps it all the way to the right. Otherwise, 114 * leaves the view as-is and doesn't redraw anything. 115 */ 116 updateEndDate: function() { 117 this.endTime_ = (new Date()).getTime(); 118 this.updateScrollbarRange_(this.graphScrolledToRightEdge_()); 119 }, 120 121 getStartDate: function() { 122 return new Date(this.startTime_); 123 }, 124 125 /** 126 * Replaces the current TimelineDataSeries with |dataSeries|. 127 */ 128 setDataSeries: function(dataSeries) { 129 // Simply recreates the Graph. 130 this.graph_ = new Graph(); 131 for (var i = 0; i < dataSeries.length; ++i) 132 this.graph_.addDataSeries(dataSeries[i]); 133 this.repaint(); 134 }, 135 136 /** 137 * Adds |dataSeries| to the current graph. 138 */ 139 addDataSeries: function(dataSeries) { 140 if (!this.graph_) 141 this.graph_ = new Graph(); 142 this.graph_.addDataSeries(dataSeries); 143 this.repaint(); 144 }, 145 146 /** 147 * Draws the graph on |canvas_|. 148 */ 149 repaint: function() { 150 this.repaintTimerRunning_ = false; 151 152 var width = this.canvas_.width; 153 var height = this.canvas_.height; 154 var context = this.canvas_.getContext('2d'); 155 156 // Clear the canvas. 157 context.fillStyle = BACKGROUND_COLOR; 158 context.fillRect(0, 0, width, height); 159 160 // Try to get font height in pixels. Needed for layout. 161 var fontHeightString = context.font.match(/([0-9]+)px/)[1]; 162 var fontHeight = parseInt(fontHeightString); 163 164 // Safety check, to avoid drawing anything too ugly. 165 if (fontHeightString.length == 0 || fontHeight <= 0 || 166 fontHeight * 4 > height || width < 50) { 167 return; 168 } 169 170 // Save current transformation matrix so we can restore it later. 171 context.save(); 172 173 // The center of an HTML canvas pixel is technically at (0.5, 0.5). This 174 // makes near straight lines look bad, due to anti-aliasing. This 175 // translation reduces the problem a little. 176 context.translate(0.5, 0.5); 177 178 // Figure out what time values to display. 179 var position = this.scrollbar_.position_; 180 // If the entire time range is being displayed, align the right edge of 181 // the graph to the end of the time range. 182 if (this.scrollbar_.range_ == 0) 183 position = this.getLength_() - this.canvas_.width; 184 var visibleStartTime = this.startTime_ + position * DEFAULT_SCALE; 185 186 // Make space at the bottom of the graph for the time labels, and then 187 // draw the labels. 188 var textHeight = height; 189 height -= fontHeight + LABEL_VERTICAL_SPACING; 190 this.drawTimeLabels(context, width, height, textHeight, visibleStartTime); 191 192 // Draw outline of the main graph area. 193 context.strokeStyle = GRID_COLOR; 194 context.strokeRect(0, 0, width - 1, height - 1); 195 196 if (this.graph_) { 197 // Layout graph and have them draw their tick marks. 198 this.graph_.layout( 199 width, height, fontHeight, visibleStartTime, DEFAULT_SCALE); 200 this.graph_.drawTicks(context); 201 202 // Draw the lines of all graphs, and then draw their labels. 203 this.graph_.drawLines(context); 204 this.graph_.drawLabels(context); 205 } 206 207 // Restore original transformation matrix. 208 context.restore(); 209 }, 210 211 /** 212 * Draw time labels below the graph. Takes in start time as an argument 213 * since it may not be |startTime_|, when we're displaying the entire 214 * time range. 215 */ 216 drawTimeLabels: function(context, width, height, textHeight, startTime) { 217 // Draw the labels 1 minute apart. 218 var timeStep = 1000 * 60; 219 220 // Find the time for the first label. This time is a perfect multiple of 221 // timeStep because of how UTC times work. 222 var time = Math.ceil(startTime / timeStep) * timeStep; 223 224 context.textBaseline = 'bottom'; 225 context.textAlign = 'center'; 226 context.fillStyle = TEXT_COLOR; 227 context.strokeStyle = GRID_COLOR; 228 229 // Draw labels and vertical grid lines. 230 while (true) { 231 var x = Math.round((time - startTime) / DEFAULT_SCALE); 232 if (x >= width) 233 break; 234 var text = (new Date(time)).toLocaleTimeString(); 235 context.fillText(text, x, textHeight); 236 context.beginPath(); 237 context.lineTo(x, 0); 238 context.lineTo(x, height); 239 context.stroke(); 240 time += timeStep; 241 } 242 }, 243 244 getDataSeriesCount: function() { 245 if (this.graph_) 246 return this.graph_.dataSeries_.length; 247 return 0; 248 }, 249 250 hasDataSeries: function(dataSeries) { 251 if (this.graph_) 252 return this.graph_.hasDataSeries(dataSeries); 253 return false; 254 }, 255 256 }; 257 258 /** 259 * A Graph is responsible for drawing all the TimelineDataSeries that have 260 * the same data type. Graphs are responsible for scaling the values, laying 261 * out labels, and drawing both labels and lines for its data series. 262 */ 263 var Graph = (function() { 264 /** 265 * @constructor 266 */ 267 function Graph() { 268 this.dataSeries_ = []; 269 270 // Cached properties of the graph, set in layout. 271 this.width_ = 0; 272 this.height_ = 0; 273 this.fontHeight_ = 0; 274 this.startTime_ = 0; 275 this.scale_ = 0; 276 277 // At least the highest value in the displayed range of the graph. 278 // Used for scaling and setting labels. Set in layoutLabels. 279 this.max_ = 0; 280 281 // Cached text of equally spaced labels. Set in layoutLabels. 282 this.labels_ = []; 283 } 284 285 /** 286 * A Label is the label at a particular position along the y-axis. 287 * @constructor 288 */ 289 function Label(height, text) { 290 this.height = height; 291 this.text = text; 292 } 293 294 Graph.prototype = { 295 addDataSeries: function(dataSeries) { 296 this.dataSeries_.push(dataSeries); 297 }, 298 299 hasDataSeries: function(dataSeries) { 300 for (var i = 0; i < this.dataSeries_.length; ++i) { 301 if (this.dataSeries_[i] == dataSeries) 302 return true; 303 } 304 return false; 305 }, 306 307 /** 308 * Returns a list of all the values that should be displayed for a given 309 * data series, using the current graph layout. 310 */ 311 getValues: function(dataSeries) { 312 if (!dataSeries.isVisible()) 313 return null; 314 return dataSeries.getValues(this.startTime_, this.scale_, this.width_); 315 }, 316 317 /** 318 * Updates the graph's layout. In particular, both the max value and 319 * label positions are updated. Must be called before calling any of the 320 * drawing functions. 321 */ 322 layout: function(width, height, fontHeight, startTime, scale) { 323 this.width_ = width; 324 this.height_ = height; 325 this.fontHeight_ = fontHeight; 326 this.startTime_ = startTime; 327 this.scale_ = scale; 328 329 // Find largest value. 330 var max = 0; 331 for (var i = 0; i < this.dataSeries_.length; ++i) { 332 var values = this.getValues(this.dataSeries_[i]); 333 if (!values) 334 continue; 335 for (var j = 0; j < values.length; ++j) { 336 if (values[j] > max) 337 max = values[j]; 338 } 339 } 340 341 this.layoutLabels_(max); 342 }, 343 344 /** 345 * Lays out labels and sets |max_|, taking the time units into 346 * consideration. |maxValue| is the actual maximum value, and 347 * |max_| will be set to the value of the largest label, which 348 * will be at least |maxValue|. 349 */ 350 layoutLabels_: function(maxValue) { 351 if (maxValue < 1024) { 352 this.layoutLabelsBasic_(maxValue, 0); 353 return; 354 } 355 356 // Find appropriate units to use. 357 var units = ['', 'k', 'M', 'G', 'T', 'P']; 358 // Units to use for labels. 0 is '1', 1 is K, etc. 359 // We start with 1, and work our way up. 360 var unit = 1; 361 maxValue /= 1024; 362 while (units[unit + 1] && maxValue >= 1024) { 363 maxValue /= 1024; 364 ++unit; 365 } 366 367 // Calculate labels. 368 this.layoutLabelsBasic_(maxValue, 1); 369 370 // Append units to labels. 371 for (var i = 0; i < this.labels_.length; ++i) 372 this.labels_[i] += ' ' + units[unit]; 373 374 // Convert |max_| back to unit '1'. 375 this.max_ *= Math.pow(1024, unit); 376 }, 377 378 /** 379 * Same as layoutLabels_, but ignores units. |maxDecimalDigits| is the 380 * maximum number of decimal digits allowed. The minimum allowed 381 * difference between two adjacent labels is 10^-|maxDecimalDigits|. 382 */ 383 layoutLabelsBasic_: function(maxValue, maxDecimalDigits) { 384 this.labels_ = []; 385 // No labels if |maxValue| is 0. 386 if (maxValue == 0) { 387 this.max_ = maxValue; 388 return; 389 } 390 391 // The maximum number of equally spaced labels allowed. |fontHeight_| 392 // is doubled because the top two labels are both drawn in the same 393 // gap. 394 var minLabelSpacing = 2 * this.fontHeight_ + LABEL_VERTICAL_SPACING; 395 396 // The + 1 is for the top label. 397 var maxLabels = 1 + this.height_ / minLabelSpacing; 398 if (maxLabels < 2) { 399 maxLabels = 2; 400 } else if (maxLabels > MAX_VERTICAL_LABELS) { 401 maxLabels = MAX_VERTICAL_LABELS; 402 } 403 404 // Initial try for step size between conecutive labels. 405 var stepSize = Math.pow(10, -maxDecimalDigits); 406 // Number of digits to the right of the decimal of |stepSize|. 407 // Used for formating label strings. 408 var stepSizeDecimalDigits = maxDecimalDigits; 409 410 // Pick a reasonable step size. 411 while (true) { 412 // If we use a step size of |stepSize| between labels, we'll need: 413 // 414 // Math.ceil(maxValue / stepSize) + 1 415 // 416 // labels. The + 1 is because we need labels at both at 0 and at 417 // the top of the graph. 418 419 // Check if we can use steps of size |stepSize|. 420 if (Math.ceil(maxValue / stepSize) + 1 <= maxLabels) 421 break; 422 // Check |stepSize| * 2. 423 if (Math.ceil(maxValue / (stepSize * 2)) + 1 <= maxLabels) { 424 stepSize *= 2; 425 break; 426 } 427 // Check |stepSize| * 5. 428 if (Math.ceil(maxValue / (stepSize * 5)) + 1 <= maxLabels) { 429 stepSize *= 5; 430 break; 431 } 432 stepSize *= 10; 433 if (stepSizeDecimalDigits > 0) 434 --stepSizeDecimalDigits; 435 } 436 437 // Set the max so it's an exact multiple of the chosen step size. 438 this.max_ = Math.ceil(maxValue / stepSize) * stepSize; 439 440 // Create labels. 441 for (var label = this.max_; label >= 0; label -= stepSize) 442 this.labels_.push(label.toFixed(stepSizeDecimalDigits)); 443 }, 444 445 /** 446 * Draws tick marks for each of the labels in |labels_|. 447 */ 448 drawTicks: function(context) { 449 var x1; 450 var x2; 451 x1 = this.width_ - 1; 452 x2 = this.width_ - 1 - Y_AXIS_TICK_LENGTH; 453 454 context.fillStyle = GRID_COLOR; 455 context.beginPath(); 456 for (var i = 1; i < this.labels_.length - 1; ++i) { 457 // The rounding is needed to avoid ugly 2-pixel wide anti-aliased 458 // lines. 459 var y = Math.round(this.height_ * i / (this.labels_.length - 1)); 460 context.moveTo(x1, y); 461 context.lineTo(x2, y); 462 } 463 context.stroke(); 464 }, 465 466 /** 467 * Draws a graph line for each of the data series. 468 */ 469 drawLines: function(context) { 470 // Factor by which to scale all values to convert them to a number from 471 // 0 to height - 1. 472 var scale = 0; 473 var bottom = this.height_ - 1; 474 if (this.max_) 475 scale = bottom / this.max_; 476 477 // Draw in reverse order, so earlier data series are drawn on top of 478 // subsequent ones. 479 for (var i = this.dataSeries_.length - 1; i >= 0; --i) { 480 var values = this.getValues(this.dataSeries_[i]); 481 if (!values) 482 continue; 483 context.strokeStyle = this.dataSeries_[i].getColor(); 484 context.beginPath(); 485 for (var x = 0; x < values.length; ++x) { 486 // The rounding is needed to avoid ugly 2-pixel wide anti-aliased 487 // horizontal lines. 488 context.lineTo(x, bottom - Math.round(values[x] * scale)); 489 } 490 context.stroke(); 491 } 492 }, 493 494 /** 495 * Draw labels in |labels_|. 496 */ 497 drawLabels: function(context) { 498 if (this.labels_.length == 0) 499 return; 500 var x = this.width_ - LABEL_HORIZONTAL_SPACING; 501 502 // Set up the context. 503 context.fillStyle = TEXT_COLOR; 504 context.textAlign = 'right'; 505 506 // Draw top label, which is the only one that appears below its tick 507 // mark. 508 context.textBaseline = 'top'; 509 context.fillText(this.labels_[0], x, 0); 510 511 // Draw all the other labels. 512 context.textBaseline = 'bottom'; 513 var step = (this.height_ - 1) / (this.labels_.length - 1); 514 for (var i = 1; i < this.labels_.length; ++i) 515 context.fillText(this.labels_[i], x, step * i); 516 } 517 }; 518 519 return Graph; 520 })(); 521 522 return TimelineGraphView; 523 })(); 524