From c36d82ed803509df109eb28285a2892b64781188 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 20 Mar 2021 22:17:43 -0400 Subject: [PATCH] feat: add lookahead for marker #272 --- src/ts/waterfall/sub-components/svg-marks.ts | 19 ++++++++++++++----- tsconfig.json | 1 + 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/ts/waterfall/sub-components/svg-marks.ts b/src/ts/waterfall/sub-components/svg-marks.ts index 39765314..55b7473f 100644 --- a/src/ts/waterfall/sub-components/svg-marks.ts +++ b/src/ts/waterfall/sub-components/svg-marks.ts @@ -21,7 +21,7 @@ export function createMarks(context: Context, marks: Mark[]) { const markHolder = svg.newG("mark-holder type-" + mark.name.toLowerCase().replace(/([0-9]+[ ]?ms)|\W/g, "")); const lineHolder = svg.newG("line-holder"); const lineLabelHolder = svg.newG("line-label-holder"); - const lineLabel = svg.newTextEl(mark.name, { x: x + "%", y: diagramHeight + 25 }); + const lineLabel = svg.newTextEl(mark.name, { x: `${x}%`, y: diagramHeight + 25 }); lineLabel.setAttribute("writing-mode", "tb"); let lineRect: SVGGElement; mark.x = x; @@ -33,11 +33,20 @@ export function createMarks(context: Context, marks: Mark[]) { y2: diagramHeight, }); - const lastMark = marks[i - 1]; + const previousMark = marks[i - 1]; + const nextMark: Mark | undefined = marks[i + 1]; const minDistance = 2.5; // minimum distance between marks - if (lastMark && lastMark.x !== undefined && mark.x - lastMark.x < minDistance) { - lineLabel.setAttribute("x", lastMark.x + minDistance + "%"); - mark.x = lastMark.x + minDistance; + const isCloseToPerviousMark = previousMark?.x !== undefined && mark.x - previousMark.x < minDistance; + const nextX = roundNumber((nextMark?.startTime || 0) / context.unit); + + if (nextX && nextX - mark.x < minDistance && nextX + minDistance >= 100 && !isCloseToPerviousMark) { // look ahead + // push current mark back if next mark would be pushed past 100% and there is no close previous mark + lineLabel.setAttribute("x", `${nextX - minDistance}%`); + mark.x = nextX - minDistance; + } else if (previousMark?.x !== undefined && isCloseToPerviousMark) { // look behind + // push mark ahead to not collide with previous mark + lineLabel.setAttribute("x", `${previousMark.x + minDistance}%`); + mark.x = previousMark.x + minDistance; } // would use polyline but can't use percentage for points const lineConnection = svg.newLine({ diff --git a/tsconfig.json b/tsconfig.json index 30d1d536..d0198cc1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,7 @@ "module": "commonjs", "moduleResolution": "node", "sourceMap": true, + "alwaysStrict": true, "removeComments": false, "noImplicitAny": false, "noImplicitReturns": true,