Files
brk/website_next/learn/charts/path.js
T
2026-06-06 22:29:33 +02:00

36 lines
907 B
JavaScript

/** @param {number} value */
export function formatCoordinate(value) {
return value.toFixed(2);
}
/**
* @param {string} command
* @param {number} x
* @param {number} y
*/
function createPathCommand(command, x, y) {
return `${command}${formatCoordinate(x)} ${formatCoordinate(y)}`;
}
/** @param {{ x: number, y: number }[]} points */
export function createLinePathData(points) {
return points
.map(({ x, y }, index) => createPathCommand(index ? "L" : "M", x, y))
.join(" ");
}
/** @param {{ x: number, y0: number, y1: number }[]} points */
export function createAreaPathData(points) {
const commands = points.map(({ x, y1 }, index) =>
createPathCommand(index ? "L" : "M", x, y1),
);
for (let index = points.length - 1; index >= 0; index -= 1) {
const { x, y0 } = points[index];
commands.push(createPathCommand("L", x, y0));
}
return `${commands.join(" ")} Z`;
}