Skip to content

Commit

Permalink
pass extendGap and buildClip helpers to pathBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
leeoniya committed Dec 4, 2020
1 parent dc8b5f9 commit 83c9f10
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 46 deletions.
21 changes: 10 additions & 11 deletions dist/uPlot.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2043,7 +2043,7 @@ function uPlot(opts, data, then) {
series.forEach(function (s, i) {
if (i > 0 && s.show && s._paths == null) {
var _idxs = getOuterIdxs(data[i]);
s._paths = s.paths(self, i, _idxs[0], _idxs[1]);
s._paths = s.paths(self, i, _idxs[0], _idxs[1], extendGap, buildClip);
}
});

Expand Down Expand Up @@ -2123,13 +2123,11 @@ function uPlot(opts, data, then) {
{ dir *= -1; }
}

function buildClip(is, gaps) {
var s = series[is];

function buildClip(gaps) {
var clip = null;

// create clip path (invert gaps and non-gaps)
if (gaps.length > 0 && !s.spanGaps) {
if (gaps.length > 0) {
clip = new Path2D();

var prevGapEnd = plotLft;
Expand All @@ -2148,7 +2146,7 @@ function uPlot(opts, data, then) {
return clip;
}

function addGap(gaps, fromX, toX) {
function extendGap(gaps, fromX, toX) {
if (toX > fromX) {
var prevGap = gaps[gaps.length - 1];

Expand All @@ -2168,7 +2166,7 @@ function uPlot(opts, data, then) {
return -1;
}

function buildPaths(self, is, _i0, _i1) {
function buildPaths(self, is, _i0, _i1, extendGap, buildClip) {
var s = series[is];
var isGap = s.isGap;

Expand Down Expand Up @@ -2198,7 +2196,7 @@ function uPlot(opts, data, then) {
var rgtX = incrRound(getXPos(xdata[rgtIdx], scaleX, plotWid, plotLft), 0.5);

if (lftX > plotLft)
{ addGap(gaps, plotLft, lftX); }
{ extendGap(gaps, plotLft, lftX); }

// the moves the shape edge outside the canvas so stroke doesnt bleed in
if (s.band && dir == 1)
Expand Down Expand Up @@ -2247,14 +2245,14 @@ function uPlot(opts, data, then) {
{ accGaps = true; }
}

_addGap && addGap(gaps, outX, x);
_addGap && extendGap(gaps, outX, x);

accX = x;
}
}

if (rgtX < plotLft + plotWid)
{ addGap(gaps, rgtX, plotLft + plotWid); }
{ extendGap(gaps, rgtX, plotLft + plotWid); }

if (s.band) {
var _x, _iy, ydata2;
Expand All @@ -2276,7 +2274,8 @@ function uPlot(opts, data, then) {
}

if (dir == 1) {
_paths.clip = buildClip(is, gaps, ydata[_i0] == null, ydata[_i1] == null);
if (!s.spanGaps)
{ _paths.clip = buildClip(gaps); }

if (s.fill != null) {
var fill = _paths.fill = new Path2D(stroke);
Expand Down
8 changes: 7 additions & 1 deletion dist/uPlot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,13 @@ export namespace Series {
export type Show = boolean | ((self: uPlot, seriesIdx: number, idx0: number, idx1: number) => boolean | undefined);
}

export type PathBuilder = (self: uPlot, seriesIdx: number, idx0: number, idx1: number) => Paths | null;
export type Gaps = [from: number, to: number][];

export type ExtendGap = (gaps: Gaps, fromX: number, toX: number) => void;

export type BuildClip = (gaps: Gaps) => Path2D | null;

export type PathBuilder = (self: uPlot, seriesIdx: number, idx0: number, idx1: number, extendGap: ExtendGap, buildClip: BuildClip) => Paths | null;

export type MinMaxIdxs = [minIdx: number, maxIdx: number];

Expand Down
21 changes: 10 additions & 11 deletions dist/uPlot.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,7 @@ function uPlot(opts, data, then) {
series.forEach((s, i) => {
if (i > 0 && s.show && s._paths == null) {
let _idxs = getOuterIdxs(data[i]);
s._paths = s.paths(self, i, _idxs[0], _idxs[1]);
s._paths = s.paths(self, i, _idxs[0], _idxs[1], extendGap, buildClip);
}
});

Expand Down Expand Up @@ -2112,13 +2112,11 @@ function uPlot(opts, data, then) {
dir *= -1;
}

function buildClip(is, gaps) {
let s = series[is];

function buildClip(gaps) {
let clip = null;

// create clip path (invert gaps and non-gaps)
if (gaps.length > 0 && !s.spanGaps) {
if (gaps.length > 0) {
clip = new Path2D();

let prevGapEnd = plotLft;
Expand All @@ -2137,7 +2135,7 @@ function uPlot(opts, data, then) {
return clip;
}

function addGap(gaps, fromX, toX) {
function extendGap(gaps, fromX, toX) {
if (toX > fromX) {
let prevGap = gaps[gaps.length - 1];

Expand All @@ -2157,7 +2155,7 @@ function uPlot(opts, data, then) {
return -1;
}

function buildPaths(self, is, _i0, _i1) {
function buildPaths(self, is, _i0, _i1, extendGap, buildClip) {
const s = series[is];
const isGap = s.isGap;

Expand Down Expand Up @@ -2187,7 +2185,7 @@ function uPlot(opts, data, then) {
let rgtX = incrRound(getXPos(xdata[rgtIdx], scaleX, plotWid, plotLft), 0.5);

if (lftX > plotLft)
addGap(gaps, plotLft, lftX);
extendGap(gaps, plotLft, lftX);

// the moves the shape edge outside the canvas so stroke doesnt bleed in
if (s.band && dir == 1)
Expand Down Expand Up @@ -2236,14 +2234,14 @@ function uPlot(opts, data, then) {
accGaps = true;
}

_addGap && addGap(gaps, outX, x);
_addGap && extendGap(gaps, outX, x);

accX = x;
}
}

if (rgtX < plotLft + plotWid)
addGap(gaps, rgtX, plotLft + plotWid);
extendGap(gaps, rgtX, plotLft + plotWid);

if (s.band) {
let _x, _iy, ydata2;
Expand All @@ -2265,7 +2263,8 @@ function uPlot(opts, data, then) {
}

if (dir == 1) {
_paths.clip = buildClip(is, gaps, ydata[_i0] == null, ydata[_i1] == null);
if (!s.spanGaps)
_paths.clip = buildClip(gaps);

if (s.fill != null) {
let fill = _paths.fill = new Path2D(stroke);
Expand Down
21 changes: 10 additions & 11 deletions dist/uPlot.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,7 @@ var uPlot = (function () {
series.forEach(function (s, i) {
if (i > 0 && s.show && s._paths == null) {
var _idxs = getOuterIdxs(data[i]);
s._paths = s.paths(self, i, _idxs[0], _idxs[1]);
s._paths = s.paths(self, i, _idxs[0], _idxs[1], extendGap, buildClip);
}
});

Expand Down Expand Up @@ -2124,13 +2124,11 @@ var uPlot = (function () {
{ dir *= -1; }
}

function buildClip(is, gaps) {
var s = series[is];

function buildClip(gaps) {
var clip = null;

// create clip path (invert gaps and non-gaps)
if (gaps.length > 0 && !s.spanGaps) {
if (gaps.length > 0) {
clip = new Path2D();

var prevGapEnd = plotLft;
Expand All @@ -2149,7 +2147,7 @@ var uPlot = (function () {
return clip;
}

function addGap(gaps, fromX, toX) {
function extendGap(gaps, fromX, toX) {
if (toX > fromX) {
var prevGap = gaps[gaps.length - 1];

Expand All @@ -2169,7 +2167,7 @@ var uPlot = (function () {
return -1;
}

function buildPaths(self, is, _i0, _i1) {
function buildPaths(self, is, _i0, _i1, extendGap, buildClip) {
var s = series[is];
var isGap = s.isGap;

Expand Down Expand Up @@ -2199,7 +2197,7 @@ var uPlot = (function () {
var rgtX = incrRound(getXPos(xdata[rgtIdx], scaleX, plotWid, plotLft), 0.5);

if (lftX > plotLft)
{ addGap(gaps, plotLft, lftX); }
{ extendGap(gaps, plotLft, lftX); }

// the moves the shape edge outside the canvas so stroke doesnt bleed in
if (s.band && dir == 1)
Expand Down Expand Up @@ -2248,14 +2246,14 @@ var uPlot = (function () {
{ accGaps = true; }
}

_addGap && addGap(gaps, outX, x);
_addGap && extendGap(gaps, outX, x);

accX = x;
}
}

if (rgtX < plotLft + plotWid)
{ addGap(gaps, rgtX, plotLft + plotWid); }
{ extendGap(gaps, rgtX, plotLft + plotWid); }

if (s.band) {
var _x, _iy, ydata2;
Expand All @@ -2277,7 +2275,8 @@ var uPlot = (function () {
}

if (dir == 1) {
_paths.clip = buildClip(is, gaps, ydata[_i0] == null, ydata[_i1] == null);
if (!s.spanGaps)
{ _paths.clip = buildClip(gaps); }

if (s.fill != null) {
var fill = _paths.fill = new Path2D(stroke);
Expand Down
2 changes: 1 addition & 1 deletion dist/uPlot.iife.min.js

Large diffs are not rendered by default.

21 changes: 10 additions & 11 deletions src/uPlot.js
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ export default function uPlot(opts, data, then) {
series.forEach((s, i) => {
if (i > 0 && s.show && s._paths == null) {
let _idxs = getOuterIdxs(data[i]);
s._paths = s.paths(self, i, _idxs[0], _idxs[1]);
s._paths = s.paths(self, i, _idxs[0], _idxs[1], extendGap, buildClip);
}
});

Expand Down Expand Up @@ -1117,13 +1117,11 @@ export default function uPlot(opts, data, then) {
dir *= -1;
}

function buildClip(is, gaps) {
let s = series[is];

function buildClip(gaps) {
let clip = null;

// create clip path (invert gaps and non-gaps)
if (gaps.length > 0 && !s.spanGaps) {
if (gaps.length > 0) {
clip = new Path2D();

let prevGapEnd = plotLft;
Expand All @@ -1142,7 +1140,7 @@ export default function uPlot(opts, data, then) {
return clip;
}

function addGap(gaps, fromX, toX) {
function extendGap(gaps, fromX, toX) {
if (toX > fromX) {
let prevGap = gaps[gaps.length - 1];

Expand All @@ -1162,7 +1160,7 @@ export default function uPlot(opts, data, then) {
return -1;
}

function buildPaths(self, is, _i0, _i1) {
function buildPaths(self, is, _i0, _i1, extendGap, buildClip) {
const s = series[is];
const isGap = s.isGap;

Expand Down Expand Up @@ -1192,7 +1190,7 @@ export default function uPlot(opts, data, then) {
let rgtX = incrRound(getXPos(xdata[rgtIdx], scaleX, plotWid, plotLft), 0.5);

if (lftX > plotLft)
addGap(gaps, plotLft, lftX);
extendGap(gaps, plotLft, lftX);

// the moves the shape edge outside the canvas so stroke doesnt bleed in
if (s.band && dir == 1)
Expand Down Expand Up @@ -1241,14 +1239,14 @@ export default function uPlot(opts, data, then) {
accGaps = true;
}

_addGap && addGap(gaps, outX, x);
_addGap && extendGap(gaps, outX, x);

accX = x;
}
}

if (rgtX < plotLft + plotWid)
addGap(gaps, rgtX, plotLft + plotWid);
extendGap(gaps, rgtX, plotLft + plotWid);

if (s.band) {
let _x, _iy, ydata2;
Expand All @@ -1270,7 +1268,8 @@ export default function uPlot(opts, data, then) {
}

if (dir == 1) {
_paths.clip = buildClip(is, gaps, ydata[_i0] == null, ydata[_i1] == null);
if (!s.spanGaps)
_paths.clip = buildClip(gaps);

if (s.fill != null) {
let fill = _paths.fill = new Path2D(stroke);
Expand Down

0 comments on commit 83c9f10

Please sign in to comment.