From 19be3878c33fbde46ba453c2ed81645f5f4d872c Mon Sep 17 00:00:00 2001 From: idfr-eq Date: Mon, 18 Mar 2024 13:23:28 +0100 Subject: [PATCH] :sparkles: Added option to paint dots on canvas for isolated points --- src/plots/interfaces.ts | 4 ++++ src/plots/line-plot.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/plots/interfaces.ts b/src/plots/interfaces.ts index d7d4cf6..e8d4afb 100644 --- a/src/plots/interfaces.ts +++ b/src/plots/interfaces.ts @@ -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, } /** diff --git a/src/plots/line-plot.ts b/src/plots/line-plot.ts index 72a1d5e..5db8b76 100644 --- a/src/plots/line-plot.ts +++ b/src/plots/line-plot.ts @@ -51,6 +51,32 @@ export default class LinePlot extends Plot { 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(); }