Skip to content

Commit

Permalink
perf: improve render() performance a bit
Browse files Browse the repository at this point in the history
No behavior changes
  • Loading branch information
WofWca committed Sep 24, 2021
1 parent 85cb245 commit c5519ba
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions smoothie.js
Expand Up @@ -911,17 +911,17 @@

// For each data set...
for (var d = 0; d < this.seriesSet.length; d++) {
var timeSeries = this.seriesSet[d].timeSeries;
var timeSeries = this.seriesSet[d].timeSeries,
dataSet = timeSeries.data;

// Delete old data that's moved off the left of the chart.
timeSeries.dropOldData(oldestValidTime, chartOptions.maxDataSetLength);
if (timeSeries.disabled) {
if (timeSeries.disabled || dataSet.length <= 1) {
continue;
}
context.save();

var dataSet = timeSeries.data,
seriesOptions = this.seriesSet[d].options;
var seriesOptions = this.seriesSet[d].options;

// Set style for this dataSet.
context.lineWidth = seriesOptions.lineWidth;
Expand All @@ -930,7 +930,7 @@
context.beginPath();
// Retain lastX, lastY for calculating the control points of bezier curves.
var firstX = 0, firstY = 0, lastX = 0, lastY = 0;
for (var i = 0; i < dataSet.length && dataSet.length !== 1; i++) {
for (var i = 0; i < dataSet.length; i++) {
var x = timeToXPixel(dataSet[i][0]),
y = valueToYPixel(dataSet[i][1]);

Expand Down Expand Up @@ -978,27 +978,26 @@
lastX = x; lastY = y;
}

if (dataSet.length > 1) {
if (seriesOptions.fillStyle) {
// Close up the fill region.
if (chartOptions.scrollBackwards) {
context.lineTo(lastX, dimensions.height + seriesOptions.lineWidth);
context.lineTo(firstX, dimensions.height + seriesOptions.lineWidth);
context.lineTo(firstX, firstY);
} else {
context.lineTo(dimensions.width + seriesOptions.lineWidth + 1, lastY);
context.lineTo(dimensions.width + seriesOptions.lineWidth + 1, dimensions.height + seriesOptions.lineWidth + 1);
context.lineTo(firstX, dimensions.height + seriesOptions.lineWidth);
}
context.fillStyle = seriesOptions.fillStyle;
context.fill();
if (seriesOptions.fillStyle) {
// Close up the fill region.
if (chartOptions.scrollBackwards) {
context.lineTo(lastX, dimensions.height + seriesOptions.lineWidth);
context.lineTo(firstX, dimensions.height + seriesOptions.lineWidth);
context.lineTo(firstX, firstY);
} else {
context.lineTo(dimensions.width + seriesOptions.lineWidth + 1, lastY);
context.lineTo(dimensions.width + seriesOptions.lineWidth + 1, dimensions.height + seriesOptions.lineWidth + 1);
context.lineTo(firstX, dimensions.height + seriesOptions.lineWidth);
}
context.fillStyle = seriesOptions.fillStyle;
context.fill();
}

if (seriesOptions.strokeStyle && seriesOptions.strokeStyle !== 'none') {
context.stroke();
}
context.closePath();
if (seriesOptions.strokeStyle && seriesOptions.strokeStyle !== 'none') {
context.stroke();
}
context.closePath();

context.restore();
}

Expand Down

0 comments on commit c5519ba

Please sign in to comment.