@sticker-pop/web 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +54 -0
- package/LICENSE +27 -0
- package/README.md +58 -2
- package/fonts/BricolageGrotesque-Variable.ttf +0 -0
- package/fonts/OFL.txt +93 -0
- package/package.json +68 -4
- package/src/behaviors.mjs +1011 -0
- package/src/charts.mjs +50 -0
- package/src/components.css +837 -0
- package/src/fills.css +14 -0
- package/src/format.d.ts +7 -0
- package/src/format.mjs +119 -0
- package/src/sticker-pop.css +4 -0
- package/src/tokens.css +365 -0
- package/src/tokens.d.ts +332 -0
- package/src/tokens.mjs +332 -0
package/src/charts.mjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Sticker Pop charts for the web, as SVG and HTML strings: a donut with one
|
|
2
|
+
// bright per slice outlined in 2.5 ink around a yellow centre disc, bars
|
|
3
|
+
// (see .sp-bars) and a legend (see .sp-legend). No gridlines, no gradients.
|
|
4
|
+
import tokens from './tokens.mjs';
|
|
5
|
+
|
|
6
|
+
const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
7
|
+
const { ink, yellow, white } = tokens.color;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Donut chart SVG. data: [{ label, value, color }]; color is a CSS colour,
|
|
11
|
+
* e.g. tokens.color.orange or 'var(--sp-color-orange)'.
|
|
12
|
+
*/
|
|
13
|
+
export function popDonutSvg(data, { size = 180, thickness = 38, center = '', label = 'Chart' } = {}) {
|
|
14
|
+
const total = data.reduce((s, d) => s + d.value, 0);
|
|
15
|
+
const c = size / 2;
|
|
16
|
+
const outer = c - tokens.border.regular / 2;
|
|
17
|
+
const inner = outer - thickness;
|
|
18
|
+
const pt = (r, a) => [c + r * Math.cos(a), c + r * Math.sin(a)].map((n) => n.toFixed(2)).join(' ');
|
|
19
|
+
let start = -Math.PI / 2;
|
|
20
|
+
const slices = total <= 0
|
|
21
|
+
? `<circle cx="${c}" cy="${c}" r="${outer}" fill="${white}" stroke="${ink}" stroke-width="2.5"/>`
|
|
22
|
+
: data.map((d) => {
|
|
23
|
+
const sweep = (d.value / total) * Math.PI * 2;
|
|
24
|
+
const end = start + Math.min(sweep, Math.PI * 2 - 0.0001);
|
|
25
|
+
const large = sweep > Math.PI ? 1 : 0;
|
|
26
|
+
const path = `M ${pt(outer, start)} A ${outer} ${outer} 0 ${large} 1 ${pt(outer, end)} L ${pt(inner, end)} A ${inner} ${inner} 0 ${large} 0 ${pt(inner, start)} Z`;
|
|
27
|
+
start += sweep;
|
|
28
|
+
return `<path d="${path}" fill="${d.color}" stroke="${ink}" stroke-width="2.5" stroke-linejoin="round"/>`;
|
|
29
|
+
}).join('');
|
|
30
|
+
const disc = inner - 8;
|
|
31
|
+
return `<svg class="sp-donut" viewBox="0 0 ${size} ${size}" width="${size}" height="${size}" role="img" aria-label="${esc(label)}">${slices}`
|
|
32
|
+
+ `<circle cx="${c}" cy="${c}" r="${disc}" fill="${yellow}" stroke="${ink}" stroke-width="2.5"/>`
|
|
33
|
+
+ (center ? `<text x="${c}" y="${c + 5}" text-anchor="middle" font-family="Bricolage Grotesque, sans-serif" font-weight="800" font-size="15" fill="${ink}">${esc(center)}</text>` : '')
|
|
34
|
+
+ '</svg>';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Bars HTML for .sp-bars; the bar at highlightIndex gets a heavier outline and shadow. */
|
|
38
|
+
export function popBarsHtml(data, { height = 140, highlightIndex, label = 'Chart' } = {}) {
|
|
39
|
+
const max = Math.max(0, ...data.map((d) => d.value));
|
|
40
|
+
return `<div class="sp-bars" role="img" aria-label="${esc(label)}">${data.map((d, i) => {
|
|
41
|
+
const h = max === 0 ? 4 : Math.max(4, (d.value / max) * height);
|
|
42
|
+
return `<div class="${i === highlightIndex ? 'is-current' : ''}"><i style="--h:${h.toFixed(0)}px;--sp-bar-fill:${d.color}"></i>${esc(d.label)}</div>`;
|
|
43
|
+
}).join('')}</div>`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Legend HTML for .sp-legend with rounded percentages. */
|
|
47
|
+
export function popLegendHtml(data) {
|
|
48
|
+
const total = data.reduce((s, d) => s + d.value, 0);
|
|
49
|
+
return `<div class="sp-legend">${data.map((d) => `<div><i class="sp-swatch" style="--sp-swatch-fill:${d.color}"></i>${esc(d.label)}<b>${total ? Math.round((d.value / total) * 100) : 0}%</b></div>`).join('')}</div>`;
|
|
50
|
+
}
|