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