140 lines
4.6 KiB
TypeScript
140 lines
4.6 KiB
TypeScript
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
|
|
export function maxChartPointsForViewport(): number {
|
|
return window.innerWidth < 768 ? 60 : 120;
|
|
}
|
|
|
|
function buildPolylinePoints(
|
|
values: number[],
|
|
width: number,
|
|
height: number,
|
|
minVal: number,
|
|
maxVal: number,
|
|
): string {
|
|
const cap = maxChartPointsForViewport();
|
|
const sliced = values.length > cap ? values.slice(values.length - cap) : values;
|
|
if (sliced.length === 0) return '';
|
|
if (sliced.length === 1) {
|
|
const y = computeY(sliced[0]!, height, minVal, maxVal);
|
|
return `0,${y} ${width},${y}`;
|
|
}
|
|
|
|
const span = Math.max(0.0001, maxVal - minVal);
|
|
const stepX = width / (sliced.length - 1);
|
|
return sliced
|
|
.map((v, idx) => {
|
|
const x = (idx * stepX).toFixed(1);
|
|
const norm = Math.max(0, Math.min(1, (v - minVal) / span));
|
|
const y = (height - 4 - norm * (height - 8)).toFixed(1);
|
|
return `${x},${y}`;
|
|
})
|
|
.join(' ');
|
|
}
|
|
|
|
function computeY(v: number, height: number, minVal: number, maxVal: number): string {
|
|
const span = Math.max(0.0001, maxVal - minVal);
|
|
const norm = Math.max(0, Math.min(1, (v - minVal) / span));
|
|
return (height - 4 - norm * (height - 8)).toFixed(1);
|
|
}
|
|
|
|
export function createSparklineSvg(values: number[], maxVal = 100): SVGSVGElement {
|
|
const svg = document.createElementNS(SVG_NS, 'svg');
|
|
svg.setAttribute('viewBox', '0 0 240 44');
|
|
svg.setAttribute('preserveAspectRatio', 'none');
|
|
svg.setAttribute('class', 'spark-svg');
|
|
svg.setAttribute('aria-hidden', 'true');
|
|
|
|
const poly = document.createElementNS(SVG_NS, 'polyline');
|
|
poly.setAttribute('fill', 'none');
|
|
poly.setAttribute('stroke', 'var(--accent)');
|
|
poly.setAttribute('stroke-width', '1.75');
|
|
poly.setAttribute('points', buildPolylinePoints(values, 240, 44, 0, maxVal));
|
|
svg.appendChild(poly);
|
|
return svg;
|
|
}
|
|
|
|
export function updateSparklineSvg(
|
|
svg: SVGSVGElement | null,
|
|
values: number[],
|
|
maxVal = 100,
|
|
): void {
|
|
if (!svg) return;
|
|
const poly = svg.querySelector('polyline');
|
|
if (!poly) return;
|
|
poly.setAttribute('points', buildPolylinePoints(values, 240, 44, 0, maxVal));
|
|
}
|
|
|
|
export function createRangeChartSvg(
|
|
values: number[],
|
|
options: { minVal?: number; maxVal?: number; color?: string } = {},
|
|
): SVGSVGElement {
|
|
const minVal = options.minVal ?? 0;
|
|
const computedMax =
|
|
options.maxVal ?? Math.max(1, ...values.map((v) => (Number.isFinite(v) ? v : 0)));
|
|
|
|
const svg = document.createElementNS(SVG_NS, 'svg');
|
|
svg.setAttribute('viewBox', '0 0 600 120');
|
|
svg.setAttribute('preserveAspectRatio', 'none');
|
|
svg.setAttribute('class', 'chart-svg');
|
|
|
|
const midLine = document.createElementNS(SVG_NS, 'line');
|
|
midLine.setAttribute('x1', '0');
|
|
midLine.setAttribute('y1', '60');
|
|
midLine.setAttribute('x2', '600');
|
|
midLine.setAttribute('y2', '60');
|
|
midLine.setAttribute('stroke', 'var(--border)');
|
|
midLine.setAttribute('stroke-dasharray', '4 4');
|
|
svg.appendChild(midLine);
|
|
|
|
const poly = document.createElementNS(SVG_NS, 'polyline');
|
|
poly.setAttribute('fill', 'none');
|
|
poly.setAttribute('stroke', options.color ?? 'var(--accent)');
|
|
poly.setAttribute('stroke-width', '2');
|
|
poly.setAttribute(
|
|
'points',
|
|
buildPolylinePoints(values, 600, 120, minVal, computedMax),
|
|
);
|
|
svg.appendChild(poly);
|
|
return svg;
|
|
}
|
|
|
|
export function createDualRangeChartSvg(
|
|
seriesA: number[],
|
|
seriesB: number[],
|
|
options: { colorA?: string; colorB?: string; maxVal?: number } = {},
|
|
): SVGSVGElement {
|
|
const computedMax =
|
|
options.maxVal ??
|
|
Math.max(
|
|
1,
|
|
...seriesA.map((v) => (Number.isFinite(v) ? v : 0)),
|
|
...seriesB.map((v) => (Number.isFinite(v) ? v : 0)),
|
|
);
|
|
|
|
const svg = document.createElementNS(SVG_NS, 'svg');
|
|
svg.setAttribute('viewBox', '0 0 600 120');
|
|
svg.setAttribute('preserveAspectRatio', 'none');
|
|
svg.setAttribute('class', 'chart-svg');
|
|
|
|
const midLine = document.createElementNS(SVG_NS, 'line');
|
|
midLine.setAttribute('x1', '0');
|
|
midLine.setAttribute('y1', '60');
|
|
midLine.setAttribute('x2', '600');
|
|
midLine.setAttribute('y2', '60');
|
|
midLine.setAttribute('stroke', 'var(--border)');
|
|
midLine.setAttribute('stroke-dasharray', '4 4');
|
|
svg.appendChild(midLine);
|
|
|
|
const addLine = (vals: number[], stroke: string) => {
|
|
const poly = document.createElementNS(SVG_NS, 'polyline');
|
|
poly.setAttribute('fill', 'none');
|
|
poly.setAttribute('stroke', stroke);
|
|
poly.setAttribute('stroke-width', '2');
|
|
poly.setAttribute('points', buildPolylinePoints(vals, 600, 120, 0, computedMax));
|
|
svg.appendChild(poly);
|
|
};
|
|
|
|
addLine(seriesA, options.colorA ?? 'var(--accent)');
|
|
addLine(seriesB, options.colorB ?? 'var(--warn)');
|
|
return svg;
|
|
}
|