OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * A TimelineGraphView displays a timeline graph on a canvas element. | 6 * A TimelineGraphView displays a timeline graph on a canvas element. |
7 */ | 7 */ |
8 var TimelineGraphView = (function() { | 8 var TimelineGraphView = (function() { |
9 'use strict'; | 9 'use strict'; |
10 | 10 |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 // Restore original transformation matrix. | 207 // Restore original transformation matrix. |
208 context.restore(); | 208 context.restore(); |
209 }, | 209 }, |
210 | 210 |
211 /** | 211 /** |
212 * Draw time labels below the graph. Takes in start time as an argument | 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 | 213 * since it may not be |startTime_|, when we're displaying the entire |
214 * time range. | 214 * time range. |
215 */ | 215 */ |
216 drawTimeLabels: function(context, width, height, textHeight, startTime) { | 216 drawTimeLabels: function(context, width, height, textHeight, startTime) { |
217 // Text for a time string to use in determining how far apart | 217 // Draw the labels 1 minute apart. |
218 // to place text labels. | 218 var timeStep = 1000 * 60; |
219 var sampleText = (new Date(startTime)).toLocaleTimeString(); | |
220 | |
221 // The desired spacing for text labels. | |
222 var targetSpacing = context.measureText(sampleText).width + | |
223 LABEL_LABEL_HORIZONTAL_SPACING; | |
224 | |
225 // The allowed time step values between adjacent labels. Anything much | |
226 // over a couple minutes isn't terribly realistic, given how much memory | |
227 // we use, and how slow a lot of the net-internals code is. | |
228 var timeStepValues = [ | |
229 1000, // 1 second | |
230 1000 * 5, | |
231 1000 * 30, | |
232 1000 * 60, // 1 minute | |
233 1000 * 60 * 5, | |
234 1000 * 60 * 30, | |
235 1000 * 60 * 60, // 1 hour | |
236 1000 * 60 * 60 * 5 | |
237 ]; | |
238 | |
239 // Find smallest time step value that gives us at least |targetSpacing|, | |
240 // if any. | |
241 var timeStep = null; | |
242 for (var i = 0; i < timeStepValues.length; ++i) { | |
243 if (timeStepValues[i] / DEFAULT_SCALE >= targetSpacing) { | |
244 timeStep = timeStepValues[i]; | |
245 break; | |
246 } | |
247 } | |
248 | |
249 // If no such value, give up. | |
250 if (!timeStep) | |
251 return; | |
252 | 219 |
253 // Find the time for the first label. This time is a perfect multiple of | 220 // Find the time for the first label. This time is a perfect multiple of |
254 // timeStep because of how UTC times work. | 221 // timeStep because of how UTC times work. |
255 var time = Math.ceil(startTime / timeStep) * timeStep; | 222 var time = Math.ceil(startTime / timeStep) * timeStep; |
256 | 223 |
257 context.textBaseline = 'bottom'; | 224 context.textBaseline = 'bottom'; |
258 context.textAlign = 'center'; | 225 context.textAlign = 'center'; |
259 context.fillStyle = TEXT_COLOR; | 226 context.fillStyle = TEXT_COLOR; |
260 context.strokeStyle = GRID_COLOR; | 227 context.strokeStyle = GRID_COLOR; |
261 | 228 |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 for (var i = 1; i < this.labels_.length; ++i) | 514 for (var i = 1; i < this.labels_.length; ++i) |
548 context.fillText(this.labels_[i], x, step * i); | 515 context.fillText(this.labels_[i], x, step * i); |
549 } | 516 } |
550 }; | 517 }; |
551 | 518 |
552 return Graph; | 519 return Graph; |
553 })(); | 520 })(); |
554 | 521 |
555 return TimelineGraphView; | 522 return TimelineGraphView; |
556 })(); | 523 })(); |
OLD | NEW |