Files
smeta-web/static/admin2/ui/chart.js
T

33 lines
1.6 KiB
JavaScript

import { h } from '/static/admin2/vendor/preact.module.js';
import htm from '/static/admin2/vendor/htm.module.js';
const html = htm.bind(h);
// SVG-атрибуты, не style: CSP v2 запрещает inline-стили.
export const Bars = ({ values, label, labels = [] }) => {
const max = Math.max(1, ...values.map((v) => Math.max(0, v)));
const w = 12, gap = 6, h0 = 90;
const total = Math.max(1, values.length) * (w + gap);
return html`<figure class="chart">
<figcaption>${label}</figcaption>
<svg viewBox=${`0 0 ${total} ${h0 + 18}`} role="img" aria-label=${label}>
${values.map((v, i) => html`<rect key=${i} x=${i * (w + gap)} y=${h0 - Math.max(0, v) / max * h0}
width=${w} height=${Math.max(1, v) / max * h0} rx="2" fill="#2457d6"></rect>`)}
${labels.map((m, i) => html`<text key=${'l' + i} x=${i * (w + gap)} y=${h0 + 14}
font-size="9" fill="#667085">${String(m).slice(5)}</text>`)}
</svg>
</figure>`;
};
export const Line = ({ values, label }) => {
const max = Math.max(1, ...values.map((v) => Math.max(0, v)));
const pts = values.map((v, i) => `${i * 20},${90 - Math.max(0, v) / max * 80}`).join(' ');
return html`<figure class="chart">
<figcaption>${label}</figcaption>
<svg viewBox=${`0 0 ${Math.max(1, (values.length - 1) * 20)} 94`} role="img" aria-label=${label}>
${values.length > 1
? html`<polyline points=${pts} fill="none" stroke="#067647" stroke-width="3"></polyline>`
: html`<circle cx="0" cy=${90 - Math.max(0, values[0] || 0) / max * 80} r="3" fill="#067647"></circle>`}
</svg>
</figure>`;
};