@r2digisolutions/components 0.21.33 → 0.21.34
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/dist/components/molecules/AreaLineChart/AreaLineChart.svelte +57 -37
- package/dist/components/molecules/BarChart/BarChart.svelte +34 -22
- package/dist/components/molecules/HorizontalBarChart/HorizontalBarChart.svelte +48 -89
- package/dist/components/molecules/HorizontalBarChart/HorizontalBarChart.svelte.d.ts +2 -2
- package/package.json +1 -1
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
}: AreaLineChartProps = $props();
|
|
51
51
|
|
|
52
52
|
const W = 500;
|
|
53
|
-
const pad: ChartPad = { ...DEFAULT_PAD, b:
|
|
53
|
+
const pad: ChartPad = { ...DEFAULT_PAD, b: 28, l: 40 };
|
|
54
54
|
const uid = `alc-${Math.random().toString(36).slice(2, 8)}`;
|
|
55
55
|
|
|
56
56
|
let tipIndex = $state<number | null>(null);
|
|
@@ -76,9 +76,7 @@
|
|
|
76
76
|
const { min, max } = domain;
|
|
77
77
|
return [0, 0.5, 1].map((t) => {
|
|
78
78
|
const raw = min + t * (max - min);
|
|
79
|
-
const label = integerValues
|
|
80
|
-
? String(Math.round(raw))
|
|
81
|
-
: formatTick(raw);
|
|
79
|
+
const label = integerValues ? String(Math.round(raw)) : formatTick(raw);
|
|
82
80
|
return {
|
|
83
81
|
y: pad.t + (1 - t) * (height - pad.t - pad.b),
|
|
84
82
|
label
|
|
@@ -86,6 +84,18 @@
|
|
|
86
84
|
});
|
|
87
85
|
});
|
|
88
86
|
|
|
87
|
+
/** First / last / sparse middle — HTML so labels stay readable (SVG stretch does not squash them). */
|
|
88
|
+
const xLabels = $derived.by(() => {
|
|
89
|
+
const out: { label: string; leftPct: number }[] = [];
|
|
90
|
+
for (let i = 0; i < data.length; i++) {
|
|
91
|
+
const show = i === 0 || i === data.length - 1 || data.length <= 7;
|
|
92
|
+
const p = points[i];
|
|
93
|
+
if (!show || !p) continue;
|
|
94
|
+
out.push({ label: data[i].label, leftPct: (p.x / W) * 100 });
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
});
|
|
98
|
+
|
|
89
99
|
const tip = $derived.by(() => {
|
|
90
100
|
if (tipIndex == null || !points[tipIndex]) return null;
|
|
91
101
|
const p = points[tipIndex];
|
|
@@ -106,14 +116,49 @@
|
|
|
106
116
|
}
|
|
107
117
|
</script>
|
|
108
118
|
|
|
109
|
-
<div
|
|
119
|
+
<div
|
|
120
|
+
class={['relative w-full select-none', className]}
|
|
121
|
+
style:height={`${height}px`}
|
|
122
|
+
role="img"
|
|
123
|
+
aria-label="Area line chart"
|
|
124
|
+
>
|
|
125
|
+
<!-- HTML axis labels: preserveAspectRatio=none would distort SVG <text> -->
|
|
126
|
+
{#if showGrid}
|
|
127
|
+
<div class="pointer-events-none absolute inset-0 z-[1]">
|
|
128
|
+
{#each gridYs as g (g.label + g.y)}
|
|
129
|
+
<span
|
|
130
|
+
class="text-secondary absolute -translate-y-1/2 text-[11px] leading-none tabular-nums"
|
|
131
|
+
style:left="0"
|
|
132
|
+
style:top={`${(g.y / height) * 100}%`}
|
|
133
|
+
style:width={`${(pad.l / W) * 100}%`}
|
|
134
|
+
style:text-align="right"
|
|
135
|
+
style:padding-right="6px"
|
|
136
|
+
>
|
|
137
|
+
{g.label}
|
|
138
|
+
</span>
|
|
139
|
+
{/each}
|
|
140
|
+
</div>
|
|
141
|
+
{/if}
|
|
142
|
+
|
|
143
|
+
<div
|
|
144
|
+
class="pointer-events-none absolute inset-x-0 bottom-0 z-[1]"
|
|
145
|
+
style:height={`${(pad.b / height) * 100}%`}
|
|
146
|
+
>
|
|
147
|
+
{#each xLabels as xl (xl.label + xl.leftPct)}
|
|
148
|
+
<span
|
|
149
|
+
class="text-secondary absolute top-1 -translate-x-1/2 text-[11px] leading-none whitespace-nowrap"
|
|
150
|
+
style:left={`${xl.leftPct}%`}
|
|
151
|
+
>
|
|
152
|
+
{xl.label}
|
|
153
|
+
</span>
|
|
154
|
+
{/each}
|
|
155
|
+
</div>
|
|
156
|
+
|
|
110
157
|
<svg
|
|
111
158
|
viewBox={`0 0 ${W} ${height}`}
|
|
112
159
|
preserveAspectRatio="none"
|
|
113
|
-
class="block w-full overflow-visible"
|
|
114
|
-
|
|
115
|
-
role="img"
|
|
116
|
-
aria-label="Area line chart"
|
|
160
|
+
class="absolute inset-0 block h-full w-full overflow-visible"
|
|
161
|
+
aria-hidden="true"
|
|
117
162
|
>
|
|
118
163
|
<defs>
|
|
119
164
|
<linearGradient id="{uid}-fill" x1="0" y1="0" x2="0" y2="1">
|
|
@@ -130,7 +175,7 @@
|
|
|
130
175
|
</defs>
|
|
131
176
|
|
|
132
177
|
{#if showGrid}
|
|
133
|
-
{#each gridYs as g}
|
|
178
|
+
{#each gridYs as g (g.label + g.y)}
|
|
134
179
|
<line
|
|
135
180
|
x1={pad.l}
|
|
136
181
|
x2={W - pad.r}
|
|
@@ -140,15 +185,6 @@
|
|
|
140
185
|
stroke-width="1"
|
|
141
186
|
stroke-dasharray="4 4"
|
|
142
187
|
/>
|
|
143
|
-
<text
|
|
144
|
-
x={pad.l - 8}
|
|
145
|
-
y={g.y + 3}
|
|
146
|
-
text-anchor="end"
|
|
147
|
-
class="fill-muted"
|
|
148
|
-
font-size="10"
|
|
149
|
-
>
|
|
150
|
-
{g.label}
|
|
151
|
-
</text>
|
|
152
188
|
{/each}
|
|
153
189
|
{/if}
|
|
154
190
|
|
|
@@ -168,7 +204,7 @@
|
|
|
168
204
|
/>
|
|
169
205
|
{/if}
|
|
170
206
|
|
|
171
|
-
{#each points as p, i}
|
|
207
|
+
{#each points as p, i (i)}
|
|
172
208
|
{#if showDots}
|
|
173
209
|
<circle
|
|
174
210
|
cx={p.x}
|
|
@@ -183,6 +219,7 @@
|
|
|
183
219
|
{#if interactive}
|
|
184
220
|
<!-- Hit area: pointer-only (no focus ring / square outline) -->
|
|
185
221
|
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
222
|
+
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
|
186
223
|
<circle
|
|
187
224
|
cx={p.x}
|
|
188
225
|
cy={p.y}
|
|
@@ -193,23 +230,6 @@
|
|
|
193
230
|
/>
|
|
194
231
|
{/if}
|
|
195
232
|
{/each}
|
|
196
|
-
|
|
197
|
-
{#each data as d, i}
|
|
198
|
-
{#if i === 0 || i === data.length - 1 || data.length <= 7}
|
|
199
|
-
{@const p = points[i]}
|
|
200
|
-
{#if p}
|
|
201
|
-
<text
|
|
202
|
-
x={p.x}
|
|
203
|
-
y={height - 8}
|
|
204
|
-
text-anchor="middle"
|
|
205
|
-
class="fill-muted"
|
|
206
|
-
font-size="10"
|
|
207
|
-
>
|
|
208
|
-
{d.label}
|
|
209
|
-
</text>
|
|
210
|
-
{/if}
|
|
211
|
-
{/if}
|
|
212
|
-
{/each}
|
|
213
233
|
</svg>
|
|
214
234
|
|
|
215
235
|
{#if tip}
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
}: BarChartProps = $props();
|
|
30
30
|
|
|
31
31
|
const W = 420;
|
|
32
|
-
const pad = { t:
|
|
32
|
+
const pad = { t: 28, r: 12, b: 36, l: 12 };
|
|
33
33
|
const uid = `bc-${Math.random().toString(36).slice(2, 8)}`;
|
|
34
34
|
const color = 'var(--color-brand-500, #6366f1)';
|
|
35
35
|
|
|
@@ -76,14 +76,40 @@
|
|
|
76
76
|
}
|
|
77
77
|
</script>
|
|
78
78
|
|
|
79
|
-
<div
|
|
79
|
+
<div
|
|
80
|
+
class={['relative w-full select-none', className]}
|
|
81
|
+
style:height={`${height}px`}
|
|
82
|
+
role="img"
|
|
83
|
+
aria-label="Bar chart"
|
|
84
|
+
>
|
|
85
|
+
<!-- HTML labels stay readable; SVG stretch would squash <text> -->
|
|
86
|
+
{#each bars as b, i (b.label + i)}
|
|
87
|
+
{#if showValues && b.value > 0}
|
|
88
|
+
<span
|
|
89
|
+
class="text-secondary pointer-events-none absolute z-[1] -translate-x-1/2 -translate-y-full text-[11px] font-semibold tabular-nums"
|
|
90
|
+
style:left={`${(b.cx / W) * 100}%`}
|
|
91
|
+
style:top={`${(b.y / height) * 100}%`}
|
|
92
|
+
style:margin-top="-2px"
|
|
93
|
+
>
|
|
94
|
+
{formatTick(b.value, 0)}{unit}
|
|
95
|
+
</span>
|
|
96
|
+
{/if}
|
|
97
|
+
<span
|
|
98
|
+
class="text-secondary pointer-events-none absolute z-[1] -translate-x-1/2 truncate text-center text-[11px] leading-tight"
|
|
99
|
+
style:left={`${(b.cx / W) * 100}%`}
|
|
100
|
+
style:bottom="4px"
|
|
101
|
+
style:max-width={`${((W - pad.l - pad.r) / Math.max(data.length, 1) / W) * 100}%`}
|
|
102
|
+
title={b.label}
|
|
103
|
+
>
|
|
104
|
+
{axisLabel(b.label)}
|
|
105
|
+
</span>
|
|
106
|
+
{/each}
|
|
107
|
+
|
|
80
108
|
<svg
|
|
81
109
|
viewBox={`0 0 ${W} ${height}`}
|
|
82
110
|
preserveAspectRatio="none"
|
|
83
|
-
class="block w-full overflow-visible"
|
|
84
|
-
|
|
85
|
-
role="img"
|
|
86
|
-
aria-label="Bar chart"
|
|
111
|
+
class="absolute inset-0 block h-full w-full overflow-visible"
|
|
112
|
+
aria-hidden="true"
|
|
87
113
|
>
|
|
88
114
|
<defs>
|
|
89
115
|
<linearGradient id="{uid}-bar" x1="0" y1="0" x2="0" y2="1">
|
|
@@ -92,7 +118,7 @@
|
|
|
92
118
|
</linearGradient>
|
|
93
119
|
</defs>
|
|
94
120
|
|
|
95
|
-
{#each bars as b, i}
|
|
121
|
+
{#each bars as b, i (b.label + i)}
|
|
96
122
|
<rect
|
|
97
123
|
x={b.x}
|
|
98
124
|
y={b.y}
|
|
@@ -103,23 +129,9 @@
|
|
|
103
129
|
opacity={tipIndex == null || tipIndex === i ? 1 : 0.35}
|
|
104
130
|
class="transition-opacity"
|
|
105
131
|
/>
|
|
106
|
-
{#if showValues && b.value > 0}
|
|
107
|
-
<text
|
|
108
|
-
x={b.cx}
|
|
109
|
-
y={b.y - 6}
|
|
110
|
-
text-anchor="middle"
|
|
111
|
-
class="fill-secondary"
|
|
112
|
-
font-size="10"
|
|
113
|
-
font-weight="600"
|
|
114
|
-
>
|
|
115
|
-
{formatTick(b.value, 0)}{unit}
|
|
116
|
-
</text>
|
|
117
|
-
{/if}
|
|
118
|
-
<text x={b.cx} y={height - 8} text-anchor="middle" class="fill-muted" font-size="12">
|
|
119
|
-
{axisLabel(b.label)}
|
|
120
|
-
</text>
|
|
121
132
|
{#if interactive}
|
|
122
133
|
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
134
|
+
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
|
123
135
|
<rect
|
|
124
136
|
x={b.x - 4}
|
|
125
137
|
y={pad.t}
|
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
|
|
10
10
|
interface HorizontalBarChartProps {
|
|
11
11
|
data?: HBarPoint[];
|
|
12
|
-
/**
|
|
12
|
+
/** Kept for API compat — layout is CSS, not SVG viewBox */
|
|
13
13
|
barWidth?: number;
|
|
14
|
-
/**
|
|
14
|
+
/** Kept for API compat — label column uses CSS */
|
|
15
15
|
labelWidth?: number;
|
|
16
16
|
rowHeight?: number;
|
|
17
17
|
interactive?: boolean;
|
|
@@ -22,7 +22,6 @@
|
|
|
22
22
|
|
|
23
23
|
const {
|
|
24
24
|
data = [],
|
|
25
|
-
barWidth = 280,
|
|
26
25
|
labelWidth = 88,
|
|
27
26
|
rowHeight = 36,
|
|
28
27
|
interactive = true,
|
|
@@ -32,107 +31,67 @@
|
|
|
32
31
|
}: HorizontalBarChartProps = $props();
|
|
33
32
|
|
|
34
33
|
const color = 'var(--color-brand-500, #6366f1)';
|
|
35
|
-
const padR = 48;
|
|
36
|
-
const W = $derived(labelWidth + barWidth + padR);
|
|
37
|
-
const H = $derived(Math.max(data.length, 1) * rowHeight + 8);
|
|
38
|
-
const maxLabelChars = $derived(Math.floor(labelWidth / 6.5));
|
|
39
34
|
|
|
40
35
|
let tipIndex = $state<number | null>(null);
|
|
41
36
|
|
|
42
37
|
const max = $derived(Math.max(...data.map((d) => d.value), 1));
|
|
43
38
|
|
|
44
|
-
const rows = $derived(
|
|
45
|
-
data.map((d, i) => {
|
|
46
|
-
const ratio = Math.max(0, d.value) / max;
|
|
47
|
-
const bw = Math.max(d.value > 0 ? 4 : 0, ratio * barWidth);
|
|
48
|
-
const y = 4 + i * rowHeight;
|
|
49
|
-
return { ...d, bw, y, midY: y + (rowHeight - 10) / 2 };
|
|
50
|
-
})
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
function displayLabel(label: string): string {
|
|
54
|
-
const maxChars = maxLabelChars;
|
|
55
|
-
if (label.length <= maxChars) return label;
|
|
56
|
-
return `${label.slice(0, Math.max(0, maxChars - 1))}…`;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
39
|
function select(i: number) {
|
|
60
40
|
if (!interactive) return;
|
|
61
41
|
tipIndex = tipIndex === i ? null : i;
|
|
62
42
|
}
|
|
63
43
|
</script>
|
|
64
44
|
|
|
65
|
-
<div
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
x={labelWidth}
|
|
86
|
-
y={r.y + 4}
|
|
87
|
-
width={barWidth}
|
|
88
|
-
height={rowHeight - 14}
|
|
89
|
-
rx="5"
|
|
90
|
-
class="fill-surface-muted"
|
|
91
|
-
opacity="0.6"
|
|
92
|
-
/>
|
|
93
|
-
<rect
|
|
94
|
-
x={labelWidth}
|
|
95
|
-
y={r.y + 4}
|
|
96
|
-
width={r.bw}
|
|
97
|
-
height={rowHeight - 14}
|
|
98
|
-
rx="5"
|
|
99
|
-
fill={r.color ?? color}
|
|
100
|
-
opacity={tipIndex == null || tipIndex === i ? 1 : 0.35}
|
|
101
|
-
class="transition-opacity"
|
|
102
|
-
/>
|
|
103
|
-
{#if showValues}
|
|
104
|
-
<text
|
|
105
|
-
x={labelWidth + r.bw + 6}
|
|
106
|
-
y={r.midY + 4}
|
|
107
|
-
class="fill-muted"
|
|
108
|
-
font-size="10"
|
|
109
|
-
font-weight="600"
|
|
110
|
-
>
|
|
111
|
-
{formatTick(r.value, 0)}{unit}
|
|
112
|
-
</text>
|
|
113
|
-
{/if}
|
|
114
|
-
{#if interactive}
|
|
115
|
-
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
116
|
-
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
|
117
|
-
<rect
|
|
118
|
-
x={0}
|
|
119
|
-
y={r.y}
|
|
120
|
-
width={W}
|
|
121
|
-
height={rowHeight}
|
|
122
|
-
fill="transparent"
|
|
123
|
-
class="cursor-pointer"
|
|
45
|
+
<div
|
|
46
|
+
class={['relative w-full select-none', className]}
|
|
47
|
+
role="img"
|
|
48
|
+
aria-label="Horizontal bar chart"
|
|
49
|
+
>
|
|
50
|
+
<ul class="flex flex-col" style:gap="2px">
|
|
51
|
+
{#each data as r, i (r.label + i)}
|
|
52
|
+
{@const ratio = Math.max(0, r.value) / max}
|
|
53
|
+
{@const pct = Math.max(r.value > 0 ? 1.5 : 0, ratio * 100)}
|
|
54
|
+
<li>
|
|
55
|
+
<button
|
|
56
|
+
type="button"
|
|
57
|
+
class={[
|
|
58
|
+
'grid w-full items-center gap-2 rounded-lg px-0.5 py-0.5 text-left transition-opacity',
|
|
59
|
+
interactive && 'hover:bg-surface-overlay/80 cursor-pointer',
|
|
60
|
+
!interactive && 'cursor-default',
|
|
61
|
+
tipIndex != null && tipIndex !== i && 'opacity-40'
|
|
62
|
+
]}
|
|
63
|
+
style:min-height={`${rowHeight}px`}
|
|
64
|
+
style:grid-template-columns={`minmax(0, ${labelWidth}px) minmax(0, 1fr) auto`}
|
|
124
65
|
onclick={() => select(i)}
|
|
125
|
-
|
|
126
|
-
|
|
66
|
+
disabled={!interactive}
|
|
67
|
+
>
|
|
68
|
+
<span
|
|
69
|
+
class="text-secondary truncate text-right text-xs leading-snug"
|
|
70
|
+
title={r.label}
|
|
71
|
+
>
|
|
72
|
+
{r.label}
|
|
73
|
+
</span>
|
|
74
|
+
<span class="bg-surface-overlay relative block h-4 overflow-hidden rounded-md">
|
|
75
|
+
<span
|
|
76
|
+
class="absolute inset-y-0 left-0 rounded-md transition-[width]"
|
|
77
|
+
style:width={`${pct}%`}
|
|
78
|
+
style:background={r.color ?? color}
|
|
79
|
+
></span>
|
|
80
|
+
</span>
|
|
81
|
+
{#if showValues}
|
|
82
|
+
<span class="text-secondary min-w-[2ch] text-right text-xs font-semibold tabular-nums">
|
|
83
|
+
{formatTick(r.value, 0)}{unit}
|
|
84
|
+
</span>
|
|
85
|
+
{/if}
|
|
86
|
+
</button>
|
|
87
|
+
</li>
|
|
127
88
|
{/each}
|
|
128
|
-
</
|
|
89
|
+
</ul>
|
|
129
90
|
|
|
130
|
-
{#if tipIndex != null &&
|
|
131
|
-
{@const t =
|
|
91
|
+
{#if tipIndex != null && data[tipIndex]}
|
|
92
|
+
{@const t = data[tipIndex]}
|
|
132
93
|
<div
|
|
133
|
-
class="pointer-events-none absolute z-10 rounded-lg border border-border bg-surface-elevated px-2.5 py-1.5 shadow-lg"
|
|
134
|
-
style:left={`${((labelWidth + t.bw) / W) * 100}%`}
|
|
135
|
-
style:top={`${(t.y / H) * 100}%`}
|
|
94
|
+
class="pointer-events-none absolute top-0 right-0 z-10 rounded-lg border border-border bg-surface-elevated px-2.5 py-1.5 shadow-lg"
|
|
136
95
|
role="status"
|
|
137
96
|
>
|
|
138
97
|
<p class="text-[10px] font-medium text-muted">{t.label}</p>
|
|
@@ -5,9 +5,9 @@ export interface HBarPoint {
|
|
|
5
5
|
}
|
|
6
6
|
interface HorizontalBarChartProps {
|
|
7
7
|
data?: HBarPoint[];
|
|
8
|
-
/**
|
|
8
|
+
/** Kept for API compat — layout is CSS, not SVG viewBox */
|
|
9
9
|
barWidth?: number;
|
|
10
|
-
/**
|
|
10
|
+
/** Kept for API compat — label column uses CSS */
|
|
11
11
|
labelWidth?: number;
|
|
12
12
|
rowHeight?: number;
|
|
13
13
|
interactive?: boolean;
|