Skip to content

Commit

Permalink
Merge pull request #241 from idfr-eq/fix/show_isolated_points
Browse files Browse the repository at this point in the history
✨ Added option to paint dots on LinePlot for isolated points
  • Loading branch information
tomktjemsland authored Mar 20, 2024
2 parents 146a3d4 + 19be387 commit 5db0547
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/plots/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ export interface LinePlotOptions extends PlotOptions {
* @example [2, 3] // Draw 2, skip 3 pixels
*/
dashWrapped?: number[],
/**
* If enabled, points surrounded by non-defined values will be displayed as a dot
*/
showIsolatedPoints?: boolean,
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/plots/line-plot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ export default class LinePlot extends Plot<LinePlotOptions> {
this.plotWrapped(ctx, lineFunction);
}

if (options.showIsolatedPoints) {
const arcL = Math.PI * 2;

ctx.fillStyle = options.color;

plotdata
.filter((t, i) => {
if (!options.defined(t[1], t[0])) return false;

const prev = plotdata[i - 1]?.[1];
const next = plotdata[i + 1]?.[1];

if (i === 0) return !options.defined(next);
if (i === plotdata.length - 1) return !options.defined(prev);
return !options.defined(prev) && !options.defined(next);
})
.forEach(d => {
ctx.beginPath();

if (options.horizontal) ctx.arc(scale(d[0]), xscale(d[1]), 1, 0, arcL);
else ctx.arc(xscale(d[1]), scale(d[0]), 1, 0, arcL);

ctx.fill();
});
}

ctx.restore();
}

Expand Down

0 comments on commit 5db0547

Please sign in to comment.