Skip to content

Commit

Permalink
ensure leading and trailing data gaps are still clipped when spanGaps…
Browse files Browse the repository at this point in the history
…: true. close #253.

this also removes the awkward spanGaps fn signature, since it required filtering canvas pixel ranges and not scale or data ranges.
  • Loading branch information
leeoniya committed Jun 4, 2020
1 parent fa0074b commit 5b98555
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 47 deletions.
24 changes: 14 additions & 10 deletions dist/uPlot.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ function fnOrSelf(v) {
return typeof v == "function" ? v : function () { return v; };
}

function retArg2(a, b) {
return b;
}

function incrRoundUp(num, incr) {
return ceil(num/incr)*incr;
}
Expand Down Expand Up @@ -800,6 +796,7 @@ var ySeriesOpts = {
scale: "y",
show: true,
band: false,
spanGaps: false,
alpha: 1,
points: {
show: seriesPoints,
Expand Down Expand Up @@ -1090,8 +1087,6 @@ function uPlot(opts, data, then) {

sc.range = fnOrSelf(sc.range || (isTime ? snapTimeX : i == 0 ? snapNumX : snapNumY));

s.spanGaps = s.spanGaps === true ? retArg2 : fnOrSelf(s.spanGaps || []);

var sv = s.value;
s.value = isTime ? (isStr(sv) ? timeSeriesVal(_tzDate, timeSeriesStamp(sv, _fmtDate)) : sv || _timeSeriesVal) : sv || numSeriesVal;
s.label = s.label || (isTime ? timeSeriesLabel : numSeriesLabel);
Expand Down Expand Up @@ -1663,10 +1658,19 @@ function uPlot(opts, data, then) {
{ dir *= -1; }
}

function buildClip(is, gaps) {
function buildClip(is, gaps, nullHead, nullTail) {
var s = series[is];
var toSpan = new Set(s.spanGaps(self, gaps, is));
gaps = gaps.filter(function (g) { return !toSpan.has(g); });

if (s.spanGaps) {
var headGap = gaps[0];
var tailGap = gaps[gaps.length - 1];
gaps = [];

if (nullHead)
{ gaps.push(headGap); }
if (nullTail)
{ gaps.push(tailGap); }
}

var clip = null;

Expand Down Expand Up @@ -1790,7 +1794,7 @@ function uPlot(opts, data, then) {
}

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

if (s.fill != null) {
var fill = _paths.fill = new Path2D(stroke);
Expand Down
7 changes: 2 additions & 5 deletions dist/uPlot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,6 @@ declare namespace uPlot {
max?: number,
}

/** a min,max tuple of canvas pixel offsets */
export type DataGap = [number, number];

export interface Series {
/** series on/off. when off, it will not affect its scale */
show?: boolean;
Expand All @@ -302,8 +299,8 @@ declare namespace uPlot {
/** scale key */
scale?: string;

/** when true, null data values will not cause line breaks. when fn, should filter and return gaps to span */
spanGaps?: boolean | ((self: uPlot, foundGaps: Array<DataGap>, seriesIdx: number) => Array<DataGap>);
/** when true, null data values will not cause line breaks */
spanGaps?: boolean;

/** legend label */
label?: string;
Expand Down
24 changes: 14 additions & 10 deletions dist/uPlot.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ function fnOrSelf(v) {
return typeof v == "function" ? v : () => v;
}

function retArg2(a, b) {
return b;
}

function incrRoundUp(num, incr) {
return ceil(num/incr)*incr;
}
Expand Down Expand Up @@ -803,6 +799,7 @@ const ySeriesOpts = {
scale: "y",
show: true,
band: false,
spanGaps: false,
alpha: 1,
points: {
show: seriesPoints,
Expand Down Expand Up @@ -1093,8 +1090,6 @@ function uPlot(opts, data, then) {

sc.range = fnOrSelf(sc.range || (isTime ? snapTimeX : i == 0 ? snapNumX : snapNumY));

s.spanGaps = s.spanGaps === true ? retArg2 : fnOrSelf(s.spanGaps || []);

let sv = s.value;
s.value = isTime ? (isStr(sv) ? timeSeriesVal(_tzDate, timeSeriesStamp(sv, _fmtDate)) : sv || _timeSeriesVal) : sv || numSeriesVal;
s.label = s.label || (isTime ? timeSeriesLabel : numSeriesLabel);
Expand Down Expand Up @@ -1659,10 +1654,19 @@ function uPlot(opts, data, then) {
dir *= -1;
}

function buildClip(is, gaps) {
function buildClip(is, gaps, nullHead, nullTail) {
let s = series[is];
let toSpan = new Set(s.spanGaps(self, gaps, is));
gaps = gaps.filter(g => !toSpan.has(g));

if (s.spanGaps) {
let headGap = gaps[0];
let tailGap = gaps[gaps.length - 1];
gaps = [];

if (nullHead)
gaps.push(headGap);
if (nullTail)
gaps.push(tailGap);
}

let clip = null;

Expand Down Expand Up @@ -1786,7 +1790,7 @@ function uPlot(opts, data, then) {
}

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

if (s.fill != null) {
let fill = _paths.fill = new Path2D(stroke);
Expand Down
24 changes: 14 additions & 10 deletions dist/uPlot.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ var uPlot = (function () {
return typeof v == "function" ? v : function () { return v; };
}

function retArg2(a, b) {
return b;
}

function incrRoundUp(num, incr) {
return ceil(num/incr)*incr;
}
Expand Down Expand Up @@ -801,6 +797,7 @@ var uPlot = (function () {
scale: "y",
show: true,
band: false,
spanGaps: false,
alpha: 1,
points: {
show: seriesPoints,
Expand Down Expand Up @@ -1091,8 +1088,6 @@ var uPlot = (function () {

sc.range = fnOrSelf(sc.range || (isTime ? snapTimeX : i == 0 ? snapNumX : snapNumY));

s.spanGaps = s.spanGaps === true ? retArg2 : fnOrSelf(s.spanGaps || []);

var sv = s.value;
s.value = isTime ? (isStr(sv) ? timeSeriesVal(_tzDate, timeSeriesStamp(sv, _fmtDate)) : sv || _timeSeriesVal) : sv || numSeriesVal;
s.label = s.label || (isTime ? timeSeriesLabel : numSeriesLabel);
Expand Down Expand Up @@ -1664,10 +1659,19 @@ var uPlot = (function () {
{ dir *= -1; }
}

function buildClip(is, gaps) {
function buildClip(is, gaps, nullHead, nullTail) {
var s = series[is];
var toSpan = new Set(s.spanGaps(self, gaps, is));
gaps = gaps.filter(function (g) { return !toSpan.has(g); });

if (s.spanGaps) {
var headGap = gaps[0];
var tailGap = gaps[gaps.length - 1];
gaps = [];

if (nullHead)
{ gaps.push(headGap); }
if (nullTail)
{ gaps.push(tailGap); }
}

var clip = null;

Expand Down Expand Up @@ -1791,7 +1795,7 @@ var uPlot = (function () {
}

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

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.

1 change: 1 addition & 0 deletions src/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ export const ySeriesOpts = {
scale: "y",
show: true,
band: false,
spanGaps: false,
alpha: 1,
points: {
show: seriesPoints,
Expand Down
20 changes: 13 additions & 7 deletions src/uPlot.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
isArr,
isStr,
fnOrSelf,
retArg2,
} from './utils';

import {
Expand Down Expand Up @@ -345,8 +344,6 @@ export default function uPlot(opts, data, then) {

sc.range = fnOrSelf(sc.range || (isTime ? snapTimeX : i == 0 ? snapNumX : snapNumY));

s.spanGaps = s.spanGaps === true ? retArg2 : fnOrSelf(s.spanGaps || []);

let sv = s.value;
s.value = isTime ? (isStr(sv) ? timeSeriesVal(_tzDate, timeSeriesStamp(sv, _fmtDate)) : sv || _timeSeriesVal) : sv || numSeriesVal;
s.label = s.label || (isTime ? timeSeriesLabel : numSeriesLabel);
Expand Down Expand Up @@ -912,10 +909,19 @@ export default function uPlot(opts, data, then) {
dir *= -1;
}

function buildClip(is, gaps) {
function buildClip(is, gaps, nullHead, nullTail) {
let s = series[is];
let toSpan = new Set(s.spanGaps(self, gaps, is));
gaps = gaps.filter(g => !toSpan.has(g));

if (s.spanGaps) {
let headGap = gaps[0];
let tailGap = gaps[gaps.length - 1];
gaps = [];

if (nullHead)
gaps.push(headGap);
if (nullTail)
gaps.push(tailGap);
}

let clip = null;

Expand Down Expand Up @@ -1039,7 +1045,7 @@ export default function uPlot(opts, data, then) {
}

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

if (s.fill != null) {
let fill = _paths.fill = new Path2D(stroke);
Expand Down
4 changes: 0 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ export function fnOrSelf(v) {
return typeof v == "function" ? v : () => v;
}

export function retArg2(a, b) {
return b;
}

export function incrRoundUp(num, incr) {
return ceil(num/incr)*incr;
}
Expand Down

0 comments on commit 5b98555

Please sign in to comment.