Skip to content

Commit

Permalink
Added an optional timer to reset the min/max for spikey graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelley Reynolds authored and ngd committed Sep 3, 2010
1 parent 98161dc commit dc4ef83
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions smoothie.js
Expand Up @@ -28,20 +28,35 @@
* v1.1: Auto scaling of axis, by Neil Dunn
* v1.2: fps (frames per second) option, by Mathias Petterson
* v1.3: Fix for divide by zero, by Paul Nikitochkin
* v1.4: Set minimum, top-scale padding, remove timeseries, add optional timer to reset bounds, by Kelley Reynolds
*/

function TimeSeries() {
function TimeSeries(options) {
options = options || {};
options.resetBoundsInterval = options.resetBoundsInterval || 3000; // Reset the max/min bounds after this many milliseconds
options.resetBounds = options.resetBounds || true; // Enable or disable the resetBounds timer
this.options = options;
this.data = [];
/**
* The maximum value ever seen in this time series.
*/
this.max = Number.NaN;
/**
* The minimum value ever seen in this time series.
*/
this.min = Number.NaN;

this.maxValue = Number.NaN; // The maximum value ever seen in this time series.
this.minValue = Number.NaN; // The minimum value ever seen in this time series.

// Start a resetBounds Interval timer desired
if (options.resetBounds) {
this.boundsTimer = setInterval(function(thisObj) { thisObj.resetBounds(); }, options.resetBoundsInterval, this);
}
}

// Reset the min and max for this timeseries so the graph rescales itself
TimeSeries.prototype.resetBounds = function() {
this.maxValue = Number.NaN;

This comment has been minimized.

Copy link
@ngd

ngd Sep 3, 2010

Collaborator

It took me a few seconds to realise this wasn't just calculating what is already calculated because the graph shrinks the time series' data field.

What might be cleaner, rather than having 1 timer per time series and 1 timer in the graph for this cleanup would be to do all this work in the chart object itself. It makes the time series simpler and using less timers is better for performance.

This comment has been minimized.

Copy link
@kreynolds

kreynolds Sep 3, 2010

Contributor

It wasn't immediately clear to me whether or not shrinking every data line in the graph would be desirable behavior in every case so I put the data management inside the TimeSeries. With the new ability to add/remove lines from a graph at will, I can also see cases where series move between graphs and this setting will want to move with them.

It's definitely better to have fewer timers if that's what everyone wants though

this.minValue = Number.NaN;
for (var i = 0; i < this.data.length; i++) {
this.maxValue = !isNaN(this.maxValue) ? Math.max(this.maxValue, this.data[i][1]) : this.data[i][1];
this.minValue = !isNaN(this.minValue) ? Math.min(this.minValue, this.data[i][1]) : this.data[i][1];
}
};

TimeSeries.prototype.append = function(timestamp, value) {
this.data.push([timestamp, value]);
this.maxValue = !isNaN(this.maxValue) ? Math.max(this.maxValue, value) : value;
Expand Down

0 comments on commit dc4ef83

Please sign in to comment.