Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an optional timer to reset the min/max for spikey graphs
- Loading branch information
Showing
1 changed file
with
24 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
kreynolds
Contributor
|
||
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; | ||
|
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.