Skip to content

Commit

Permalink
ui-trackoccupancydiagram: add through train
Browse files Browse the repository at this point in the history
  - create drawThroughTrain and drawDefaultZone function

Co-authored-by: Yohh <[email protected]>
Signed-off-by: Uriel-Sautron <[email protected]>
  • Loading branch information
Uriel-Sautron and Yohh committed Dec 6, 2024
1 parent c6fe372 commit c6a937d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const OccupancyZones = [
originStation: 'FOO',
destinationStation: 'BAR',
arrivalTime: new Date('2024/04/02 01:30'),
departureTime: new Date('2024/04/02 01:31'),
departureTime: new Date('2024/04/02 01:30'),
},
{
id: '2',
Expand Down Expand Up @@ -96,7 +96,7 @@ const OccupancyZones = [
arrivalTime: new Date('2024/04/02 00:40'),
originStation: 'FOO',
destinationStation: 'BAR',
departureTime: new Date('2024/04/02 00:41'),
departureTime: new Date('2024/04/02 00:40'),
},
{
id: '10',
Expand Down Expand Up @@ -228,7 +228,7 @@ const OccupancyZones = [
originStation: 'FOO',
destinationStation: 'BAR',
arrivalTime: new Date('2024/04/02 02:59'),
departureTime: new Date('2024/04/02 03:00'),
departureTime: new Date('2024/04/02 02:59'),
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ import {
COLORS,
} from '../../consts';
import type { OccupancyZone, Track } from '../../types';
type DrawZone = {
ctx: CanvasRenderingContext2D;
arrivalTime: number;
departureTime: number;
};

const drawDefaultZone = ({ ctx, arrivalTime, departureTime }: DrawZone) => {
ctx.beginPath();
ctx.rect(arrivalTime, OCCUPANCY_ZONE_START, departureTime! - arrivalTime, OCCUPANCY_ZONE_HEIGHT);
ctx.fill();
ctx.stroke();
};

const drawThroughTrain = ({ ctx, arrivalTime }: Omit<DrawZone, 'departureTime'>) => {
ctx.beginPath();
ctx.moveTo(arrivalTime - 1, OCCUPANCY_ZONE_START + 1.5);
ctx.lineTo(arrivalTime - 4.5, OCCUPANCY_ZONE_START - 3.5);
ctx.lineTo(arrivalTime + 4.5, OCCUPANCY_ZONE_START - 3.5);
ctx.lineTo(arrivalTime + 1, OCCUPANCY_ZONE_START + 1.5);
ctx.lineTo(arrivalTime + 4.5, OCCUPANCY_ZONE_START + 6.5);
ctx.lineTo(arrivalTime - 4.5, OCCUPANCY_ZONE_START + 6.5);
ctx.lineTo(arrivalTime - 1, OCCUPANCY_ZONE_START + 1.5);
ctx.fill();
ctx.moveTo(arrivalTime - 1, OCCUPANCY_ZONE_START + 1.5);
ctx.lineTo(arrivalTime + 1, OCCUPANCY_ZONE_START + 1.5);
ctx.stroke();
};

export const drawOccupancyZones = ({
ctx,
Expand Down Expand Up @@ -35,25 +62,25 @@ export const drawOccupancyZones = ({
filteredOccupancyZone.forEach((zone) => {
const arrivalTime = getTimePixel(zone.arrivalTime.getTime());
const departureTime = getTimePixel(zone.departureTime.getTime());
const isThroughTrain = arrivalTime === departureTime;

ctx.fillStyle = zone.color;
ctx.strokeStyle = COLORS.WHITE_100;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.rect(
arrivalTime,
OCCUPANCY_ZONE_START,
departureTime - arrivalTime,
OCCUPANCY_ZONE_HEIGHT
);
ctx.fill();
ctx.stroke();
ctx.lineJoin = 'round';
ctx.lineCap = 'round';

if (isThroughTrain) {
drawThroughTrain({ ctx, arrivalTime });
} else {
drawDefaultZone({ ctx, arrivalTime, departureTime });
}
drawOccupancyZonesTexts({
ctx,
zone,
arrivalTime,
departureTime,
isThroughTrain,
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const drawOccupancyZonesTexts = ({
zone,
arrivalTime,
departureTime,
isThroughTrain,
}: {
ctx: CanvasRenderingContext2D;
zone: {
Expand All @@ -33,6 +34,7 @@ export const drawOccupancyZonesTexts = ({
};
arrivalTime: number;
departureTime: number;
isThroughTrain: boolean;
}) => {
const zoneOccupancyLength = departureTime - arrivalTime - STROKE_WIDTH;

Expand Down Expand Up @@ -84,17 +86,18 @@ export const drawOccupancyZonesTexts = ({
stroke: textStroke,
});

drawText({
ctx,
text: zone.departureTime.getMinutes().toLocaleString('fr-FR', { minimumIntegerDigits: 2 }),
x: departureTime,
y: OCCUPANCY_ZONE_START + MINUTES_TEXT_OFFSET,
color: GREY_80,
xPosition: xDeparturePosition,
yPosition: 'top',
font: '400 12px IBM Plex Sans',
stroke: textStroke,
});
if (!isThroughTrain)
drawText({
ctx,
text: zone.departureTime.getMinutes().toLocaleString('fr-FR', { minimumIntegerDigits: 2 }),
x: departureTime,
y: OCCUPANCY_ZONE_START + MINUTES_TEXT_OFFSET,
color: GREY_80,
xPosition: xDeparturePosition,
yPosition: 'top',
font: '400 12px IBM Plex Sans',
stroke: textStroke,
});

// origin & destination
drawText({
Expand Down

0 comments on commit c6a937d

Please sign in to comment.