Skip to content

Commit

Permalink
Stop refreshing frames if all scales are paused
Browse files Browse the repository at this point in the history
  • Loading branch information
nagix committed Oct 2, 2018
1 parent bc7edde commit d76c0d6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 46 deletions.
55 changes: 37 additions & 18 deletions src/plugins/plugin.streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,30 @@ export default function(Chart) {

function startFrameRefreshTimer(chart) {
var streaming = chart.streaming;
var lastDrawn = 0;
var frameRefresh = function() {
var frameRate = chart.options.plugins.streaming.frameRate;
var frameDuration = 1000 / (Math.max(frameRate, 0) || 30);
var now = Date.now();

if (lastDrawn + frameDuration <= now) {
// Draw only when animation is inactive
if (!chart.animating && !chart.tooltip._start) {
chart.draw();
}
generateMouseMoveEvent(chart);
lastDrawn += frameDuration;

if (!streaming.frameRequestID) {
var lastDrawn = 0;
var frameRefresh = function() {
var frameRate = chart.options.plugins.streaming.frameRate;
var frameDuration = 1000 / (Math.max(frameRate, 0) || 30);
var now = Date.now();

if (lastDrawn + frameDuration <= now) {
lastDrawn = now;
// Draw only when animation is inactive
if (!chart.animating && !chart.tooltip._start) {
chart.draw();
}
generateMouseMoveEvent(chart);
lastDrawn += frameDuration;
if (lastDrawn + frameDuration <= now) {
lastDrawn = now;
}
}
}
streaming.frameRequestID = helpers.requestAnimFrame.call(window, frameRefresh);
};
streaming.frameRequestID = helpers.requestAnimFrame.call(window, frameRefresh);
};

streaming.frameRequestID = helpers.requestAnimFrame.call(window, frameRefresh);
streaming.frameRequestID = helpers.requestAnimFrame.call(window, frameRefresh);
}
}

function stopFrameRefreshTimer(chart) {
Expand Down Expand Up @@ -160,6 +163,22 @@ export default function(Chart) {
return true;
},

afterUpdate: function(chart, options) {
var pause = true;

// if all scales are paused, stop refreshing frames
helpers.each(chart.scales, function(scale) {
if (scale instanceof realTimeScale) {
pause &= helpers.valueOrDefault(scale.options.realtime.pause, options.pause);
}
});
if (pause) {
stopFrameRefreshTimer(chart);
} else {
startFrameRefreshTimer(chart);
}
},

beforeDatasetDraw: function(chart, args) {
var meta = args.meta;
var chartArea = chart.chartArea;
Expand Down
10 changes: 4 additions & 6 deletions src/plugins/plugin.zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ export default function(Chart) {
}

function zoomRealTimeScale(scale, zoom, center, zoomOptions) {
var options = scale.options;
var realtimeOpts = options.realtime = options.realtime;
var streamingOpts = scale.chart.options.plugins.streaming || {};
var realtimeOpts = scale.options.realtime;
var streamingOpts = scale.chart.options.plugins.streaming;
var duration = helpers.valueOrDefault(realtimeOpts.duration, streamingOpts.duration);
var delay = helpers.valueOrDefault(realtimeOpts.delay, streamingOpts.delay);
var newDuration = duration * (2 - zoom);
Expand All @@ -59,9 +58,8 @@ export default function(Chart) {
}

function panRealTimeScale(scale, delta, panOptions) {
var options = scale.options;
var realtimeOpts = options.realtime = options.realtime;
var streamingOpts = scale.chart.options.plugins.streaming || {};
var realtimeOpts = scale.options.realtime;
var streamingOpts = scale.chart.options.plugins.streaming;
var delay = helpers.valueOrDefault(realtimeOpts.delay, streamingOpts.delay);
var newDelay = delay + (scale.getValueForPixel(delta) - scale.getValueForPixel(0));

Expand Down
38 changes: 16 additions & 22 deletions src/scales/scale.realtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export default function(Chart, moment) {

function resolveOption(scale, key) {
var realtimeOpts = scale.options.realtime;
var streamingOpts = scale.chart.options.plugins.streaming || {};
var streamingOpts = scale.chart.options.plugins.streaming;
return helpers.valueOrDefault(realtimeOpts[key], streamingOpts[key]);
}

Expand Down Expand Up @@ -505,13 +505,16 @@ export default function(Chart, moment) {

function startFrameRefreshTimer(scale) {
var realtime = scale.realtime;
var frameRefresh = function() {
scroll(scale);
realtime.frameRequestID = helpers.requestAnimFrame.call(window, frameRefresh);
};

realtime.head = Date.now();
realtime.frameRequestID = helpers.requestAnimFrame.call(window, frameRefresh);
if (!realtime.frameRequestID) {
var frameRefresh = function() {
scroll(scale);
realtime.frameRequestID = helpers.requestAnimFrame.call(window, frameRefresh);
};

realtime.head = Date.now();
realtime.frameRequestID = helpers.requestAnimFrame.call(window, frameRefresh);
}
}

function stopFrameRefreshTimer(scale) {
Expand All @@ -537,12 +540,11 @@ export default function(Chart, moment) {
var RealTimeScale = TimeScale.extend({
initialize: function() {
var me = this;
var chart = me.chart;

TimeScale.prototype.initialize.apply(me, arguments);

// For backwards compatibility
if (me.options.type === 'time' && !chart.options.plugins.streaming) {
if (me.options.type === 'time' && !me.chart.options.plugins.streaming) {
return;
}

Expand All @@ -554,23 +556,16 @@ export default function(Chart, moment) {

update: function() {
var me = this;
var options = me.options;
var chart = me.chart;

// For backwards compatibility
if (options.type === 'time' && !chart.options.plugins.streaming) {
if (me.options.type === 'time' && !me.chart.options.plugins.streaming) {
return TimeScale.prototype.update.apply(me, arguments);
}

var frameRequestID = me.realtime.frameRequestID;
var pause = resolveOption(me, 'pause');

if (!frameRequestID && !pause) {
startFrameRefreshTimer(me);
} else if (frameRequestID && pause) {
if (resolveOption(me, 'pause')) {
stopFrameRefreshTimer(me);
}
if (!pause) {
} else {
startFrameRefreshTimer(me);
me.realtime.head = Date.now();
}

Expand All @@ -580,10 +575,9 @@ export default function(Chart, moment) {
buildTicks: function() {
var me = this;
var options = me.options;
var chart = me.chart;

// For backwards compatibility
if (options.type === 'time' && !chart.options.plugins.streaming) {
if (options.type === 'time' && !me.chart.options.plugins.streaming) {
return TimeScale.prototype.buildTicks.apply(me, arguments);
}

Expand Down

0 comments on commit d76c0d6

Please sign in to comment.