OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 // We inherit from TopMidBottomView. |
| 11 var superClass = TopMidBottomView; |
| 12 |
| 13 // Default starting scale factor, in terms of milliseconds per pixel. |
| 14 var DEFAULT_SCALE = 1000; |
| 15 |
| 16 // Maximum number of labels placed vertically along the sides of the graph. |
| 17 var MAX_VERTICAL_LABELS = 6; |
| 18 |
| 19 // Vertical spacing between labels and between the graph and labels. |
| 20 var LABEL_VERTICAL_SPACING = 4; |
| 21 // Horizontal spacing between vertically placed labels and the edges of the |
| 22 // graph. |
| 23 var LABEL_HORIZONTAL_SPACING = 3; |
| 24 // Horizintal spacing between two horitonally placed labels along the bottom |
| 25 // of the graph. |
| 26 var LABEL_LABEL_HORIZONTAL_SPACING = 25; |
| 27 |
| 28 // Length of ticks, in pixels, next to y-axis labels. The x-axis only has |
| 29 // one set of labels, so it can use lines instead. |
| 30 var Y_AXIS_TICK_LENGTH = 10; |
| 31 |
| 32 // The number of units mouse wheel deltas increase for each tick of the |
| 33 // wheel. |
| 34 var MOUSE_WHEEL_UNITS_PER_CLICK = 120; |
| 35 |
| 36 // Amount we zoom for one vertical tick of the mouse wheel, as a ratio. |
| 37 var MOUSE_WHEEL_ZOOM_RATE = 1.25; |
| 38 // Amount we scroll for one horizontal tick of the mouse wheel, in pixels. |
| 39 var MOUSE_WHEEL_SCROLL_RATE = MOUSE_WHEEL_UNITS_PER_CLICK; |
| 40 // Number of pixels to scroll per pixel the mouse is dragged. |
| 41 var MOUSE_WHEEL_DRAG_RATE = 3; |
| 42 |
| 43 var GRID_COLOR = '#CCC'; |
| 44 var TEXT_COLOR = '#000'; |
| 45 var BACKGROUND_COLOR = '#FFF'; |
| 46 |
| 47 // Which side of the canvas y-axis labels should go on, for a given Graph. |
| 48 // TODO(mmenke): Figure out a reasonable way to handle more than 2 sets |
| 49 // of labels. |
| 50 var LabelAlign = { |
| 51 LEFT: 0, |
| 52 RIGHT: 1 |
| 53 }; |
| 54 |
| 55 /** |
| 56 * @constructor |
| 57 */ |
| 58 function TimelineGraphView(divId, canvasId, scrollbarId, scrollbarInnerId) { |
| 59 this.scrollbar_ = new HorizontalScrollbarView(scrollbarId, |
| 60 scrollbarInnerId, |
| 61 this.onScroll_.bind(this)); |
| 62 // Call superclass's constructor. |
| 63 superClass.call(this, null, new DivView(divId), this.scrollbar_); |
| 64 |
| 65 this.graphDiv_ = $(divId); |
| 66 this.canvas_ = $(canvasId); |
| 67 this.canvas_.onmousewheel = this.onMouseWheel_.bind(this); |
| 68 this.canvas_.onmousedown = this.onMouseDown_.bind(this); |
| 69 this.canvas_.onmousemove = this.onMouseMove_.bind(this); |
| 70 this.canvas_.onmouseup = this.onMouseUp_.bind(this); |
| 71 this.canvas_.onmouseout = this.onMouseUp_.bind(this); |
| 72 |
| 73 // Used for click and drag scrolling of graph. Drag-zooming not supported, |
| 74 // for a more stable scrolling experience. |
| 75 this.isDragging_ = false; |
| 76 this.dragX_ = 0; |
| 77 |
| 78 // Set the range and scale of the graph. Times are in milliseconds since |
| 79 // the Unix epoch. |
| 80 |
| 81 // All measurements we have must be after this time. |
| 82 this.startTime_ = 0; |
| 83 // The current rightmost position of the graph is always at most this. |
| 84 // We may have some later events. When actively capturing new events, it's |
| 85 // updated on a timer. |
| 86 this.endTime_ = 1; |
| 87 |
| 88 // Current scale, in terms of milliseconds per pixel. Each column of |
| 89 // pixels represents a point in time |scale_| milliseconds after the |
| 90 // previous one. We only display times that are of the form |
| 91 // |startTime_| + K * |scale_| to avoid jittering, and the rightmost |
| 92 // pixel that we can display has a time <= |endTime_|. Non-integer values |
| 93 // are allowed. |
| 94 this.scale_ = DEFAULT_SCALE; |
| 95 |
| 96 this.graphs_ = []; |
| 97 |
| 98 // Initialize the scrollbar. |
| 99 this.updateScrollbarRange_(true); |
| 100 } |
| 101 |
| 102 // Smallest allowed scaling factor. |
| 103 TimelineGraphView.MIN_SCALE = 5; |
| 104 |
| 105 TimelineGraphView.prototype = { |
| 106 // Inherit the superclass's methods. |
| 107 __proto__: superClass.prototype, |
| 108 |
| 109 setGeometry: function(left, top, width, height) { |
| 110 superClass.prototype.setGeometry.call(this, left, top, width, height); |
| 111 |
| 112 // The size of the canvas can only be set by using its |width| and |
| 113 // |height| properties, which do not take padding into account, so we |
| 114 // need to use them ourselves. |
| 115 var style = getComputedStyle(this.canvas_); |
| 116 var horizontalPadding = parseInt(style.paddingRight) + |
| 117 parseInt(style.paddingLeft); |
| 118 var verticalPadding = parseInt(style.paddingTop) + |
| 119 parseInt(style.paddingBottom); |
| 120 var width = parseInt(this.graphDiv_.style.width) - horizontalPadding; |
| 121 // For unknown reasons, there's an extra 3 pixels border between the |
| 122 // bottom of the canvas and the bottom margin of the enclosing div. |
| 123 var height = parseInt(this.graphDiv_.style.height) - verticalPadding - 3; |
| 124 |
| 125 // Protect against degenerates. |
| 126 if (width < 10) |
| 127 width = 10; |
| 128 if (height < 10) |
| 129 height = 10; |
| 130 |
| 131 this.canvas_.width = width; |
| 132 this.canvas_.height = height; |
| 133 |
| 134 // Use the same font style for the canvas as we use elsewhere. |
| 135 // Has to be updated every resize. |
| 136 this.canvas_.getContext('2d').font = getComputedStyle(this.canvas_).font; |
| 137 |
| 138 this.updateScrollbarRange_(this.graphScrolledToRightEdge_()); |
| 139 this.repaint(); |
| 140 }, |
| 141 |
| 142 show: function(isVisible) { |
| 143 superClass.prototype.show.call(this, isVisible); |
| 144 if (isVisible) |
| 145 this.repaint(); |
| 146 }, |
| 147 |
| 148 // Returns the total length of the graph, in pixels. |
| 149 getLength_ : function() { |
| 150 var timeRange = this.endTime_ - this.startTime_; |
| 151 // Math.floor is used to ignore the last partial area, of length less |
| 152 // than |scale_|. |
| 153 return Math.floor(timeRange / this.scale_); |
| 154 }, |
| 155 |
| 156 /** |
| 157 * Returns true if the graph is scrolled all the way to the right. |
| 158 */ |
| 159 graphScrolledToRightEdge_: function() { |
| 160 return this.scrollbar_.getPosition() == this.scrollbar_.getRange(); |
| 161 }, |
| 162 |
| 163 /** |
| 164 * Update the range of the scrollbar. If |resetPosition| is true, also |
| 165 * sets the slider to point at the rightmost position and triggers a |
| 166 * repaint. |
| 167 */ |
| 168 updateScrollbarRange_: function(resetPosition) { |
| 169 var scrollbarRange = this.getLength_() - this.canvas_.width; |
| 170 if (scrollbarRange < 0) |
| 171 scrollbarRange = 0; |
| 172 |
| 173 // If we've decreased the range to less than the current scroll position, |
| 174 // we need to move the scroll position. |
| 175 if (this.scrollbar_.getPosition() > scrollbarRange) |
| 176 resetPosition = true; |
| 177 |
| 178 this.scrollbar_.setRange(scrollbarRange); |
| 179 if (resetPosition) { |
| 180 this.scrollbar_.setPosition(scrollbarRange); |
| 181 this.repaint(); |
| 182 } |
| 183 }, |
| 184 |
| 185 /** |
| 186 * Sets the date range displayed on the graph, switches to the default |
| 187 * scale factor, and moves the scrollbar all the way to the right. |
| 188 */ |
| 189 setDateRange: function(startDate, endDate) { |
| 190 this.startTime_ = startDate.getTime(); |
| 191 this.endTime_ = endDate.getTime(); |
| 192 |
| 193 // Safety check. |
| 194 if (this.endTime_ <= this.startTime_) |
| 195 this.startTime_ = this.endTime_ - 1; |
| 196 |
| 197 this.scale_ = DEFAULT_SCALE; |
| 198 this.updateScrollbarRange_(true); |
| 199 }, |
| 200 |
| 201 /** |
| 202 * Updates the end time at the right of the graph to be the current time. |
| 203 * Specifically, updates the scrollbar's range, and if the scrollbar is |
| 204 * all the way to the right, keeps it all the way to the right. Otherwise, |
| 205 * leaves the view as-is and doesn't redraw anything. |
| 206 */ |
| 207 updateEndDate: function() { |
| 208 this.endTime_ = timeutil.getCurrentTime(); |
| 209 this.updateScrollbarRange_(this.graphScrolledToRightEdge_()); |
| 210 }, |
| 211 |
| 212 getStartDate: function() { |
| 213 return new Date(this.startTime_); |
| 214 }, |
| 215 |
| 216 /** |
| 217 * Scrolls the graph horizontally by the specified amount. |
| 218 */ |
| 219 horizontalScroll_: function(delta) { |
| 220 var newPosition = this.scrollbar_.getPosition() + Math.round(delta); |
| 221 // Make sure the new position is in the right range. |
| 222 if (newPosition < 0) { |
| 223 newPosition = 0; |
| 224 } else if (newPosition > this.scrollbar_.getRange()) { |
| 225 newPosition = this.scrollbar_.getRange(); |
| 226 } |
| 227 |
| 228 if (this.scrollbar_.getPosition() == newPosition) |
| 229 return; |
| 230 this.scrollbar_.setPosition(newPosition); |
| 231 this.onScroll_(); |
| 232 }, |
| 233 |
| 234 /** |
| 235 * Zooms the graph by the specified amount. |
| 236 */ |
| 237 zoom_: function(ratio) { |
| 238 var oldScale = this.scale_; |
| 239 this.scale_ *= ratio; |
| 240 if (this.scale_ < TimelineGraphView.MIN_SCALE) |
| 241 this.scale_ = TimelineGraphView.MIN_SCALE; |
| 242 |
| 243 if (this.scale_ == oldScale) |
| 244 return; |
| 245 |
| 246 // If we were at the end of the range before, remain at the end of the |
| 247 // range. |
| 248 if (this.graphScrolledToRightEdge_()) { |
| 249 this.updateScrollbarRange_(true); |
| 250 return; |
| 251 } |
| 252 |
| 253 // Otherwise, do our best to maintain the old position. We use the |
| 254 // position at the far right of the graph for consistency. |
| 255 var oldMaxTime = |
| 256 oldScale * (this.scrollbar_.getPosition() + this.canvas_.width); |
| 257 var newMaxTime = Math.round(oldMaxTime / this.scale_); |
| 258 var newPosition = newMaxTime - this.canvas_.width; |
| 259 |
| 260 // Update range and scroll position. |
| 261 this.updateScrollbarRange_(false); |
| 262 this.horizontalScroll_(newPosition - this.scrollbar_.getPosition()); |
| 263 }, |
| 264 |
| 265 onMouseWheel_: function(event) { |
| 266 event.preventDefault(); |
| 267 this.horizontalScroll_( |
| 268 MOUSE_WHEEL_SCROLL_RATE * |
| 269 -event.wheelDeltaX / MOUSE_WHEEL_UNITS_PER_CLICK); |
| 270 this.zoom_(Math.pow(MOUSE_WHEEL_ZOOM_RATE, |
| 271 -event.wheelDeltaY / MOUSE_WHEEL_UNITS_PER_CLICK)); |
| 272 }, |
| 273 |
| 274 onMouseDown_: function(event) { |
| 275 event.preventDefault(); |
| 276 this.isDragging_ = true; |
| 277 this.dragX_ = event.clientX; |
| 278 }, |
| 279 |
| 280 onMouseMove_: function(event) { |
| 281 if (!this.isDragging_) |
| 282 return; |
| 283 event.preventDefault(); |
| 284 this.horizontalScroll_( |
| 285 MOUSE_WHEEL_DRAG_RATE * (event.clientX - this.dragX_)); |
| 286 this.dragX_ = event.clientX; |
| 287 }, |
| 288 |
| 289 onMouseUp_: function(event) { |
| 290 this.isDragging_ = false; |
| 291 }, |
| 292 |
| 293 onScroll_: function() { |
| 294 this.repaint(); |
| 295 }, |
| 296 |
| 297 /** |
| 298 * Replaces the current TimelineDataSeries with |dataSeries|. |
| 299 */ |
| 300 setDataSeries: function(dataSeries) { |
| 301 // Simplest just to recreate the Graphs. |
| 302 this.graphs_ = []; |
| 303 this.graphs_[TimelineDataType.BYTES_PER_SECOND] = |
| 304 new Graph(TimelineDataType.BYTES_PER_SECOND, LabelAlign.RIGHT); |
| 305 this.graphs_[TimelineDataType.SOURCE_COUNT] = |
| 306 new Graph(TimelineDataType.SOURCE_COUNT, LabelAlign.LEFT); |
| 307 for (var i = 0; i < dataSeries.length; ++i) |
| 308 this.graphs_[dataSeries[i].getDataType()].addDataSeries(dataSeries[i]); |
| 309 |
| 310 this.repaint(); |
| 311 }, |
| 312 |
| 313 /** |
| 314 * Draws the graph on |canvas_|. |
| 315 */ |
| 316 repaint: function() { |
| 317 this.repaintTimerRunning_ = false; |
| 318 if (!this.isVisible()) |
| 319 return; |
| 320 |
| 321 var width = this.canvas_.width; |
| 322 var height = this.canvas_.height; |
| 323 var context = this.canvas_.getContext('2d'); |
| 324 |
| 325 // Clear the canvas. |
| 326 context.fillStyle = BACKGROUND_COLOR; |
| 327 context.fillRect(0, 0, width, height); |
| 328 |
| 329 // Try to get font height in pixels. Needed for layout. |
| 330 var fontHeightString = context.font.match(/([0-9]+)px/)[1]; |
| 331 var fontHeight = parseInt(fontHeightString); |
| 332 |
| 333 // Safety check, to avoid drawing anything too ugly. |
| 334 if (fontHeightString.length == 0 || fontHeight <= 0 || |
| 335 fontHeight * 4 > height || width < 50) { |
| 336 return; |
| 337 } |
| 338 |
| 339 // Save current transformation matrix so we can restore it later. |
| 340 context.save(); |
| 341 |
| 342 // The center of an HTML canvas pixel is technically at (0.5, 0.5). This |
| 343 // makes near straight lines look bad, due to anti-aliasing. This |
| 344 // translation reduces the problem a little. |
| 345 context.translate(0.5, 0.5); |
| 346 |
| 347 // Figure out what time values to display. |
| 348 var position = this.scrollbar_.getPosition(); |
| 349 // If the entire time range is being displayed, align the right edge of |
| 350 // the graph to the end of the time range. |
| 351 if (this.scrollbar_.getRange() == 0) |
| 352 position = this.getLength_() - this.canvas_.width; |
| 353 var visibleStartTime = this.startTime_ + position * this.scale_; |
| 354 |
| 355 // Make space at the bottom of the graph for the time labels, and then |
| 356 // draw the labels. |
| 357 height -= fontHeight + LABEL_VERTICAL_SPACING; |
| 358 this.drawTimeLabels(context, width, height, visibleStartTime); |
| 359 |
| 360 // Draw outline of the main graph area. |
| 361 context.strokeStyle = GRID_COLOR; |
| 362 context.strokeRect(0, 0, width - 1, height - 1); |
| 363 |
| 364 // Layout graphs and have them draw their tick marks. |
| 365 for (var i = 0; i < this.graphs_.length; ++i) { |
| 366 this.graphs_[i].layout(width, height, fontHeight, visibleStartTime, |
| 367 this.scale_); |
| 368 this.graphs_[i].drawTicks(context); |
| 369 } |
| 370 |
| 371 // Draw the lines of all graphs, and then draw their labels. |
| 372 for (var i = 0; i < this.graphs_.length; ++i) |
| 373 this.graphs_[i].drawLines(context); |
| 374 for (var i = 0; i < this.graphs_.length; ++i) |
| 375 this.graphs_[i].drawLabels(context); |
| 376 |
| 377 // Restore original transformation matrix. |
| 378 context.restore(); |
| 379 }, |
| 380 |
| 381 /** |
| 382 * Draw time labels below the graph. Takes in start time as an argument |
| 383 * since it may not be |startTime_|, when we're displaying the entire |
| 384 * time range. |
| 385 */ |
| 386 drawTimeLabels: function(context, width, height, startTime) { |
| 387 var textHeight = height + LABEL_VERTICAL_SPACING; |
| 388 |
| 389 // Text for a time string to use in determining how far apart |
| 390 // to place text labels. |
| 391 var sampleText = (new Date(startTime)).toLocaleTimeString(); |
| 392 |
| 393 // The desired spacing for text labels. |
| 394 var targetSpacing = context.measureText(sampleText).width + |
| 395 LABEL_LABEL_HORIZONTAL_SPACING; |
| 396 |
| 397 // The allowed time step values between adjacent labels. Anything much |
| 398 // over a couple minutes isn't terribly realistic, given how much memory |
| 399 // we use, and how slow a lot of the net-internals code is. |
| 400 var timeStepValues = [ |
| 401 1000, // 1 second |
| 402 1000 * 5, |
| 403 1000 * 30, |
| 404 1000 * 60, // 1 minute |
| 405 1000 * 60 * 5, |
| 406 1000 * 60 * 30, |
| 407 1000 * 60 * 60, // 1 hour |
| 408 1000 * 60 * 60 * 5 |
| 409 ]; |
| 410 |
| 411 // Find smallest time step value that gives us at least |targetSpacing|, |
| 412 // if any. |
| 413 var timeStep = null; |
| 414 for (var i = 0; i < timeStepValues.length; ++i) { |
| 415 if (timeStepValues[i] / this.scale_ >= targetSpacing) { |
| 416 timeStep = timeStepValues[i]; |
| 417 break; |
| 418 } |
| 419 } |
| 420 |
| 421 // If no such value, give up. |
| 422 if (!timeStep) |
| 423 return; |
| 424 |
| 425 // Find the time for the first label. This time is a perfect multiple of |
| 426 // timeStep because of how UTC times work. |
| 427 var time = Math.ceil(startTime / timeStep) * timeStep; |
| 428 |
| 429 context.textBaseline = 'top'; |
| 430 context.textAlign = 'center'; |
| 431 context.fillStyle = TEXT_COLOR; |
| 432 context.strokeStyle = GRID_COLOR; |
| 433 |
| 434 // Draw labels and vertical grid lines. |
| 435 while (true) { |
| 436 var x = Math.round((time - startTime) / this.scale_); |
| 437 if (x >= width) |
| 438 break; |
| 439 var text = (new Date(time)).toLocaleTimeString(); |
| 440 context.fillText(text, x, textHeight); |
| 441 context.beginPath(); |
| 442 context.lineTo(x, 0); |
| 443 context.lineTo(x, height); |
| 444 context.stroke(); |
| 445 time += timeStep; |
| 446 } |
| 447 } |
| 448 }; |
| 449 |
| 450 /** |
| 451 * A Graph is responsible for drawing all the TimelineDataSeries that have |
| 452 * the same data type. Graphs are responsible for scaling the values, laying |
| 453 * out labels, and drawing both labels and lines for its data series. |
| 454 */ |
| 455 var Graph = (function() { |
| 456 /** |
| 457 * |dataType| is the DataType that will be shared by all its DataSeries. |
| 458 * |labelAlign| is the LabelAlign value indicating whether the labels |
| 459 * should be aligned to the right of left of the graph. |
| 460 * @constructor |
| 461 */ |
| 462 function Graph(dataType, labelAlign) { |
| 463 this.dataType_ = dataType; |
| 464 this.dataSeries_ = []; |
| 465 this.labelAlign_ = labelAlign; |
| 466 |
| 467 // Cached properties of the graph, set in layout. |
| 468 this.width_ = 0; |
| 469 this.height_ = 0; |
| 470 this.fontHeight_ = 0; |
| 471 this.startTime_ = 0; |
| 472 this.scale_ = 0; |
| 473 |
| 474 // At least the highest value in the displayed range of the graph. |
| 475 // Used for scaling and setting labels. Set in layoutLabels. |
| 476 this.max_ = 0; |
| 477 |
| 478 // Cached text of equally spaced labels. Set in layoutLabels. |
| 479 this.labels_ = []; |
| 480 } |
| 481 |
| 482 /** |
| 483 * A Label is the label at a particular position along the y-axis. |
| 484 * @constructor |
| 485 */ |
| 486 function Label(height, text) { |
| 487 this.height = height; |
| 488 this.text = text; |
| 489 } |
| 490 |
| 491 Graph.prototype = { |
| 492 addDataSeries: function(dataSeries) { |
| 493 this.dataSeries_.push(dataSeries); |
| 494 }, |
| 495 |
| 496 /** |
| 497 * Returns a list of all the values that should be displayed for a given |
| 498 * data series, using the current graph layout. |
| 499 */ |
| 500 getValues: function(dataSeries) { |
| 501 if (!dataSeries.isVisible()) |
| 502 return null; |
| 503 return dataSeries.getValues(this.startTime_, this.scale_, this.width_); |
| 504 }, |
| 505 |
| 506 /** |
| 507 * Updates the graph's layout. In particular, both the max value and |
| 508 * label positions are updated. Must be called before calling any of the |
| 509 * drawing functions. |
| 510 */ |
| 511 layout: function(width, height, fontHeight, startTime, scale) { |
| 512 this.width_ = width; |
| 513 this.height_ = height; |
| 514 this.fontHeight_ = fontHeight; |
| 515 this.startTime_ = startTime; |
| 516 this.scale_ = scale; |
| 517 |
| 518 // Find largest value. |
| 519 var max = 0; |
| 520 for (var i = 0; i < this.dataSeries_.length; ++i) { |
| 521 var values = this.getValues(this.dataSeries_[i]); |
| 522 if (!values) |
| 523 continue; |
| 524 for (var j = 0; j < values.length; ++j) { |
| 525 if (values[j] > max) |
| 526 max = values[j]; |
| 527 } |
| 528 } |
| 529 |
| 530 this.layoutLabels_(max); |
| 531 }, |
| 532 |
| 533 /** |
| 534 * Lays out labels and sets |max_|, taking the time units into |
| 535 * consideration. |maxValue| is the actual maximum value, and |
| 536 * |max_| will be set to the value of the largest label, which |
| 537 * will be at least |maxValue|. |
| 538 */ |
| 539 layoutLabels_: function(maxValue) { |
| 540 if (this.dataType_ != TimelineDataType.BYTES_PER_SECOND) { |
| 541 this.layoutLabelsBasic_(maxValue, 0); |
| 542 return; |
| 543 } |
| 544 |
| 545 // Special handling for data rates. |
| 546 |
| 547 // Find appropriate units to use. |
| 548 var units = ['B/s', 'kB/s', 'MB/s', 'GB/s', 'TB/s', 'PB/s']; |
| 549 // Units to use for labels. 0 is bytes, 1 is kilobytes, etc. |
| 550 // We start with kilobytes, and work our way up. |
| 551 var unit = 1; |
| 552 // Update |maxValue| to be in the right units. |
| 553 var maxValue = maxValue / 1024; |
| 554 while (units[unit + 1] && maxValue >= 999) { |
| 555 maxValue /= 1024; |
| 556 ++unit; |
| 557 } |
| 558 |
| 559 // Calculate labels. |
| 560 this.layoutLabelsBasic_(maxValue, 1); |
| 561 |
| 562 // Append units to labels. |
| 563 for (var i = 0; i < this.labels_.length; ++i) |
| 564 this.labels_[i] += ' ' + units[unit]; |
| 565 |
| 566 // Convert |max_| back to bytes, so it can be used when scaling values |
| 567 // for display. |
| 568 this.max_ *= Math.pow(1024, unit); |
| 569 }, |
| 570 |
| 571 /** |
| 572 * Same as layoutLabels_, but ignores units. |maxDecimalDigits| is the |
| 573 * maximum number of decimal digits allowed. The minimum allowed |
| 574 * difference between two adjacent labels is 10^-|maxDecimalDigits|. |
| 575 */ |
| 576 layoutLabelsBasic_: function(maxValue, maxDecimalDigits) { |
| 577 this.labels_ = []; |
| 578 // No labels if |maxValue| is 0. |
| 579 if (maxValue == 0) { |
| 580 this.max_ = maxValue; |
| 581 return; |
| 582 } |
| 583 |
| 584 // The maximum number of equally spaced labels allowed. |fontHeight_| |
| 585 // is doubled because the top two labels are both drawn in the same |
| 586 // gap. |
| 587 var minLabelSpacing = 2 * this.fontHeight_ + LABEL_VERTICAL_SPACING; |
| 588 |
| 589 // The + 1 is for the top label. |
| 590 var maxLabels = 1 + this.height_ / minLabelSpacing; |
| 591 if (maxLabels < 2) { |
| 592 maxLabels = 2; |
| 593 } else if (maxLabels > MAX_VERTICAL_LABELS) { |
| 594 maxLabels = MAX_VERTICAL_LABELS; |
| 595 } |
| 596 |
| 597 // Initial try for step size between conecutive labels. |
| 598 var stepSize = Math.pow(10, -maxDecimalDigits); |
| 599 // Number of digits to the right of the decimal of |stepSize|. |
| 600 // Used for formating label strings. |
| 601 var stepSizeDecimalDigits = maxDecimalDigits; |
| 602 |
| 603 // Pick a reasonable step size. |
| 604 while (true) { |
| 605 // If we use a step size of |stepSize| between labels, we'll need: |
| 606 // |
| 607 // Math.ceil(maxValue / stepSize) + 1 |
| 608 // |
| 609 // labels. The + 1 is because we need labels at both at 0 and at |
| 610 // the top of the graph. |
| 611 |
| 612 // Check if we can use steps of size |stepSize|. |
| 613 if (Math.ceil(maxValue / stepSize) + 1 <= maxLabels) |
| 614 break; |
| 615 // Check |stepSize| * 2. |
| 616 if (Math.ceil(maxValue / (stepSize * 2)) + 1 <= maxLabels) { |
| 617 stepSize *= 2; |
| 618 break; |
| 619 } |
| 620 // Check |stepSize| * 5. |
| 621 if (Math.ceil(maxValue / (stepSize * 5)) + 1 <= maxLabels) { |
| 622 stepSize *= 5; |
| 623 break; |
| 624 } |
| 625 stepSize *= 10; |
| 626 if (stepSizeDecimalDigits > 0) |
| 627 --stepSizeDecimalDigits; |
| 628 } |
| 629 |
| 630 // Set the max so it's an exact multiple of the chosen step size. |
| 631 this.max_ = Math.ceil(maxValue / stepSize) * stepSize; |
| 632 |
| 633 // Create labels. |
| 634 for (var label = this.max_; label >= 0; label -= stepSize) |
| 635 this.labels_.push(label.toFixed(stepSizeDecimalDigits)); |
| 636 }, |
| 637 |
| 638 /** |
| 639 * Draws tick marks for each of the labels in |labels_|. |
| 640 */ |
| 641 drawTicks: function(context) { |
| 642 var x1; |
| 643 var x2; |
| 644 if (this.labelAlign_ == LabelAlign.RIGHT) { |
| 645 x1 = this.width_ - 1; |
| 646 x2 = this.width_ - 1 - Y_AXIS_TICK_LENGTH; |
| 647 } else { |
| 648 x1 = 0; |
| 649 x2 = Y_AXIS_TICK_LENGTH; |
| 650 } |
| 651 |
| 652 context.fillStyle = GRID_COLOR; |
| 653 context.beginPath(); |
| 654 for (var i = 1; i < this.labels_.length - 1; ++i) { |
| 655 // The rounding is needed to avoid ugly 2-pixel wide anti-aliased |
| 656 // lines. |
| 657 var y = Math.round(this.height_ * i / (this.labels_.length - 1)); |
| 658 context.moveTo(x1, y); |
| 659 context.lineTo(x2, y); |
| 660 } |
| 661 context.stroke(); |
| 662 }, |
| 663 |
| 664 /** |
| 665 * Draws a graph line for each of the data series. |
| 666 */ |
| 667 drawLines: function(context) { |
| 668 // Factor by which to scale all values to convert them to a number from |
| 669 // 0 to height - 1. |
| 670 var scale = 0; |
| 671 var bottom = this.height_ - 1; |
| 672 if (this.max_) |
| 673 scale = bottom / this.max_; |
| 674 |
| 675 // Draw in reverse order, so earlier data series are drawn on top of |
| 676 // subsequent ones. |
| 677 for (var i = this.dataSeries_.length - 1; i >= 0; --i) { |
| 678 var values = this.getValues(this.dataSeries_[i]); |
| 679 if (!values) |
| 680 continue; |
| 681 context.strokeStyle = this.dataSeries_[i].getColor(); |
| 682 context.beginPath(); |
| 683 for (var x = 0; x < values.length; ++x) { |
| 684 // The rounding is needed to avoid ugly 2-pixel wide anti-aliased |
| 685 // horizontal lines. |
| 686 context.lineTo(x, bottom - Math.round(values[x] * scale)); |
| 687 } |
| 688 context.stroke(); |
| 689 } |
| 690 }, |
| 691 |
| 692 /** |
| 693 * Draw labels in |labels_|. |
| 694 */ |
| 695 drawLabels: function(context) { |
| 696 if (this.labels_.length == 0) |
| 697 return; |
| 698 var x; |
| 699 if (this.labelAlign_ == LabelAlign.RIGHT) { |
| 700 x = this.width_ - LABEL_HORIZONTAL_SPACING; |
| 701 } else { |
| 702 // Find the width of the widest label. |
| 703 var maxTextWidth = 0; |
| 704 for (var i = 0; i < this.labels_.length; ++i) { |
| 705 var textWidth = context.measureText(this.labels_[i]).width; |
| 706 if (maxTextWidth < textWidth) |
| 707 maxTextWidth = textWidth; |
| 708 } |
| 709 x = maxTextWidth + LABEL_HORIZONTAL_SPACING; |
| 710 } |
| 711 |
| 712 // Set up the context. |
| 713 context.fillStyle = TEXT_COLOR; |
| 714 context.textAlign = 'right'; |
| 715 |
| 716 // Draw top label, which is the only one that appears below its tick |
| 717 // mark. |
| 718 context.textBaseline = 'top'; |
| 719 context.fillText(this.labels_[0], x, 0); |
| 720 |
| 721 // Draw all the other labels. |
| 722 context.textBaseline = 'bottom'; |
| 723 var step = (this.height_ - 1) / (this.labels_.length - 1); |
| 724 for (var i = 1; i < this.labels_.length; ++i) |
| 725 context.fillText(this.labels_[i], x, step * i); |
| 726 } |
| 727 }; |
| 728 |
| 729 return Graph; |
| 730 })(); |
| 731 |
| 732 return TimelineGraphView; |
| 733 })(); |
OLD | NEW |