@@ -21,7 +21,7 @@ export function createMarks(context: Context, marks: Mark[]) {
21
21
const markHolder = svg . newG ( "mark-holder type-" + mark . name . toLowerCase ( ) . replace ( / ( [ 0 - 9 ] + [ ] ? m s ) | \W / g, "" ) ) ;
22
22
const lineHolder = svg . newG ( "line-holder" ) ;
23
23
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 } ) ;
25
25
lineLabel . setAttribute ( "writing-mode" , "tb" ) ;
26
26
let lineRect : SVGGElement ;
27
27
mark . x = x ;
@@ -33,11 +33,20 @@ export function createMarks(context: Context, marks: Mark[]) {
33
33
y2 : diagramHeight ,
34
34
} ) ;
35
35
36
- const lastMark = marks [ i - 1 ] ;
36
+ const previousMark = marks [ i - 1 ] ;
37
+ const nextMark : Mark | undefined = marks [ i + 1 ] ;
37
38
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 ;
41
50
}
42
51
// would use polyline but can't use percentage for points
43
52
const lineConnection = svg . newLine ( {
0 commit comments