From dc4ef8386c7075fdf3786c0ace599a3dd7a6fdcb Mon Sep 17 00:00:00 2001 From: Kelley Reynolds Date: Thu, 2 Sep 2010 23:23:49 +0800 Subject: [PATCH] Added an optional timer to reset the min/max for spikey graphs --- smoothie.js | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/smoothie.js b/smoothie.js index 9abd609..a182948 100644 --- a/smoothie.js +++ b/smoothie.js @@ -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.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;