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 TimelineDataSeries collects an ordered series of (time, value) pairs,
7 * and converts them to graph points. It also keeps track of its color and
8 * current visibility state.
9 * It keeps MAX_STATS_DATA_POINT_BUFFER_SIZE data points at most. Old data
10 * points will be dropped when it reaches this size.
12 var TimelineDataSeries
= (function() {
18 function TimelineDataSeries() {
19 // List of DataPoints in chronological order.
20 this.dataPoints_
= [];
22 // Default color. Should always be overridden prior to display.
24 // Whether or not the data series should be drawn.
25 this.isVisible_
= true;
27 this.cacheStartTime_
= null;
28 this.cacheStepSize_
= 0;
29 this.cacheValues_
= [];
32 TimelineDataSeries
.prototype = {
37 if (this.dataPoints_
.length
< 1)
41 for (var i
= 0; i
< this.dataPoints_
.length
; ++i
) {
42 values
.push(this.dataPoints_
[i
].value
);
45 startTime
: this.dataPoints_
[0].time
,
46 endTime
: this.dataPoints_
[this.dataPoints_
.length
- 1].time
,
47 values
: JSON
.stringify(values
),
52 * Adds a DataPoint to |this| with the specified time and value.
53 * DataPoints are assumed to be received in chronological order.
55 addPoint: function(timeTicks
, value
) {
56 var time
= new Date(timeTicks
);
57 this.dataPoints_
.push(new DataPoint(time
, value
));
59 if (this.dataPoints_
.length
> MAX_STATS_DATA_POINT_BUFFER_SIZE
)
60 this.dataPoints_
.shift();
63 isVisible: function() {
64 return this.isVisible_
;
67 show: function(isVisible
) {
68 this.isVisible_
= isVisible
;
71 getColor: function() {
75 setColor: function(color
) {
80 * Returns a list containing the values of the data series at |count|
81 * points, starting at |startTime|, and |stepSize| milliseconds apart.
82 * Caches values, so showing/hiding individual data series is fast.
84 getValues: function(startTime
, stepSize
, count
) {
85 // Use cached values, if we can.
86 if (this.cacheStartTime_
== startTime
&&
87 this.cacheStepSize_
== stepSize
&&
88 this.cacheValues_
.length
== count
) {
89 return this.cacheValues_
;
93 this.cacheValues_
= this.getValuesInternal_(startTime
, stepSize
, count
);
94 this.cacheStartTime_
= startTime
;
95 this.cacheStepSize_
= stepSize
;
97 return this.cacheValues_
;
101 * Returns the cached |values| in the specified time period.
103 getValuesInternal_: function(startTime
, stepSize
, count
) {
106 var currentValue
= 0;
107 var time
= startTime
;
108 for (var i
= 0; i
< count
; ++i
) {
109 while (nextPoint
< this.dataPoints_
.length
&&
110 this.dataPoints_
[nextPoint
].time
< time
) {
111 currentValue
= this.dataPoints_
[nextPoint
].value
;
114 values
[i
] = currentValue
;
122 * A single point in a data series. Each point has a time, in the form of
123 * milliseconds since the Unix epoch, and a numeric value.
126 function DataPoint(time
, value
) {
131 return TimelineDataSeries
;