@alpic-ai/ui 0.0.0-dev.g378c8b6 → 0.0.0-dev.g380fab7
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/accordion-card.d.mts +5 -5
- package/dist/components/accordion.d.mts +5 -5
- package/dist/components/alert.d.mts +8 -8
- package/dist/components/area-chart.d.mts +62 -0
- package/dist/components/area-chart.mjs +269 -0
- package/dist/components/attachment-tile.d.mts +1 -1
- package/dist/components/avatar.d.mts +7 -7
- package/dist/components/badge.d.mts +1 -1
- package/dist/components/bar-chart.d.mts +48 -0
- package/dist/components/bar-chart.mjs +245 -0
- package/dist/components/bar-list.d.mts +28 -0
- package/dist/components/bar-list.mjs +98 -0
- package/dist/components/breadcrumb.d.mts +10 -10
- package/dist/components/button.d.mts +5 -5
- package/dist/components/card.d.mts +9 -9
- package/dist/components/chart-card.d.mts +25 -0
- package/dist/components/chart-card.mjs +48 -0
- package/dist/components/chart-container.d.mts +20 -0
- package/dist/components/chart-container.mjs +37 -0
- package/dist/components/chart-legend.d.mts +16 -0
- package/dist/components/chart-legend.mjs +26 -0
- package/dist/components/chart-tooltip.d.mts +33 -0
- package/dist/components/chart-tooltip.mjs +52 -0
- package/dist/components/checkbox.d.mts +2 -2
- package/dist/components/collapsible.d.mts +4 -4
- package/dist/components/combobox.d.mts +10 -10
- package/dist/components/command.d.mts +9 -9
- package/dist/components/copyable.d.mts +2 -2
- package/dist/components/description-list.d.mts +5 -5
- package/dist/components/dialog.d.mts +13 -13
- package/dist/components/donut-chart.d.mts +46 -0
- package/dist/components/donut-chart.mjs +185 -0
- package/dist/components/dropdown-menu.d.mts +17 -17
- package/dist/components/form.d.mts +18 -18
- package/dist/components/form.mjs +6 -6
- package/dist/components/github-button.d.mts +1 -1
- package/dist/components/grid-fx.d.mts +13 -0
- package/dist/components/grid-fx.mjs +188 -0
- package/dist/components/heatmap-chart.d.mts +40 -0
- package/dist/components/heatmap-chart.mjs +198 -0
- package/dist/components/input-group.d.mts +4 -4
- package/dist/components/input.d.mts +4 -4
- package/dist/components/input.mjs +2 -2
- package/dist/components/label.d.mts +2 -2
- package/dist/components/line-chart.d.mts +55 -0
- package/dist/components/line-chart.mjs +211 -0
- package/dist/components/page-loader.d.mts +1 -1
- package/dist/components/pagination.d.mts +3 -3
- package/dist/components/popover.d.mts +5 -5
- package/dist/components/radio-group.d.mts +3 -3
- package/dist/components/scroll-area.d.mts +3 -3
- package/dist/components/select.d.mts +9 -9
- package/dist/components/separator.d.mts +2 -2
- package/dist/components/sheet.d.mts +11 -11
- package/dist/components/shimmer-text.d.mts +3 -1
- package/dist/components/sidebar.d.mts +33 -33
- package/dist/components/sidebar.mjs +10 -10
- package/dist/components/skeleton.d.mts +1 -1
- package/dist/components/sonner.d.mts +5 -5
- package/dist/components/spinner.d.mts +2 -2
- package/dist/components/stat.d.mts +30 -0
- package/dist/components/stat.mjs +107 -0
- package/dist/components/status-dot.d.mts +1 -1
- package/dist/components/switch.d.mts +2 -2
- package/dist/components/table.d.mts +10 -10
- package/dist/components/tabs.d.mts +10 -10
- package/dist/components/tag.d.mts +3 -3
- package/dist/components/task-progress.d.mts +1 -1
- package/dist/components/textarea.d.mts +3 -3
- package/dist/components/textarea.mjs +2 -2
- package/dist/components/toggle-group.d.mts +3 -3
- package/dist/components/toggle-group.mjs +3 -3
- package/dist/components/tooltip.d.mts +5 -5
- package/dist/components/typography.d.mts +4 -4
- package/dist/components/wizard.d.mts +4 -4
- package/dist/hooks/use-chart-theme.d.mts +18 -0
- package/dist/hooks/use-chart-theme.mjs +57 -0
- package/dist/hooks/use-mobile.mjs +3 -3
- package/dist/hooks/use-reduced-motion.d.mts +4 -0
- package/dist/hooks/use-reduced-motion.mjs +16 -0
- package/dist/lib/chart-palette.d.mts +4 -0
- package/dist/lib/chart-palette.mjs +95 -0
- package/dist/lib/chart.d.mts +14 -0
- package/dist/lib/chart.mjs +27 -0
- package/package.json +30 -29
- package/src/components/area-chart.tsx +339 -0
- package/src/components/bar-chart.tsx +300 -0
- package/src/components/bar-list.tsx +150 -0
- package/src/components/chart-card.tsx +63 -0
- package/src/components/chart-container.tsx +49 -0
- package/src/components/chart-legend.tsx +41 -0
- package/src/components/chart-tooltip.tsx +93 -0
- package/src/components/donut-chart.tsx +217 -0
- package/src/components/grid-fx.tsx +238 -0
- package/src/components/heatmap-chart.tsx +287 -0
- package/src/components/line-chart.tsx +264 -0
- package/src/components/stat.tsx +109 -0
- package/src/hooks/use-chart-theme.ts +75 -0
- package/src/hooks/use-reduced-motion.ts +17 -0
- package/src/lib/chart-palette.ts +110 -0
- package/src/lib/chart.ts +56 -0
- package/src/stories/area-chart.stories.tsx +200 -0
- package/src/stories/bar-chart.stories.tsx +169 -0
- package/src/stories/bar-list.stories.tsx +85 -0
- package/src/stories/donut-chart.stories.tsx +112 -0
- package/src/stories/heatmap-chart.stories.tsx +107 -0
- package/src/stories/line-chart.stories.tsx +146 -0
- package/src/stories/stat.stories.tsx +64 -0
- package/src/styles/tokens.css +63 -0
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import {
|
|
5
|
+
Area,
|
|
6
|
+
CartesianGrid,
|
|
7
|
+
LabelList,
|
|
8
|
+
AreaChart as RechartsAreaChart,
|
|
9
|
+
ReferenceArea,
|
|
10
|
+
ReferenceDot,
|
|
11
|
+
ReferenceLine,
|
|
12
|
+
ResponsiveContainer,
|
|
13
|
+
Tooltip,
|
|
14
|
+
XAxis,
|
|
15
|
+
YAxis,
|
|
16
|
+
} from "recharts";
|
|
17
|
+
|
|
18
|
+
import { useReducedMotion } from "../hooks/use-reduced-motion";
|
|
19
|
+
import { type ChartSeries, orderByLuminance, resolveSeries } from "../lib/chart";
|
|
20
|
+
import type { ChartPaletteName } from "../lib/chart-palette";
|
|
21
|
+
import { cn } from "../lib/cn";
|
|
22
|
+
import { useChartContext } from "./chart-container";
|
|
23
|
+
import { ChartLegend } from "./chart-legend";
|
|
24
|
+
import { ChartTooltipContent } from "./chart-tooltip";
|
|
25
|
+
|
|
26
|
+
const CURVE_TYPE = { monotone: "monotone", linear: "linear", step: "stepAfter" } as const;
|
|
27
|
+
|
|
28
|
+
export interface ChartMarker {
|
|
29
|
+
x: string | number;
|
|
30
|
+
y: number;
|
|
31
|
+
label?: string;
|
|
32
|
+
color?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface AreaChartProps {
|
|
36
|
+
data: ReadonlyArray<Record<string, string | number | null | undefined>>;
|
|
37
|
+
index: string;
|
|
38
|
+
series: ChartSeries[];
|
|
39
|
+
variant?: "stacked" | "grouped" | "expand";
|
|
40
|
+
curve?: keyof typeof CURVE_TYPE;
|
|
41
|
+
legend?: boolean;
|
|
42
|
+
valueFlags?: boolean;
|
|
43
|
+
height?: number;
|
|
44
|
+
yAxisWidth?: number;
|
|
45
|
+
palette?: ChartPaletteName;
|
|
46
|
+
referenceLine?: { y: number; label?: string; band?: boolean };
|
|
47
|
+
markers?: ChartMarker[];
|
|
48
|
+
lastValueLabel?: boolean;
|
|
49
|
+
texture?: boolean;
|
|
50
|
+
loading?: boolean;
|
|
51
|
+
valueFormatter?: (value: number) => string;
|
|
52
|
+
labelFormatter?: (label: string | number) => string;
|
|
53
|
+
className?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function AreaChart({
|
|
57
|
+
data,
|
|
58
|
+
index,
|
|
59
|
+
series,
|
|
60
|
+
variant = "stacked",
|
|
61
|
+
curve = "monotone",
|
|
62
|
+
legend = false,
|
|
63
|
+
valueFlags = false,
|
|
64
|
+
height = 200,
|
|
65
|
+
yAxisWidth = 48,
|
|
66
|
+
palette,
|
|
67
|
+
referenceLine,
|
|
68
|
+
markers,
|
|
69
|
+
lastValueLabel = false,
|
|
70
|
+
texture = false,
|
|
71
|
+
loading = false,
|
|
72
|
+
valueFormatter = (value) => value.toLocaleString("en-US"),
|
|
73
|
+
labelFormatter,
|
|
74
|
+
className,
|
|
75
|
+
}: AreaChartProps) {
|
|
76
|
+
const { palette: paletteColors, theme } = useChartContext(palette);
|
|
77
|
+
const reactId = React.useId().replace(/:/g, "");
|
|
78
|
+
const reducedMotion = useReducedMotion();
|
|
79
|
+
const animated = !reducedMotion;
|
|
80
|
+
|
|
81
|
+
const resolved = resolveSeries(series, paletteColors, theme);
|
|
82
|
+
const stacked = variant === "stacked" || variant === "expand";
|
|
83
|
+
const rendered = stacked ? orderByLuminance(resolved) : resolved;
|
|
84
|
+
|
|
85
|
+
const strokeOnly = variant === "grouped" && rendered.length > 1;
|
|
86
|
+
const filled = !strokeOnly;
|
|
87
|
+
const lead = resolved[0];
|
|
88
|
+
const withTotal = stacked && rendered.length > 1;
|
|
89
|
+
|
|
90
|
+
// Stacked bands must reach the stack height (sum per x-point), not the tallest
|
|
91
|
+
// single series, or the reference band stops short of the chart top.
|
|
92
|
+
const numericMax = React.useMemo(() => {
|
|
93
|
+
let max = 0;
|
|
94
|
+
for (const row of data) {
|
|
95
|
+
let rowTotal = 0;
|
|
96
|
+
for (const entry of series) {
|
|
97
|
+
const value = Number(row[entry.key]);
|
|
98
|
+
if (Number.isFinite(value)) {
|
|
99
|
+
rowTotal += value;
|
|
100
|
+
if (!stacked && value > max) {
|
|
101
|
+
max = value;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (stacked && rowTotal > max) {
|
|
106
|
+
max = rowTotal;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return max;
|
|
110
|
+
}, [data, series, stacked]);
|
|
111
|
+
|
|
112
|
+
const curveType = CURVE_TYPE[curve];
|
|
113
|
+
const margin = { top: markers?.length ? 18 : 8, right: lastValueLabel ? 56 : 8, bottom: 2, left: 0 };
|
|
114
|
+
|
|
115
|
+
const axis = {
|
|
116
|
+
stroke: theme.border,
|
|
117
|
+
tick: { fill: theme.axisForeground, fontSize: 10, fontFamily: theme.fontMono },
|
|
118
|
+
tickLine: false as const,
|
|
119
|
+
axisLine: { stroke: theme.border, strokeOpacity: 0.6 },
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const legendItems = resolved.map((entry) => ({ name: entry.name, color: entry.color, dashed: entry.dashed }));
|
|
123
|
+
|
|
124
|
+
const activeDotFor = (entry: (typeof rendered)[number]) =>
|
|
125
|
+
valueFlags
|
|
126
|
+
? (dotProps: { cx?: number; cy?: number; value?: number | string }) => {
|
|
127
|
+
if (dotProps.cx == null || dotProps.cy == null) {
|
|
128
|
+
return <g />;
|
|
129
|
+
}
|
|
130
|
+
return (
|
|
131
|
+
<g>
|
|
132
|
+
<circle
|
|
133
|
+
cx={dotProps.cx}
|
|
134
|
+
cy={dotProps.cy}
|
|
135
|
+
r={3.5}
|
|
136
|
+
fill={entry.color}
|
|
137
|
+
stroke={theme.card}
|
|
138
|
+
strokeWidth={2}
|
|
139
|
+
/>
|
|
140
|
+
<text
|
|
141
|
+
x={dotProps.cx}
|
|
142
|
+
y={dotProps.cy - 8}
|
|
143
|
+
textAnchor="middle"
|
|
144
|
+
fill={entry.color}
|
|
145
|
+
fontFamily={theme.fontMono}
|
|
146
|
+
fontSize={10}
|
|
147
|
+
style={{ fontVariantNumeric: "tabular-nums" }}
|
|
148
|
+
>
|
|
149
|
+
{valueFormatter(Number(dotProps.value ?? 0))}
|
|
150
|
+
</text>
|
|
151
|
+
</g>
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
: { r: 3.5, fill: entry.color, stroke: theme.card, strokeWidth: 2 };
|
|
155
|
+
|
|
156
|
+
const fillFor = (entry: (typeof rendered)[number], slot: number) => {
|
|
157
|
+
if (!filled) {
|
|
158
|
+
return "none";
|
|
159
|
+
}
|
|
160
|
+
if (texture && lead && entry.key === lead.key) {
|
|
161
|
+
return `url(#hatch-${reactId})`;
|
|
162
|
+
}
|
|
163
|
+
return `url(#area-${reactId}-${slot})`;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const renderLastLabel =
|
|
167
|
+
(color: string) =>
|
|
168
|
+
(props: {
|
|
169
|
+
x?: string | number;
|
|
170
|
+
y?: string | number;
|
|
171
|
+
value?: string | number | boolean | Array<string | number | boolean> | null;
|
|
172
|
+
index?: number;
|
|
173
|
+
}) => {
|
|
174
|
+
if (props.index !== data.length - 1 || props.x == null || props.y == null) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
return (
|
|
178
|
+
<text
|
|
179
|
+
x={Number(props.x) + 6}
|
|
180
|
+
y={Number(props.y)}
|
|
181
|
+
dy={3}
|
|
182
|
+
fill={color}
|
|
183
|
+
fontFamily={theme.fontMono}
|
|
184
|
+
fontSize={10}
|
|
185
|
+
textAnchor="start"
|
|
186
|
+
style={{ fontVariantNumeric: "tabular-nums" }}
|
|
187
|
+
>
|
|
188
|
+
{valueFormatter(Number(props.value ?? 0))}
|
|
189
|
+
</text>
|
|
190
|
+
);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const isEmpty = data.length === 0 || rendered.length === 0;
|
|
194
|
+
|
|
195
|
+
return (
|
|
196
|
+
<div data-slot="area-chart" className={cn("flex w-full flex-col gap-3", className)}>
|
|
197
|
+
<div className="w-full" style={{ height }}>
|
|
198
|
+
{loading ? (
|
|
199
|
+
<div className="flex h-full items-center justify-center gap-2.5 font-mono text-quaternary-foreground text-text-xs">
|
|
200
|
+
<span className="size-4 animate-spin rounded-full border-2 border-border border-t-foreground" />
|
|
201
|
+
loading…
|
|
202
|
+
</div>
|
|
203
|
+
) : isEmpty ? (
|
|
204
|
+
<div className="flex h-full items-center justify-center rounded-lg border border-border border-dashed font-mono text-quaternary-foreground text-text-xs">
|
|
205
|
+
no data in range
|
|
206
|
+
</div>
|
|
207
|
+
) : (
|
|
208
|
+
<ResponsiveContainer width="100%" height="100%">
|
|
209
|
+
<RechartsAreaChart
|
|
210
|
+
data={data as Record<string, string | number>[]}
|
|
211
|
+
stackOffset={variant === "expand" ? "expand" : "none"}
|
|
212
|
+
margin={margin}
|
|
213
|
+
>
|
|
214
|
+
<defs>
|
|
215
|
+
{filled &&
|
|
216
|
+
rendered.map((entry, slot) => (
|
|
217
|
+
<linearGradient key={entry.key} id={`area-${reactId}-${slot}`} x1="0" y1="0" x2="0" y2="1">
|
|
218
|
+
<stop offset="0%" stopColor={entry.color} stopOpacity={stacked ? 0.78 : 0.5} />
|
|
219
|
+
<stop offset="100%" stopColor={entry.color} stopOpacity={stacked ? 0.32 : 0.04} />
|
|
220
|
+
</linearGradient>
|
|
221
|
+
))}
|
|
222
|
+
{texture && filled && lead && (
|
|
223
|
+
<pattern
|
|
224
|
+
id={`hatch-${reactId}`}
|
|
225
|
+
patternUnits="userSpaceOnUse"
|
|
226
|
+
width={6}
|
|
227
|
+
height={6}
|
|
228
|
+
patternTransform="rotate(45)"
|
|
229
|
+
>
|
|
230
|
+
<rect width={6} height={6} fill={lead.color} fillOpacity={0.18} />
|
|
231
|
+
<line x1={0} y1={0} x2={0} y2={6} stroke={lead.color} strokeWidth={1.2} strokeOpacity={0.6} />
|
|
232
|
+
</pattern>
|
|
233
|
+
)}
|
|
234
|
+
</defs>
|
|
235
|
+
|
|
236
|
+
<CartesianGrid vertical={false} stroke={theme.grid} strokeDasharray="2 4" />
|
|
237
|
+
<XAxis dataKey={index} {...axis} interval="preserveStartEnd" minTickGap={44} />
|
|
238
|
+
<YAxis
|
|
239
|
+
{...axis}
|
|
240
|
+
width={yAxisWidth}
|
|
241
|
+
tickFormatter={(value: number) =>
|
|
242
|
+
variant === "expand" ? `${Math.round(value * 100)}%` : valueFormatter(value)
|
|
243
|
+
}
|
|
244
|
+
/>
|
|
245
|
+
<Tooltip
|
|
246
|
+
offset={12}
|
|
247
|
+
allowEscapeViewBox={{ x: false, y: false }}
|
|
248
|
+
cursor={{ stroke: theme.axisForeground, strokeWidth: 1, strokeDasharray: "3 3" }}
|
|
249
|
+
content={
|
|
250
|
+
<ChartTooltipContent
|
|
251
|
+
valueFormatter={valueFormatter}
|
|
252
|
+
labelFormatter={labelFormatter}
|
|
253
|
+
showTotal={withTotal}
|
|
254
|
+
/>
|
|
255
|
+
}
|
|
256
|
+
/>
|
|
257
|
+
{referenceLine?.band && (
|
|
258
|
+
<ReferenceArea
|
|
259
|
+
y1={referenceLine.y}
|
|
260
|
+
y2={numericMax}
|
|
261
|
+
fill={theme.warning}
|
|
262
|
+
fillOpacity={0.06}
|
|
263
|
+
ifOverflow="extendDomain"
|
|
264
|
+
/>
|
|
265
|
+
)}
|
|
266
|
+
{referenceLine && (
|
|
267
|
+
<ReferenceLine
|
|
268
|
+
y={referenceLine.y}
|
|
269
|
+
stroke={theme.warning}
|
|
270
|
+
strokeDasharray="4 4"
|
|
271
|
+
strokeOpacity={0.6}
|
|
272
|
+
label={
|
|
273
|
+
referenceLine.label
|
|
274
|
+
? {
|
|
275
|
+
value: referenceLine.label,
|
|
276
|
+
fill: theme.warning,
|
|
277
|
+
fontSize: 9,
|
|
278
|
+
fontFamily: theme.fontMono,
|
|
279
|
+
position: "insideBottomRight",
|
|
280
|
+
}
|
|
281
|
+
: undefined
|
|
282
|
+
}
|
|
283
|
+
/>
|
|
284
|
+
)}
|
|
285
|
+
|
|
286
|
+
{rendered.map((entry, slot) => (
|
|
287
|
+
<Area
|
|
288
|
+
key={entry.key}
|
|
289
|
+
type={curveType}
|
|
290
|
+
dataKey={entry.key}
|
|
291
|
+
name={entry.name}
|
|
292
|
+
stackId={stacked ? "stack" : undefined}
|
|
293
|
+
stroke={entry.color}
|
|
294
|
+
strokeWidth={entry.dashed ? 2 : 1.6}
|
|
295
|
+
strokeDasharray={entry.dashed ? "5 3" : undefined}
|
|
296
|
+
fill={fillFor(entry, slot)}
|
|
297
|
+
dot={false}
|
|
298
|
+
activeDot={activeDotFor(entry)}
|
|
299
|
+
isAnimationActive={animated}
|
|
300
|
+
animationDuration={650}
|
|
301
|
+
animationEasing="ease-out"
|
|
302
|
+
>
|
|
303
|
+
{lastValueLabel && <LabelList dataKey={entry.key} content={renderLastLabel(entry.color)} />}
|
|
304
|
+
</Area>
|
|
305
|
+
))}
|
|
306
|
+
|
|
307
|
+
{markers?.map((marker) => (
|
|
308
|
+
<ReferenceDot
|
|
309
|
+
key={`${marker.x}-${marker.y}`}
|
|
310
|
+
x={marker.x}
|
|
311
|
+
y={marker.y}
|
|
312
|
+
r={3.5}
|
|
313
|
+
fill={marker.color ?? theme.foreground}
|
|
314
|
+
stroke={theme.card}
|
|
315
|
+
strokeWidth={2}
|
|
316
|
+
label={
|
|
317
|
+
marker.label
|
|
318
|
+
? {
|
|
319
|
+
value: marker.label,
|
|
320
|
+
fill: marker.color ?? theme.foreground,
|
|
321
|
+
fontSize: 9,
|
|
322
|
+
fontFamily: theme.fontMono,
|
|
323
|
+
position: "top",
|
|
324
|
+
}
|
|
325
|
+
: undefined
|
|
326
|
+
}
|
|
327
|
+
/>
|
|
328
|
+
))}
|
|
329
|
+
</RechartsAreaChart>
|
|
330
|
+
</ResponsiveContainer>
|
|
331
|
+
)}
|
|
332
|
+
</div>
|
|
333
|
+
|
|
334
|
+
{legend && !isEmpty && <ChartLegend items={legendItems} style={{ paddingLeft: yAxisWidth }} />}
|
|
335
|
+
</div>
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export { AreaChart };
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import {
|
|
5
|
+
Bar,
|
|
6
|
+
CartesianGrid,
|
|
7
|
+
LabelList,
|
|
8
|
+
BarChart as RechartsBarChart,
|
|
9
|
+
ReferenceArea,
|
|
10
|
+
ReferenceDot,
|
|
11
|
+
ReferenceLine,
|
|
12
|
+
ResponsiveContainer,
|
|
13
|
+
Tooltip,
|
|
14
|
+
XAxis,
|
|
15
|
+
YAxis,
|
|
16
|
+
} from "recharts";
|
|
17
|
+
|
|
18
|
+
import { useReducedMotion } from "../hooks/use-reduced-motion";
|
|
19
|
+
import { type ChartSeries, orderByLuminance, resolveSeries } from "../lib/chart";
|
|
20
|
+
import type { ChartPaletteName } from "../lib/chart-palette";
|
|
21
|
+
import { cn } from "../lib/cn";
|
|
22
|
+
import type { ChartMarker } from "./area-chart";
|
|
23
|
+
import { useChartContext } from "./chart-container";
|
|
24
|
+
import { ChartLegend } from "./chart-legend";
|
|
25
|
+
import { ChartTooltipContent } from "./chart-tooltip";
|
|
26
|
+
|
|
27
|
+
const BAR_RADIUS = 4;
|
|
28
|
+
const MAX_BAR_SIZE = 48;
|
|
29
|
+
|
|
30
|
+
export interface BarChartProps {
|
|
31
|
+
data: ReadonlyArray<Record<string, string | number | null | undefined>>;
|
|
32
|
+
index: string;
|
|
33
|
+
series: ChartSeries[];
|
|
34
|
+
variant?: "stacked" | "grouped" | "expand";
|
|
35
|
+
legend?: boolean;
|
|
36
|
+
valueLabels?: boolean;
|
|
37
|
+
height?: number;
|
|
38
|
+
yAxisWidth?: number;
|
|
39
|
+
palette?: ChartPaletteName;
|
|
40
|
+
referenceLine?: { y: number; label?: string; band?: boolean };
|
|
41
|
+
markers?: ChartMarker[];
|
|
42
|
+
texture?: boolean;
|
|
43
|
+
loading?: boolean;
|
|
44
|
+
valueFormatter?: (value: number) => string;
|
|
45
|
+
labelFormatter?: (label: string | number) => string;
|
|
46
|
+
className?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function BarChart({
|
|
50
|
+
data,
|
|
51
|
+
index,
|
|
52
|
+
series,
|
|
53
|
+
variant = "stacked",
|
|
54
|
+
legend = false,
|
|
55
|
+
valueLabels = false,
|
|
56
|
+
height = 200,
|
|
57
|
+
yAxisWidth = 48,
|
|
58
|
+
palette,
|
|
59
|
+
referenceLine,
|
|
60
|
+
markers,
|
|
61
|
+
texture = false,
|
|
62
|
+
loading = false,
|
|
63
|
+
valueFormatter = (value) => value.toLocaleString("en-US"),
|
|
64
|
+
labelFormatter,
|
|
65
|
+
className,
|
|
66
|
+
}: BarChartProps) {
|
|
67
|
+
const { palette: paletteColors, theme } = useChartContext(palette);
|
|
68
|
+
const reactId = React.useId().replace(/:/g, "");
|
|
69
|
+
const reducedMotion = useReducedMotion();
|
|
70
|
+
const animated = !reducedMotion;
|
|
71
|
+
|
|
72
|
+
const resolved = resolveSeries(series, paletteColors, theme);
|
|
73
|
+
const stacked = variant === "stacked" || variant === "expand";
|
|
74
|
+
const rendered = stacked ? orderByLuminance(resolved) : resolved;
|
|
75
|
+
const lead = resolved[0];
|
|
76
|
+
const withTotal = stacked && rendered.length > 1;
|
|
77
|
+
|
|
78
|
+
const numericMax = React.useMemo(() => {
|
|
79
|
+
let max = 0;
|
|
80
|
+
for (const row of data) {
|
|
81
|
+
for (const entry of series) {
|
|
82
|
+
const value = Number(row[entry.key]);
|
|
83
|
+
if (Number.isFinite(value) && value > max) {
|
|
84
|
+
max = value;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return max;
|
|
89
|
+
}, [data, series]);
|
|
90
|
+
|
|
91
|
+
const margin = { top: markers?.length || valueLabels ? 18 : 8, right: 8, bottom: 2, left: 0 };
|
|
92
|
+
|
|
93
|
+
const axis = {
|
|
94
|
+
stroke: theme.border,
|
|
95
|
+
tick: { fill: theme.axisForeground, fontSize: 10, fontFamily: theme.fontMono },
|
|
96
|
+
tickLine: false as const,
|
|
97
|
+
axisLine: { stroke: theme.border, strokeOpacity: 0.6 },
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const legendItems = resolved.map((entry) => ({ name: entry.name, color: entry.color, dashed: entry.dashed }));
|
|
101
|
+
|
|
102
|
+
const radiusFor = (slot: number): [number, number, number, number] => {
|
|
103
|
+
if (!stacked || slot === rendered.length - 1) {
|
|
104
|
+
return [BAR_RADIUS, BAR_RADIUS, 0, 0];
|
|
105
|
+
}
|
|
106
|
+
return [0, 0, 0, 0];
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const fillFor = (entry: (typeof rendered)[number], slot: number) => {
|
|
110
|
+
if (texture && lead && entry.key === lead.key) {
|
|
111
|
+
return `url(#bar-hatch-${reactId})`;
|
|
112
|
+
}
|
|
113
|
+
// Stacked segments read cleanest as flat solids — a per-segment gradient
|
|
114
|
+
// banding looks like a drop shadow between bands. Grouped bars keep the fade.
|
|
115
|
+
if (stacked) {
|
|
116
|
+
return entry.color;
|
|
117
|
+
}
|
|
118
|
+
return `url(#bar-${reactId}-${slot})`;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const renderValueLabel =
|
|
122
|
+
(color: string) =>
|
|
123
|
+
(props: {
|
|
124
|
+
x?: string | number;
|
|
125
|
+
y?: string | number;
|
|
126
|
+
width?: string | number;
|
|
127
|
+
value?: string | number | boolean | Array<string | number | boolean> | null;
|
|
128
|
+
}) => {
|
|
129
|
+
if (props.x == null || props.y == null) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
return (
|
|
133
|
+
<text
|
|
134
|
+
x={Number(props.x) + Number(props.width ?? 0) / 2}
|
|
135
|
+
y={Number(props.y) - 5}
|
|
136
|
+
textAnchor="middle"
|
|
137
|
+
fill={color}
|
|
138
|
+
fontFamily={theme.fontMono}
|
|
139
|
+
fontSize={10}
|
|
140
|
+
style={{ fontVariantNumeric: "tabular-nums" }}
|
|
141
|
+
>
|
|
142
|
+
{valueFormatter(Number(props.value ?? 0))}
|
|
143
|
+
</text>
|
|
144
|
+
);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const isEmpty = data.length === 0 || rendered.length === 0;
|
|
148
|
+
|
|
149
|
+
return (
|
|
150
|
+
<div data-slot="bar-chart" className={cn("flex w-full flex-col gap-3", className)}>
|
|
151
|
+
<div className="w-full" style={{ height }}>
|
|
152
|
+
{loading ? (
|
|
153
|
+
<div className="flex h-full items-center justify-center gap-2.5 font-mono text-quaternary-foreground text-text-xs">
|
|
154
|
+
<span className="size-4 animate-spin rounded-full border-2 border-border border-t-foreground" />
|
|
155
|
+
loading…
|
|
156
|
+
</div>
|
|
157
|
+
) : isEmpty ? (
|
|
158
|
+
<div className="flex h-full items-center justify-center rounded-lg border border-border border-dashed font-mono text-quaternary-foreground text-text-xs">
|
|
159
|
+
no data in range
|
|
160
|
+
</div>
|
|
161
|
+
) : (
|
|
162
|
+
<ResponsiveContainer width="100%" height="100%">
|
|
163
|
+
<RechartsBarChart
|
|
164
|
+
data={data as Record<string, string | number>[]}
|
|
165
|
+
stackOffset={variant === "expand" ? "expand" : "none"}
|
|
166
|
+
margin={margin}
|
|
167
|
+
barCategoryGap={stacked ? "20%" : "16%"}
|
|
168
|
+
>
|
|
169
|
+
<defs>
|
|
170
|
+
{!stacked &&
|
|
171
|
+
rendered.map((entry, slot) => (
|
|
172
|
+
<linearGradient key={entry.key} id={`bar-${reactId}-${slot}`} x1="0" y1="0" x2="0" y2="1">
|
|
173
|
+
<stop offset="0%" stopColor={entry.color} stopOpacity={0.95} />
|
|
174
|
+
<stop offset="100%" stopColor={entry.color} stopOpacity={0.5} />
|
|
175
|
+
</linearGradient>
|
|
176
|
+
))}
|
|
177
|
+
{texture && lead && (
|
|
178
|
+
<pattern
|
|
179
|
+
id={`bar-hatch-${reactId}`}
|
|
180
|
+
patternUnits="userSpaceOnUse"
|
|
181
|
+
width={6}
|
|
182
|
+
height={6}
|
|
183
|
+
patternTransform="rotate(45)"
|
|
184
|
+
>
|
|
185
|
+
<rect width={6} height={6} fill={lead.color} fillOpacity={0.22} />
|
|
186
|
+
<line x1={0} y1={0} x2={0} y2={6} stroke={lead.color} strokeWidth={1.2} strokeOpacity={0.65} />
|
|
187
|
+
</pattern>
|
|
188
|
+
)}
|
|
189
|
+
</defs>
|
|
190
|
+
|
|
191
|
+
<CartesianGrid vertical={false} stroke={theme.grid} strokeDasharray="2 4" />
|
|
192
|
+
<XAxis dataKey={index} {...axis} interval="preserveStartEnd" minTickGap={44} />
|
|
193
|
+
<YAxis
|
|
194
|
+
{...axis}
|
|
195
|
+
width={yAxisWidth}
|
|
196
|
+
domain={
|
|
197
|
+
referenceLine && variant !== "expand"
|
|
198
|
+
? [0, Math.ceil(Math.max(numericMax, referenceLine.y) * 1.15)]
|
|
199
|
+
: undefined
|
|
200
|
+
}
|
|
201
|
+
tickFormatter={(value: number) =>
|
|
202
|
+
variant === "expand" ? `${Math.round(value * 100)}%` : valueFormatter(value)
|
|
203
|
+
}
|
|
204
|
+
/>
|
|
205
|
+
<Tooltip
|
|
206
|
+
offset={12}
|
|
207
|
+
allowEscapeViewBox={{ x: false, y: false }}
|
|
208
|
+
cursor={{ fill: lead?.color ?? theme.mutedForeground, fillOpacity: theme.isDark ? 0.1 : 0.06 }}
|
|
209
|
+
content={
|
|
210
|
+
<ChartTooltipContent
|
|
211
|
+
valueFormatter={valueFormatter}
|
|
212
|
+
labelFormatter={labelFormatter}
|
|
213
|
+
showTotal={withTotal}
|
|
214
|
+
/>
|
|
215
|
+
}
|
|
216
|
+
/>
|
|
217
|
+
{referenceLine?.band && (
|
|
218
|
+
<ReferenceArea
|
|
219
|
+
y1={referenceLine.y}
|
|
220
|
+
y2={numericMax}
|
|
221
|
+
fill={theme.warning}
|
|
222
|
+
fillOpacity={0.06}
|
|
223
|
+
ifOverflow="extendDomain"
|
|
224
|
+
/>
|
|
225
|
+
)}
|
|
226
|
+
{referenceLine && (
|
|
227
|
+
<ReferenceLine
|
|
228
|
+
y={referenceLine.y}
|
|
229
|
+
stroke={theme.warning}
|
|
230
|
+
strokeDasharray="4 4"
|
|
231
|
+
strokeOpacity={0.6}
|
|
232
|
+
label={
|
|
233
|
+
referenceLine.label
|
|
234
|
+
? {
|
|
235
|
+
value: referenceLine.label,
|
|
236
|
+
fill: theme.warning,
|
|
237
|
+
fontSize: 9,
|
|
238
|
+
fontFamily: theme.fontMono,
|
|
239
|
+
position: "insideBottomRight",
|
|
240
|
+
}
|
|
241
|
+
: undefined
|
|
242
|
+
}
|
|
243
|
+
/>
|
|
244
|
+
)}
|
|
245
|
+
|
|
246
|
+
{rendered.map((entry, slot) => (
|
|
247
|
+
<Bar
|
|
248
|
+
key={entry.key}
|
|
249
|
+
dataKey={entry.key}
|
|
250
|
+
name={entry.name}
|
|
251
|
+
stackId={stacked ? "stack" : undefined}
|
|
252
|
+
fill={fillFor(entry, slot)}
|
|
253
|
+
stroke={entry.color}
|
|
254
|
+
strokeWidth={0}
|
|
255
|
+
radius={radiusFor(slot)}
|
|
256
|
+
maxBarSize={MAX_BAR_SIZE}
|
|
257
|
+
activeBar={{ fillOpacity: 1, stroke: theme.card, strokeWidth: 1 }}
|
|
258
|
+
isAnimationActive={animated}
|
|
259
|
+
animationDuration={650}
|
|
260
|
+
animationEasing="ease-out"
|
|
261
|
+
>
|
|
262
|
+
{valueLabels && (!stacked || rendered.length === 1) && (
|
|
263
|
+
<LabelList dataKey={entry.key} content={renderValueLabel(entry.color)} />
|
|
264
|
+
)}
|
|
265
|
+
</Bar>
|
|
266
|
+
))}
|
|
267
|
+
|
|
268
|
+
{markers?.map((marker) => (
|
|
269
|
+
<ReferenceDot
|
|
270
|
+
key={`${marker.x}-${marker.y}`}
|
|
271
|
+
x={marker.x}
|
|
272
|
+
y={marker.y}
|
|
273
|
+
r={3.5}
|
|
274
|
+
fill={marker.color ?? theme.foreground}
|
|
275
|
+
stroke={theme.card}
|
|
276
|
+
strokeWidth={2}
|
|
277
|
+
label={
|
|
278
|
+
marker.label
|
|
279
|
+
? {
|
|
280
|
+
value: marker.label,
|
|
281
|
+
fill: marker.color ?? theme.foreground,
|
|
282
|
+
fontSize: 9,
|
|
283
|
+
fontFamily: theme.fontMono,
|
|
284
|
+
position: "top",
|
|
285
|
+
}
|
|
286
|
+
: undefined
|
|
287
|
+
}
|
|
288
|
+
/>
|
|
289
|
+
))}
|
|
290
|
+
</RechartsBarChart>
|
|
291
|
+
</ResponsiveContainer>
|
|
292
|
+
)}
|
|
293
|
+
</div>
|
|
294
|
+
|
|
295
|
+
{legend && !isEmpty && <ChartLegend items={legendItems} style={{ paddingLeft: yAxisWidth }} />}
|
|
296
|
+
</div>
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export { BarChart };
|