Skip to content

Commit c3e8789

Browse files
committed
fix comment
1 parent 0d8cd43 commit c3e8789

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

front/ui/ui-charts/src/spaceTimeChart/components/TrainInfoBar.tsx

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,23 @@ import { useIntervalPositions } from './useIntervalPositions';
1212
const BAR_FILL = chroma(BROWN_10).alpha(0.3).css();
1313
const BAR_STROKE = chroma(BROWN_70).alpha(0.3).css();
1414
const BAR_HEIGHT = 24;
15-
const BAR_PADDING_H = 8;
15+
const BAR_PADDING_LEFT = 8;
16+
const BAR_PADDING_RIGHT = 4;
1617
const BAR_BOTTOM_OFFSET = 17;
1718
const BAR_RADIUS = 3;
18-
const BAR_TEXT_GAP = 4; // gap between flex spans
19+
const ELLIPSIS = '...';
20+
21+
const truncateText = (ctx: CanvasRenderingContext2D, text: string, maxWidth: number) => {
22+
if (ctx.measureText(text).width <= maxWidth) return text;
23+
24+
const ellipsisWidth = ctx.measureText(ELLIPSIS).width;
25+
let truncatedText = '';
26+
for (const character of text) {
27+
if (ctx.measureText(truncatedText + character).width + ellipsisWidth > maxWidth) break;
28+
truncatedText += character;
29+
}
30+
return `${truncatedText}${ELLIPSIS}`;
31+
};
1932

2033
export type TrainInfoBarProps = {
2134
/** Length (in ms) of the train's time window. */
@@ -57,15 +70,18 @@ export const TrainInfoBar = ({ duration, origin = 0, name, intervalLabel }: Trai
5770
// Clip to bar interior to replicate overflow: hidden.
5871
ctx.save();
5972
ctx.beginPath();
60-
ctx.rect(left + BAR_PADDING_H, yTop, width - BAR_PADDING_H * 2, BAR_HEIGHT);
73+
ctx.rect(
74+
left + BAR_PADDING_LEFT,
75+
yTop,
76+
width - BAR_PADDING_LEFT - BAR_PADDING_RIGHT,
77+
BAR_HEIGHT
78+
);
6179
ctx.clip();
6280
ctx.fillStyle = '#000000';
6381
ctx.textAlign = 'left';
64-
let textX = left + BAR_PADDING_H;
65-
for (const part of [name, '|', intervalLabel]) {
66-
ctx.fillText(part, textX, textY);
67-
textX += ctx.measureText(part).width + BAR_TEXT_GAP;
68-
}
82+
const text = `${name} | ${intervalLabel}`;
83+
const textWidth = width - BAR_PADDING_LEFT - BAR_PADDING_RIGHT;
84+
ctx.fillText(truncateText(ctx, text, textWidth), left + BAR_PADDING_LEFT, textY);
6985
ctx.restore();
7086
}
7187
},

0 commit comments

Comments
 (0)