Skip to content

Commit

Permalink
track min/max Y values for every X pixel and use a single fillRect() …
Browse files Browse the repository at this point in the history
…call (#15)
  • Loading branch information
leeoniya committed Oct 11, 2019
1 parent 5d5345c commit b39bd47
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
27 changes: 22 additions & 5 deletions dist/uPlot.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,12 +565,16 @@ function uPlot(opts) {
}

function drawLine(xdata, ydata, scaleX, scaleY, color, width, dash, fill) {
setCtxStyle(color, width, dash, fill);
setCtxStyle(color, width, dash, fill || color);

var yOk;
var gap = false;

ctx.beginPath();
// ctx.beginPath();

var prevX = 0;
var yMin = Infinity;
var yMax = -Infinity;

for (var i = i0; i <= i1; i++) {
var xPos = getXPos(xdata[i], scaleX, can[WIDTH]);
Expand All @@ -586,12 +590,25 @@ function uPlot(opts) {
ctx.moveTo(xPos, yPos);
gap = false;
}
else
{ ctx.lineTo(xPos, yPos); }
else {
if (xPos == prevX) {
yMin = min(yMin, yPos); // cheaper to use if/else?
yMax = max(yMax, yPos);
}
else {
// let xCrisp = prevX + 0.5;
// ctx.moveTo(xCrisp, yMin + 0.5);
// ctx.lineTo(xCrisp, yMax + 0.5);
ctx.fillRect(prevX, yMin, 1, (yMax - yMin) || 1);
prevX = xPos;
yMin = min(Infinity, yPos); // cheaper to use if/else?
yMax = max(-Infinity, yPos);
}
}
}
}

ctx.stroke();
// ctx.stroke();
}

// dim is logical (getClientBoundingRect) pixels, not canvas pixels
Expand Down
27 changes: 22 additions & 5 deletions dist/uPlot.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,12 +566,16 @@ var uPlot = (function () {
}

function drawLine(xdata, ydata, scaleX, scaleY, color, width, dash, fill) {
setCtxStyle(color, width, dash, fill);
setCtxStyle(color, width, dash, fill || color);

var yOk;
var gap = false;

ctx.beginPath();
// ctx.beginPath();

var prevX = 0;
var yMin = Infinity;
var yMax = -Infinity;

for (var i = i0; i <= i1; i++) {
var xPos = getXPos(xdata[i], scaleX, can[WIDTH]);
Expand All @@ -587,12 +591,25 @@ var uPlot = (function () {
ctx.moveTo(xPos, yPos);
gap = false;
}
else
{ ctx.lineTo(xPos, yPos); }
else {
if (xPos == prevX) {
yMin = min(yMin, yPos); // cheaper to use if/else?
yMax = max(yMax, yPos);
}
else {
// let xCrisp = prevX + 0.5;
// ctx.moveTo(xCrisp, yMin + 0.5);
// ctx.lineTo(xCrisp, yMax + 0.5);
ctx.fillRect(prevX, yMin, 1, (yMax - yMin) || 1);
prevX = xPos;
yMin = min(Infinity, yPos); // cheaper to use if/else?
yMax = max(-Infinity, yPos);
}
}
}
}

ctx.stroke();
// ctx.stroke();
}

// dim is logical (getClientBoundingRect) pixels, not canvas pixels
Expand Down
2 changes: 1 addition & 1 deletion dist/uPlot.iife.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 22 additions & 5 deletions src/uPlot.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,16 @@ export default function uPlot(opts) {
}

function drawLine(xdata, ydata, scaleX, scaleY, color, width, dash, fill) {
setCtxStyle(color, width, dash, fill);
setCtxStyle(color, width, dash, fill || color);

let yOk;
let gap = false;

ctx.beginPath();
// ctx.beginPath();

let prevX = 0;
let yMin = Infinity;
let yMax = -Infinity;

for (let i = i0; i <= i1; i++) {
let xPos = getXPos(xdata[i], scaleX, can[WIDTH]);
Expand All @@ -283,12 +287,25 @@ export default function uPlot(opts) {
ctx.moveTo(xPos, yPos);
gap = false;
}
else
ctx.lineTo(xPos, yPos);
else {
if (xPos == prevX) {
yMin = min(yMin, yPos); // cheaper to use if/else?
yMax = max(yMax, yPos);
}
else {
// let xCrisp = prevX + 0.5;
// ctx.moveTo(xCrisp, yMin + 0.5);
// ctx.lineTo(xCrisp, yMax + 0.5);
ctx.fillRect(prevX, yMin, 1, (yMax - yMin) || 1)
prevX = xPos;
yMin = min(Infinity, yPos); // cheaper to use if/else?
yMax = max(-Infinity, yPos);
}
}
}
}

ctx.stroke();
// ctx.stroke();
}

// dim is logical (getClientBoundingRect) pixels, not canvas pixels
Expand Down

0 comments on commit b39bd47

Please sign in to comment.