Skip to content

Commit

Permalink
Merge pull request #92 from annazhelt/useLatestDataTimeAsCurrentTime
Browse files Browse the repository at this point in the history
Add option to use latest data timestamp as current chart time
  • Loading branch information
drewnoakes committed Dec 11, 2017
2 parents 7ae1ab2 + 02793c4 commit cb889d1
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion smoothie.js
Expand Up @@ -291,6 +291,7 @@
* strokeStyle: '#BBBBBB'
* },
* tooltipFormatter: SmoothieChart.tooltipFormatter, // formatter function for tooltip text
* nonRealtimeData: false, // use time of latest data as current time
* responsive: false, // whether the chart should adapt to the size of the canvas
* limitFPS: 0 // maximum frame rate the chart will render at, in FPS (zero means no limit)
* }
Expand Down Expand Up @@ -360,6 +361,7 @@
strokeStyle: '#BBBBBB'
},
tooltipFormatter: SmoothieChart.tooltipFormatter,
nonRealtimeData: false,
responsive: false,
limitFPS: 0
};
Expand Down Expand Up @@ -619,7 +621,24 @@
// Renders a frame, and queues the next frame for later rendering
var animate = function() {
this.frame = SmoothieChart.AnimateCompatibility.requestAnimationFrame(function() {
this.render();
if(this.options.nonRealtimeData){
var dateZero = new Date(0);
// find the data point with the latest timestamp
var maxTimeStamp = this.seriesSet.reduce(function(max, series){
var dataSet = series.timeSeries.data;
if(dataSet && dataSet.length > 0)
{
// timestamp corresponds to element 0 of the data point
var lastDataTimeStamp = dataSet[dataSet.length-1][0];
max = max > lastDataTimeStamp ? max : lastDataTimeStamp;
}
return max;
}, dateZero);
// use the max timestamp as current time
this.render(this.canvas, maxTimeStamp > dateZero ? maxTimeStamp : null);
} else {
this.render();
}
animate();
}.bind(this));
}.bind(this);
Expand Down

0 comments on commit cb889d1

Please sign in to comment.