Skip to content

Commit c36d82e

Browse files
committed
feat: add lookahead for marker
#272
1 parent e390311 commit c36d82e

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Diff for: src/ts/waterfall/sub-components/svg-marks.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function createMarks(context: Context, marks: Mark[]) {
2121
const markHolder = svg.newG("mark-holder type-" + mark.name.toLowerCase().replace(/([0-9]+[ ]?ms)|\W/g, ""));
2222
const lineHolder = svg.newG("line-holder");
2323
const lineLabelHolder = svg.newG("line-label-holder");
24-
const lineLabel = svg.newTextEl(mark.name, { x: x + "%", y: diagramHeight + 25 });
24+
const lineLabel = svg.newTextEl(mark.name, { x: `${x}%`, y: diagramHeight + 25 });
2525
lineLabel.setAttribute("writing-mode", "tb");
2626
let lineRect: SVGGElement;
2727
mark.x = x;
@@ -33,11 +33,20 @@ export function createMarks(context: Context, marks: Mark[]) {
3333
y2: diagramHeight,
3434
});
3535

36-
const lastMark = marks[i - 1];
36+
const previousMark = marks[i - 1];
37+
const nextMark: Mark | undefined = marks[i + 1];
3738
const minDistance = 2.5; // minimum distance between marks
38-
if (lastMark && lastMark.x !== undefined && mark.x - lastMark.x < minDistance) {
39-
lineLabel.setAttribute("x", lastMark.x + minDistance + "%");
40-
mark.x = lastMark.x + minDistance;
39+
const isCloseToPerviousMark = previousMark?.x !== undefined && mark.x - previousMark.x < minDistance;
40+
const nextX = roundNumber((nextMark?.startTime || 0) / context.unit);
41+
42+
if (nextX && nextX - mark.x < minDistance && nextX + minDistance >= 100 && !isCloseToPerviousMark) { // look ahead
43+
// push current mark back if next mark would be pushed past 100% and there is no close previous mark
44+
lineLabel.setAttribute("x", `${nextX - minDistance}%`);
45+
mark.x = nextX - minDistance;
46+
} else if (previousMark?.x !== undefined && isCloseToPerviousMark) { // look behind
47+
// push mark ahead to not collide with previous mark
48+
lineLabel.setAttribute("x", `${previousMark.x + minDistance}%`);
49+
mark.x = previousMark.x + minDistance;
4150
}
4251
// would use polyline but can't use percentage for points
4352
const lineConnection = svg.newLine({

Diff for: tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"module": "commonjs",
55
"moduleResolution": "node",
66
"sourceMap": true,
7+
"alwaysStrict": true,
78
"removeComments": false,
89
"noImplicitAny": false,
910
"noImplicitReturns": true,

0 commit comments

Comments
 (0)