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.
6 * A TimelineGraphView displays a timeline graph on a canvas element.
8 var TimelineGraphView
= (function() {
11 // Default starting scale factor, in terms of milliseconds per pixel.
12 var DEFAULT_SCALE
= 1000;
14 // Maximum number of labels placed vertically along the sides of the graph.
15 var MAX_VERTICAL_LABELS
= 6;
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
21 var LABEL_HORIZONTAL_SPACING
= 3;
22 // Horizintal spacing between two horitonally placed labels along the bottom
24 var LABEL_LABEL_HORIZONTAL_SPACING
= 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;
30 var GRID_COLOR
= '#CCC';
31 var TEXT_COLOR
= '#000';
32 var BACKGROUND_COLOR
= '#FFF';
37 function TimelineGraphView(divId
, canvasId
) {
38 this.scrollbar_
= {position_
: 0, range_
: 0};
40 this.graphDiv_
= $(divId
);
41 this.canvas_
= $(canvasId
);
43 // Set the range and scale of the graph. Times are in milliseconds since
46 // All measurements we have must be after this time.
48 // The current rightmost position of the graph is always at most this.
53 // Initialize the scrollbar.
54 this.updateScrollbarRange_(true);
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
);
67 * Returns true if the graph is scrolled all the way to the right.
69 graphScrolledToRightEdge_: function() {
70 return this.scrollbar_
.position_
== this.scrollbar_
.range_
;
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
78 updateScrollbarRange_: function(resetPosition
) {
79 var scrollbarRange
= this.getLength_() - this.canvas_
.width
;
80 if (scrollbarRange
< 0)
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
)
88 this.scrollbar_
.range_
= scrollbarRange
;
90 this.scrollbar_
.position_
= scrollbarRange
;
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.
99 setDateRange: function(startDate
, endDate
) {
100 this.startTime_
= startDate
.getTime();
101 this.endTime_
= endDate
.getTime();
104 if (this.endTime_
<= this.startTime_
)
105 this.startTime_
= this.endTime_
- 1;
107 this.updateScrollbarRange_(true);
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.
116 updateEndDate: function() {
117 this.endTime_
= (new Date()).getTime();
118 this.updateScrollbarRange_(this.graphScrolledToRightEdge_());
121 getStartDate: function() {
122 return new Date(this.startTime_
);
126 * Replaces the current TimelineDataSeries with |dataSeries|.
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
]);
137 * Adds |dataSeries| to the current graph.
139 addDataSeries: function(dataSeries
) {
141 this.graph_
= new Graph();
142 this.graph_
.addDataSeries(dataSeries
);
147 * Draws the graph on |canvas_|.
149 repaint: function() {
150 this.repaintTimerRunning_
= false;
152 var width
= this.canvas_
.width
;
153 var height
= this.canvas_
.height
;
154 var context
= this.canvas_
.getContext('2d');
157 context
.fillStyle
= BACKGROUND_COLOR
;
158 context
.fillRect(0, 0, width
, height
);
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
);
164 // Safety check, to avoid drawing anything too ugly.
165 if (fontHeightString
.length
== 0 || fontHeight
<= 0 ||
166 fontHeight
* 4 > height
|| width
< 50) {
170 // Save current transformation matrix so we can restore it later.
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);
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
;
186 // Make space at the bottom of the graph for the time labels, and then
188 var textHeight
= height
;
189 height
-= fontHeight
+ LABEL_VERTICAL_SPACING
;
190 this.drawTimeLabels(context
, width
, height
, textHeight
, visibleStartTime
);
192 // Draw outline of the main graph area.
193 context
.strokeStyle
= GRID_COLOR
;
194 context
.strokeRect(0, 0, width
- 1, height
- 1);
197 // Layout graph and have them draw their tick marks.
199 width
, height
, fontHeight
, visibleStartTime
, DEFAULT_SCALE
);
200 this.graph_
.drawTicks(context
);
202 // Draw the lines of all graphs, and then draw their labels.
203 this.graph_
.drawLines(context
);
204 this.graph_
.drawLabels(context
);
207 // Restore original transformation matrix.
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
216 drawTimeLabels: function(context
, width
, height
, textHeight
, startTime
) {
217 // Draw the labels 1 minute apart.
218 var timeStep
= 1000 * 60;
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
;
224 context
.textBaseline
= 'bottom';
225 context
.textAlign
= 'center';
226 context
.fillStyle
= TEXT_COLOR
;
227 context
.strokeStyle
= GRID_COLOR
;
229 // Draw labels and vertical grid lines.
231 var x
= Math
.round((time
- startTime
) / DEFAULT_SCALE
);
234 var text
= (new Date(time
)).toLocaleTimeString();
235 context
.fillText(text
, x
, textHeight
);
237 context
.lineTo(x
, 0);
238 context
.lineTo(x
, height
);
244 getDataSeriesCount: function() {
246 return this.graph_
.dataSeries_
.length
;
250 hasDataSeries: function(dataSeries
) {
252 return this.graph_
.hasDataSeries(dataSeries
);
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.
263 var Graph
= (function() {
268 this.dataSeries_
= [];
270 // Cached properties of the graph, set in layout.
273 this.fontHeight_
= 0;
277 // At least the highest value in the displayed range of the graph.
278 // Used for scaling and setting labels. Set in layoutLabels.
281 // Cached text of equally spaced labels. Set in layoutLabels.
286 * A Label is the label at a particular position along the y-axis.
289 function Label(height
, text
) {
290 this.height
= height
;
295 addDataSeries: function(dataSeries
) {
296 this.dataSeries_
.push(dataSeries
);
299 hasDataSeries: function(dataSeries
) {
300 for (var i
= 0; i
< this.dataSeries_
.length
; ++i
) {
301 if (this.dataSeries_
[i
] == dataSeries
)
308 * Returns a list of all the values that should be displayed for a given
309 * data series, using the current graph layout.
311 getValues: function(dataSeries
) {
312 if (!dataSeries
.isVisible())
314 return dataSeries
.getValues(this.startTime_
, this.scale_
, this.width_
);
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
322 layout: function(width
, height
, fontHeight
, startTime
, scale
) {
324 this.height_
= height
;
325 this.fontHeight_
= fontHeight
;
326 this.startTime_
= startTime
;
329 // Find largest value.
331 for (var i
= 0; i
< this.dataSeries_
.length
; ++i
) {
332 var values
= this.getValues(this.dataSeries_
[i
]);
335 for (var j
= 0; j
< values
.length
; ++j
) {
341 this.layoutLabels_(max
);
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|.
350 layoutLabels_: function(maxValue
) {
351 if (maxValue
< 1024) {
352 this.layoutLabelsBasic_(maxValue
, 0);
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.
362 while (units
[unit
+ 1] && maxValue
>= 1024) {
368 this.layoutLabelsBasic_(maxValue
, 1);
370 // Append units to labels.
371 for (var i
= 0; i
< this.labels_
.length
; ++i
)
372 this.labels_
[i
] += ' ' + units
[unit
];
374 // Convert |max_| back to unit '1'.
375 this.max_
*= Math
.pow(1024, unit
);
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|.
383 layoutLabelsBasic_: function(maxValue
, maxDecimalDigits
) {
385 // No labels if |maxValue| is 0.
387 this.max_
= maxValue
;
391 // The maximum number of equally spaced labels allowed. |fontHeight_|
392 // is doubled because the top two labels are both drawn in the same
394 var minLabelSpacing
= 2 * this.fontHeight_
+ LABEL_VERTICAL_SPACING
;
396 // The + 1 is for the top label.
397 var maxLabels
= 1 + this.height_
/ minLabelSpacing
;
400 } else if (maxLabels
> MAX_VERTICAL_LABELS
) {
401 maxLabels
= MAX_VERTICAL_LABELS
;
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
;
410 // Pick a reasonable step size.
412 // If we use a step size of |stepSize| between labels, we'll need:
414 // Math.ceil(maxValue / stepSize) + 1
416 // labels. The + 1 is because we need labels at both at 0 and at
417 // the top of the graph.
419 // Check if we can use steps of size |stepSize|.
420 if (Math
.ceil(maxValue
/ stepSize
) + 1 <= maxLabels
)
422 // Check |stepSize| * 2.
423 if (Math
.ceil(maxValue
/ (stepSize
* 2)) + 1 <= maxLabels
) {
427 // Check |stepSize| * 5.
428 if (Math
.ceil(maxValue
/ (stepSize
* 5)) + 1 <= maxLabels
) {
433 if (stepSizeDecimalDigits
> 0)
434 --stepSizeDecimalDigits
;
437 // Set the max so it's an exact multiple of the chosen step size.
438 this.max_
= Math
.ceil(maxValue
/ stepSize
) * stepSize
;
441 for (var label
= this.max_
; label
>= 0; label
-= stepSize
)
442 this.labels_
.push(label
.toFixed(stepSizeDecimalDigits
));
446 * Draws tick marks for each of the labels in |labels_|.
448 drawTicks: function(context
) {
451 x1
= this.width_
- 1;
452 x2
= this.width_
- 1 - Y_AXIS_TICK_LENGTH
;
454 context
.fillStyle
= GRID_COLOR
;
456 for (var i
= 1; i
< this.labels_
.length
- 1; ++i
) {
457 // The rounding is needed to avoid ugly 2-pixel wide anti-aliased
459 var y
= Math
.round(this.height_
* i
/ (this.labels_
.length
- 1));
460 context
.moveTo(x1
, y
);
461 context
.lineTo(x2
, y
);
467 * Draws a graph line for each of the data series.
469 drawLines: function(context
) {
470 // Factor by which to scale all values to convert them to a number from
473 var bottom
= this.height_
- 1;
475 scale
= bottom
/ this.max_
;
477 // Draw in reverse order, so earlier data series are drawn on top of
479 for (var i
= this.dataSeries_
.length
- 1; i
>= 0; --i
) {
480 var values
= this.getValues(this.dataSeries_
[i
]);
483 context
.strokeStyle
= this.dataSeries_
[i
].getColor();
485 for (var x
= 0; x
< values
.length
; ++x
) {
486 // The rounding is needed to avoid ugly 2-pixel wide anti-aliased
488 context
.lineTo(x
, bottom
- Math
.round(values
[x
] * scale
));
495 * Draw labels in |labels_|.
497 drawLabels: function(context
) {
498 if (this.labels_
.length
== 0)
500 var x
= this.width_
- LABEL_HORIZONTAL_SPACING
;
502 // Set up the context.
503 context
.fillStyle
= TEXT_COLOR
;
504 context
.textAlign
= 'right';
506 // Draw top label, which is the only one that appears below its tick
508 context
.textBaseline
= 'top';
509 context
.fillText(this.labels_
[0], x
, 0);
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
);
522 return TimelineGraphView
;