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 34a3ddc
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions smoothie.js
Expand Up @@ -915,7 +915,7 @@

// 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();
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 34a3ddc

Please sign in to comment.