@alpic-ai/ui 0.0.0-dev.g16d80c2 → 0.0.0-dev.g172fb9f
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/area-chart.d.mts +62 -0
- package/dist/components/area-chart.mjs +269 -0
- package/dist/components/badge.d.mts +1 -1
- package/dist/components/bar-chart.d.mts +48 -0
- package/dist/components/bar-chart.mjs +256 -0
- package/dist/components/bar-list.d.mts +28 -0
- package/dist/components/bar-list.mjs +98 -0
- package/dist/components/button.d.mts +1 -1
- 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/donut-chart.d.mts +46 -0
- package/dist/components/donut-chart.mjs +185 -0
- 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/line-chart.d.mts +55 -0
- package/dist/components/line-chart.mjs +211 -0
- package/dist/components/spinner.d.mts +1 -1
- package/dist/components/stat.d.mts +30 -0
- package/dist/components/stat.mjs +107 -0
- package/dist/hooks/use-chart-theme.d.mts +18 -0
- package/dist/hooks/use-chart-theme.mjs +57 -0
- 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 +2 -1
- package/src/components/area-chart.tsx +339 -0
- package/src/components/bar-chart.tsx +309 -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,62 @@
|
|
|
1
|
+
import { ChartSeries } from "../lib/chart.mjs";
|
|
2
|
+
import { ChartPaletteName } from "../lib/chart-palette.mjs";
|
|
3
|
+
import * as React$1 from "react";
|
|
4
|
+
|
|
5
|
+
//#region src/components/area-chart.d.ts
|
|
6
|
+
declare const CURVE_TYPE: {
|
|
7
|
+
readonly monotone: "monotone";
|
|
8
|
+
readonly linear: "linear";
|
|
9
|
+
readonly step: "stepAfter";
|
|
10
|
+
};
|
|
11
|
+
interface ChartMarker {
|
|
12
|
+
x: string | number;
|
|
13
|
+
y: number;
|
|
14
|
+
label?: string;
|
|
15
|
+
color?: string;
|
|
16
|
+
}
|
|
17
|
+
interface AreaChartProps {
|
|
18
|
+
data: ReadonlyArray<Record<string, string | number | null | undefined>>;
|
|
19
|
+
index: string;
|
|
20
|
+
series: ChartSeries[];
|
|
21
|
+
variant?: "stacked" | "grouped" | "expand";
|
|
22
|
+
curve?: keyof typeof CURVE_TYPE;
|
|
23
|
+
legend?: boolean;
|
|
24
|
+
valueFlags?: boolean;
|
|
25
|
+
height?: number;
|
|
26
|
+
yAxisWidth?: number;
|
|
27
|
+
palette?: ChartPaletteName;
|
|
28
|
+
referenceLine?: {
|
|
29
|
+
y: number;
|
|
30
|
+
label?: string;
|
|
31
|
+
band?: boolean;
|
|
32
|
+
};
|
|
33
|
+
markers?: ChartMarker[];
|
|
34
|
+
lastValueLabel?: boolean;
|
|
35
|
+
texture?: boolean;
|
|
36
|
+
loading?: boolean;
|
|
37
|
+
valueFormatter?: (value: number) => string;
|
|
38
|
+
labelFormatter?: (label: string | number) => string;
|
|
39
|
+
className?: string;
|
|
40
|
+
}
|
|
41
|
+
declare function AreaChart({
|
|
42
|
+
data,
|
|
43
|
+
index,
|
|
44
|
+
series,
|
|
45
|
+
variant,
|
|
46
|
+
curve,
|
|
47
|
+
legend,
|
|
48
|
+
valueFlags,
|
|
49
|
+
height,
|
|
50
|
+
yAxisWidth,
|
|
51
|
+
palette,
|
|
52
|
+
referenceLine,
|
|
53
|
+
markers,
|
|
54
|
+
lastValueLabel,
|
|
55
|
+
texture,
|
|
56
|
+
loading,
|
|
57
|
+
valueFormatter,
|
|
58
|
+
labelFormatter,
|
|
59
|
+
className
|
|
60
|
+
}: AreaChartProps): React$1.JSX.Element;
|
|
61
|
+
//#endregion
|
|
62
|
+
export { AreaChart, AreaChartProps, ChartMarker };
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cn } from "../lib/cn.mjs";
|
|
3
|
+
import { useReducedMotion } from "../hooks/use-reduced-motion.mjs";
|
|
4
|
+
import { orderByLuminance, resolveSeries } from "../lib/chart.mjs";
|
|
5
|
+
import { useChartContext } from "./chart-container.mjs";
|
|
6
|
+
import { ChartLegend } from "./chart-legend.mjs";
|
|
7
|
+
import { ChartTooltipContent } from "./chart-tooltip.mjs";
|
|
8
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
import * as React$1 from "react";
|
|
10
|
+
import { Area, AreaChart as AreaChart$1, CartesianGrid, LabelList, ReferenceArea, ReferenceDot, ReferenceLine, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts";
|
|
11
|
+
//#region src/components/area-chart.tsx
|
|
12
|
+
const CURVE_TYPE = {
|
|
13
|
+
monotone: "monotone",
|
|
14
|
+
linear: "linear",
|
|
15
|
+
step: "stepAfter"
|
|
16
|
+
};
|
|
17
|
+
function AreaChart({ data, index, series, variant = "stacked", curve = "monotone", legend = false, valueFlags = false, height = 200, yAxisWidth = 48, palette, referenceLine, markers, lastValueLabel = false, texture = false, loading = false, valueFormatter = (value) => value.toLocaleString("en-US"), labelFormatter, className }) {
|
|
18
|
+
const { palette: paletteColors, theme } = useChartContext(palette);
|
|
19
|
+
const reactId = React$1.useId().replace(/:/g, "");
|
|
20
|
+
const animated = !useReducedMotion();
|
|
21
|
+
const resolved = resolveSeries(series, paletteColors, theme);
|
|
22
|
+
const stacked = variant === "stacked" || variant === "expand";
|
|
23
|
+
const rendered = stacked ? orderByLuminance(resolved) : resolved;
|
|
24
|
+
const filled = !(variant === "grouped" && rendered.length > 1);
|
|
25
|
+
const lead = resolved[0];
|
|
26
|
+
const withTotal = stacked && rendered.length > 1;
|
|
27
|
+
const numericMax = React$1.useMemo(() => {
|
|
28
|
+
let max = 0;
|
|
29
|
+
for (const row of data) {
|
|
30
|
+
let rowTotal = 0;
|
|
31
|
+
for (const entry of series) {
|
|
32
|
+
const value = Number(row[entry.key]);
|
|
33
|
+
if (Number.isFinite(value)) {
|
|
34
|
+
rowTotal += value;
|
|
35
|
+
if (!stacked && value > max) max = value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (stacked && rowTotal > max) max = rowTotal;
|
|
39
|
+
}
|
|
40
|
+
return max;
|
|
41
|
+
}, [
|
|
42
|
+
data,
|
|
43
|
+
series,
|
|
44
|
+
stacked
|
|
45
|
+
]);
|
|
46
|
+
const curveType = CURVE_TYPE[curve];
|
|
47
|
+
const margin = {
|
|
48
|
+
top: markers?.length ? 18 : 8,
|
|
49
|
+
right: lastValueLabel ? 56 : 8,
|
|
50
|
+
bottom: 2,
|
|
51
|
+
left: 0
|
|
52
|
+
};
|
|
53
|
+
const axis = {
|
|
54
|
+
stroke: theme.border,
|
|
55
|
+
tick: {
|
|
56
|
+
fill: theme.axisForeground,
|
|
57
|
+
fontSize: 10,
|
|
58
|
+
fontFamily: theme.fontMono
|
|
59
|
+
},
|
|
60
|
+
tickLine: false,
|
|
61
|
+
axisLine: {
|
|
62
|
+
stroke: theme.border,
|
|
63
|
+
strokeOpacity: .6
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const legendItems = resolved.map((entry) => ({
|
|
67
|
+
name: entry.name,
|
|
68
|
+
color: entry.color,
|
|
69
|
+
dashed: entry.dashed
|
|
70
|
+
}));
|
|
71
|
+
const activeDotFor = (entry) => valueFlags ? (dotProps) => {
|
|
72
|
+
if (dotProps.cx == null || dotProps.cy == null) return /* @__PURE__ */ jsx("g", {});
|
|
73
|
+
return /* @__PURE__ */ jsxs("g", { children: [/* @__PURE__ */ jsx("circle", {
|
|
74
|
+
cx: dotProps.cx,
|
|
75
|
+
cy: dotProps.cy,
|
|
76
|
+
r: 3.5,
|
|
77
|
+
fill: entry.color,
|
|
78
|
+
stroke: theme.card,
|
|
79
|
+
strokeWidth: 2
|
|
80
|
+
}), /* @__PURE__ */ jsx("text", {
|
|
81
|
+
x: dotProps.cx,
|
|
82
|
+
y: dotProps.cy - 8,
|
|
83
|
+
textAnchor: "middle",
|
|
84
|
+
fill: entry.color,
|
|
85
|
+
fontFamily: theme.fontMono,
|
|
86
|
+
fontSize: 10,
|
|
87
|
+
style: { fontVariantNumeric: "tabular-nums" },
|
|
88
|
+
children: valueFormatter(Number(dotProps.value ?? 0))
|
|
89
|
+
})] });
|
|
90
|
+
} : {
|
|
91
|
+
r: 3.5,
|
|
92
|
+
fill: entry.color,
|
|
93
|
+
stroke: theme.card,
|
|
94
|
+
strokeWidth: 2
|
|
95
|
+
};
|
|
96
|
+
const fillFor = (entry, slot) => {
|
|
97
|
+
if (!filled) return "none";
|
|
98
|
+
if (texture && lead && entry.key === lead.key) return `url(#hatch-${reactId})`;
|
|
99
|
+
return `url(#area-${reactId}-${slot})`;
|
|
100
|
+
};
|
|
101
|
+
const renderLastLabel = (color) => (props) => {
|
|
102
|
+
if (props.index !== data.length - 1 || props.x == null || props.y == null) return null;
|
|
103
|
+
return /* @__PURE__ */ jsx("text", {
|
|
104
|
+
x: Number(props.x) + 6,
|
|
105
|
+
y: Number(props.y),
|
|
106
|
+
dy: 3,
|
|
107
|
+
fill: color,
|
|
108
|
+
fontFamily: theme.fontMono,
|
|
109
|
+
fontSize: 10,
|
|
110
|
+
textAnchor: "start",
|
|
111
|
+
style: { fontVariantNumeric: "tabular-nums" },
|
|
112
|
+
children: valueFormatter(Number(props.value ?? 0))
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
const isEmpty = data.length === 0 || rendered.length === 0;
|
|
116
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
117
|
+
"data-slot": "area-chart",
|
|
118
|
+
className: cn("flex w-full flex-col gap-3", className),
|
|
119
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
120
|
+
className: "w-full",
|
|
121
|
+
style: { height },
|
|
122
|
+
children: loading ? /* @__PURE__ */ jsxs("div", {
|
|
123
|
+
className: "flex h-full items-center justify-center gap-2.5 font-mono text-quaternary-foreground text-text-xs",
|
|
124
|
+
children: [/* @__PURE__ */ jsx("span", { className: "size-4 animate-spin rounded-full border-2 border-border border-t-foreground" }), "loading…"]
|
|
125
|
+
}) : isEmpty ? /* @__PURE__ */ jsx("div", {
|
|
126
|
+
className: "flex h-full items-center justify-center rounded-lg border border-border border-dashed font-mono text-quaternary-foreground text-text-xs",
|
|
127
|
+
children: "no data in range"
|
|
128
|
+
}) : /* @__PURE__ */ jsx(ResponsiveContainer, {
|
|
129
|
+
width: "100%",
|
|
130
|
+
height: "100%",
|
|
131
|
+
children: /* @__PURE__ */ jsxs(AreaChart$1, {
|
|
132
|
+
data,
|
|
133
|
+
stackOffset: variant === "expand" ? "expand" : "none",
|
|
134
|
+
margin,
|
|
135
|
+
children: [
|
|
136
|
+
/* @__PURE__ */ jsxs("defs", { children: [filled && rendered.map((entry, slot) => /* @__PURE__ */ jsxs("linearGradient", {
|
|
137
|
+
id: `area-${reactId}-${slot}`,
|
|
138
|
+
x1: "0",
|
|
139
|
+
y1: "0",
|
|
140
|
+
x2: "0",
|
|
141
|
+
y2: "1",
|
|
142
|
+
children: [/* @__PURE__ */ jsx("stop", {
|
|
143
|
+
offset: "0%",
|
|
144
|
+
stopColor: entry.color,
|
|
145
|
+
stopOpacity: stacked ? .78 : .5
|
|
146
|
+
}), /* @__PURE__ */ jsx("stop", {
|
|
147
|
+
offset: "100%",
|
|
148
|
+
stopColor: entry.color,
|
|
149
|
+
stopOpacity: stacked ? .32 : .04
|
|
150
|
+
})]
|
|
151
|
+
}, entry.key)), texture && filled && lead && /* @__PURE__ */ jsxs("pattern", {
|
|
152
|
+
id: `hatch-${reactId}`,
|
|
153
|
+
patternUnits: "userSpaceOnUse",
|
|
154
|
+
width: 6,
|
|
155
|
+
height: 6,
|
|
156
|
+
patternTransform: "rotate(45)",
|
|
157
|
+
children: [/* @__PURE__ */ jsx("rect", {
|
|
158
|
+
width: 6,
|
|
159
|
+
height: 6,
|
|
160
|
+
fill: lead.color,
|
|
161
|
+
fillOpacity: .18
|
|
162
|
+
}), /* @__PURE__ */ jsx("line", {
|
|
163
|
+
x1: 0,
|
|
164
|
+
y1: 0,
|
|
165
|
+
x2: 0,
|
|
166
|
+
y2: 6,
|
|
167
|
+
stroke: lead.color,
|
|
168
|
+
strokeWidth: 1.2,
|
|
169
|
+
strokeOpacity: .6
|
|
170
|
+
})]
|
|
171
|
+
})] }),
|
|
172
|
+
/* @__PURE__ */ jsx(CartesianGrid, {
|
|
173
|
+
vertical: false,
|
|
174
|
+
stroke: theme.grid,
|
|
175
|
+
strokeDasharray: "2 4"
|
|
176
|
+
}),
|
|
177
|
+
/* @__PURE__ */ jsx(XAxis, {
|
|
178
|
+
dataKey: index,
|
|
179
|
+
...axis,
|
|
180
|
+
interval: "preserveStartEnd",
|
|
181
|
+
minTickGap: 44
|
|
182
|
+
}),
|
|
183
|
+
/* @__PURE__ */ jsx(YAxis, {
|
|
184
|
+
...axis,
|
|
185
|
+
width: yAxisWidth,
|
|
186
|
+
tickFormatter: (value) => variant === "expand" ? `${Math.round(value * 100)}%` : valueFormatter(value)
|
|
187
|
+
}),
|
|
188
|
+
/* @__PURE__ */ jsx(Tooltip, {
|
|
189
|
+
offset: 12,
|
|
190
|
+
allowEscapeViewBox: {
|
|
191
|
+
x: false,
|
|
192
|
+
y: false
|
|
193
|
+
},
|
|
194
|
+
cursor: {
|
|
195
|
+
stroke: theme.axisForeground,
|
|
196
|
+
strokeWidth: 1,
|
|
197
|
+
strokeDasharray: "3 3"
|
|
198
|
+
},
|
|
199
|
+
content: /* @__PURE__ */ jsx(ChartTooltipContent, {
|
|
200
|
+
valueFormatter,
|
|
201
|
+
labelFormatter,
|
|
202
|
+
showTotal: withTotal
|
|
203
|
+
})
|
|
204
|
+
}),
|
|
205
|
+
referenceLine?.band && /* @__PURE__ */ jsx(ReferenceArea, {
|
|
206
|
+
y1: referenceLine.y,
|
|
207
|
+
y2: numericMax,
|
|
208
|
+
fill: theme.warning,
|
|
209
|
+
fillOpacity: .06,
|
|
210
|
+
ifOverflow: "extendDomain"
|
|
211
|
+
}),
|
|
212
|
+
referenceLine && /* @__PURE__ */ jsx(ReferenceLine, {
|
|
213
|
+
y: referenceLine.y,
|
|
214
|
+
stroke: theme.warning,
|
|
215
|
+
strokeDasharray: "4 4",
|
|
216
|
+
strokeOpacity: .6,
|
|
217
|
+
label: referenceLine.label ? {
|
|
218
|
+
value: referenceLine.label,
|
|
219
|
+
fill: theme.warning,
|
|
220
|
+
fontSize: 9,
|
|
221
|
+
fontFamily: theme.fontMono,
|
|
222
|
+
position: "insideBottomRight"
|
|
223
|
+
} : void 0
|
|
224
|
+
}),
|
|
225
|
+
rendered.map((entry, slot) => /* @__PURE__ */ jsx(Area, {
|
|
226
|
+
type: curveType,
|
|
227
|
+
dataKey: entry.key,
|
|
228
|
+
name: entry.name,
|
|
229
|
+
stackId: stacked ? "stack" : void 0,
|
|
230
|
+
stroke: entry.color,
|
|
231
|
+
strokeWidth: entry.dashed ? 2 : 1.6,
|
|
232
|
+
strokeDasharray: entry.dashed ? "5 3" : void 0,
|
|
233
|
+
fill: fillFor(entry, slot),
|
|
234
|
+
dot: false,
|
|
235
|
+
activeDot: activeDotFor(entry),
|
|
236
|
+
isAnimationActive: animated,
|
|
237
|
+
animationDuration: 650,
|
|
238
|
+
animationEasing: "ease-out",
|
|
239
|
+
children: lastValueLabel && /* @__PURE__ */ jsx(LabelList, {
|
|
240
|
+
dataKey: entry.key,
|
|
241
|
+
content: renderLastLabel(entry.color)
|
|
242
|
+
})
|
|
243
|
+
}, entry.key)),
|
|
244
|
+
markers?.map((marker) => /* @__PURE__ */ jsx(ReferenceDot, {
|
|
245
|
+
x: marker.x,
|
|
246
|
+
y: marker.y,
|
|
247
|
+
r: 3.5,
|
|
248
|
+
fill: marker.color ?? theme.foreground,
|
|
249
|
+
stroke: theme.card,
|
|
250
|
+
strokeWidth: 2,
|
|
251
|
+
label: marker.label ? {
|
|
252
|
+
value: marker.label,
|
|
253
|
+
fill: marker.color ?? theme.foreground,
|
|
254
|
+
fontSize: 9,
|
|
255
|
+
fontFamily: theme.fontMono,
|
|
256
|
+
position: "top"
|
|
257
|
+
} : void 0
|
|
258
|
+
}, `${marker.x}-${marker.y}`))
|
|
259
|
+
]
|
|
260
|
+
})
|
|
261
|
+
})
|
|
262
|
+
}), legend && !isEmpty && /* @__PURE__ */ jsx(ChartLegend, {
|
|
263
|
+
items: legendItems,
|
|
264
|
+
style: { paddingLeft: yAxisWidth }
|
|
265
|
+
})]
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
//#endregion
|
|
269
|
+
export { AreaChart };
|
|
@@ -2,7 +2,7 @@ import { VariantProps } from "class-variance-authority";
|
|
|
2
2
|
|
|
3
3
|
//#region src/components/badge.d.ts
|
|
4
4
|
declare const badgeVariants: (props?: ({
|
|
5
|
-
variant?: "warning" | "success" | "
|
|
5
|
+
variant?: "warning" | "success" | "primary" | "secondary" | "error" | null | undefined;
|
|
6
6
|
size?: "sm" | "md" | null | undefined;
|
|
7
7
|
} & import("class-variance-authority/types").ClassProp) | undefined) => string;
|
|
8
8
|
interface BadgeProps extends React.ComponentProps<"span">, VariantProps<typeof badgeVariants> {}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ChartSeries } from "../lib/chart.mjs";
|
|
2
|
+
import { ChartPaletteName } from "../lib/chart-palette.mjs";
|
|
3
|
+
import { ChartMarker } from "./area-chart.mjs";
|
|
4
|
+
import * as React$1 from "react";
|
|
5
|
+
|
|
6
|
+
//#region src/components/bar-chart.d.ts
|
|
7
|
+
interface BarChartProps {
|
|
8
|
+
data: ReadonlyArray<Record<string, string | number | null | undefined>>;
|
|
9
|
+
index: string;
|
|
10
|
+
series: ChartSeries[];
|
|
11
|
+
variant?: "stacked" | "grouped" | "expand";
|
|
12
|
+
legend?: boolean;
|
|
13
|
+
valueLabels?: boolean;
|
|
14
|
+
height?: number;
|
|
15
|
+
yAxisWidth?: number;
|
|
16
|
+
palette?: ChartPaletteName;
|
|
17
|
+
referenceLine?: {
|
|
18
|
+
y: number;
|
|
19
|
+
label?: string;
|
|
20
|
+
band?: boolean;
|
|
21
|
+
};
|
|
22
|
+
markers?: ChartMarker[];
|
|
23
|
+
texture?: boolean;
|
|
24
|
+
loading?: boolean;
|
|
25
|
+
valueFormatter?: (value: number) => string;
|
|
26
|
+
labelFormatter?: (label: string | number) => string;
|
|
27
|
+
className?: string;
|
|
28
|
+
}
|
|
29
|
+
declare function BarChart({
|
|
30
|
+
data,
|
|
31
|
+
index,
|
|
32
|
+
series,
|
|
33
|
+
variant,
|
|
34
|
+
legend,
|
|
35
|
+
valueLabels,
|
|
36
|
+
height,
|
|
37
|
+
yAxisWidth,
|
|
38
|
+
palette,
|
|
39
|
+
referenceLine,
|
|
40
|
+
markers,
|
|
41
|
+
texture,
|
|
42
|
+
loading,
|
|
43
|
+
valueFormatter,
|
|
44
|
+
labelFormatter,
|
|
45
|
+
className
|
|
46
|
+
}: BarChartProps): React$1.JSX.Element;
|
|
47
|
+
//#endregion
|
|
48
|
+
export { BarChart, BarChartProps };
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cn } from "../lib/cn.mjs";
|
|
3
|
+
import { useReducedMotion } from "../hooks/use-reduced-motion.mjs";
|
|
4
|
+
import { orderByLuminance, resolveSeries } from "../lib/chart.mjs";
|
|
5
|
+
import { useChartContext } from "./chart-container.mjs";
|
|
6
|
+
import { ChartLegend } from "./chart-legend.mjs";
|
|
7
|
+
import { ChartTooltipContent } from "./chart-tooltip.mjs";
|
|
8
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
import * as React$1 from "react";
|
|
10
|
+
import { Bar, BarChart as BarChart$1, CartesianGrid, LabelList, ReferenceArea, ReferenceDot, ReferenceLine, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts";
|
|
11
|
+
//#region src/components/bar-chart.tsx
|
|
12
|
+
const BAR_RADIUS = 4;
|
|
13
|
+
const MAX_BAR_SIZE = 48;
|
|
14
|
+
function BarChart({ data, index, series, variant = "stacked", legend = false, valueLabels = false, height = 200, yAxisWidth = 48, palette, referenceLine, markers, texture = false, loading = false, valueFormatter = (value) => value.toLocaleString("en-US"), labelFormatter, className }) {
|
|
15
|
+
const { palette: paletteColors, theme } = useChartContext(palette);
|
|
16
|
+
const reactId = React$1.useId().replace(/:/g, "");
|
|
17
|
+
const animated = !useReducedMotion();
|
|
18
|
+
const resolved = resolveSeries(series, paletteColors, theme);
|
|
19
|
+
const stacked = variant === "stacked" || variant === "expand";
|
|
20
|
+
const rendered = stacked ? orderByLuminance(resolved) : resolved;
|
|
21
|
+
const lead = resolved[0];
|
|
22
|
+
const withTotal = stacked && rendered.length > 1;
|
|
23
|
+
const numericMax = React$1.useMemo(() => {
|
|
24
|
+
let max = 0;
|
|
25
|
+
for (const row of data) {
|
|
26
|
+
let rowTotal = 0;
|
|
27
|
+
for (const entry of series) {
|
|
28
|
+
const value = Number(row[entry.key]);
|
|
29
|
+
if (Number.isFinite(value)) {
|
|
30
|
+
rowTotal += value;
|
|
31
|
+
if (!stacked && value > max) max = value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (stacked && rowTotal > max) max = rowTotal;
|
|
35
|
+
}
|
|
36
|
+
return max;
|
|
37
|
+
}, [
|
|
38
|
+
data,
|
|
39
|
+
series,
|
|
40
|
+
stacked
|
|
41
|
+
]);
|
|
42
|
+
const margin = {
|
|
43
|
+
top: markers?.length || valueLabels ? 18 : 8,
|
|
44
|
+
right: 8,
|
|
45
|
+
bottom: 2,
|
|
46
|
+
left: 0
|
|
47
|
+
};
|
|
48
|
+
const axis = {
|
|
49
|
+
stroke: theme.border,
|
|
50
|
+
tick: {
|
|
51
|
+
fill: theme.axisForeground,
|
|
52
|
+
fontSize: 10,
|
|
53
|
+
fontFamily: theme.fontMono
|
|
54
|
+
},
|
|
55
|
+
tickLine: false,
|
|
56
|
+
axisLine: {
|
|
57
|
+
stroke: theme.border,
|
|
58
|
+
strokeOpacity: .6
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const legendItems = resolved.map((entry) => ({
|
|
62
|
+
name: entry.name,
|
|
63
|
+
color: entry.color,
|
|
64
|
+
dashed: entry.dashed
|
|
65
|
+
}));
|
|
66
|
+
const radiusFor = (slot) => {
|
|
67
|
+
if (!stacked || slot === rendered.length - 1) return [
|
|
68
|
+
BAR_RADIUS,
|
|
69
|
+
BAR_RADIUS,
|
|
70
|
+
0,
|
|
71
|
+
0
|
|
72
|
+
];
|
|
73
|
+
return [
|
|
74
|
+
0,
|
|
75
|
+
0,
|
|
76
|
+
0,
|
|
77
|
+
0
|
|
78
|
+
];
|
|
79
|
+
};
|
|
80
|
+
const fillFor = (entry, slot) => {
|
|
81
|
+
if (texture && lead && entry.key === lead.key) return `url(#bar-hatch-${reactId})`;
|
|
82
|
+
if (stacked) return entry.color;
|
|
83
|
+
return `url(#bar-${reactId}-${slot})`;
|
|
84
|
+
};
|
|
85
|
+
const renderValueLabel = (color) => (props) => {
|
|
86
|
+
if (props.x == null || props.y == null) return null;
|
|
87
|
+
return /* @__PURE__ */ jsx("text", {
|
|
88
|
+
x: Number(props.x) + Number(props.width ?? 0) / 2,
|
|
89
|
+
y: Number(props.y) - 5,
|
|
90
|
+
textAnchor: "middle",
|
|
91
|
+
fill: color,
|
|
92
|
+
fontFamily: theme.fontMono,
|
|
93
|
+
fontSize: 10,
|
|
94
|
+
style: { fontVariantNumeric: "tabular-nums" },
|
|
95
|
+
children: valueFormatter(Number(props.value ?? 0))
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
const isEmpty = data.length === 0 || rendered.length === 0;
|
|
99
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
100
|
+
"data-slot": "bar-chart",
|
|
101
|
+
className: cn("flex w-full flex-col gap-3", className),
|
|
102
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
103
|
+
className: "w-full",
|
|
104
|
+
style: { height },
|
|
105
|
+
children: loading ? /* @__PURE__ */ jsxs("div", {
|
|
106
|
+
className: "flex h-full items-center justify-center gap-2.5 font-mono text-quaternary-foreground text-text-xs",
|
|
107
|
+
children: [/* @__PURE__ */ jsx("span", { className: "size-4 animate-spin rounded-full border-2 border-border border-t-foreground" }), "loading…"]
|
|
108
|
+
}) : isEmpty ? /* @__PURE__ */ jsx("div", {
|
|
109
|
+
className: "flex h-full items-center justify-center rounded-lg border border-border border-dashed font-mono text-quaternary-foreground text-text-xs",
|
|
110
|
+
children: "no data in range"
|
|
111
|
+
}) : /* @__PURE__ */ jsx(ResponsiveContainer, {
|
|
112
|
+
width: "100%",
|
|
113
|
+
height: "100%",
|
|
114
|
+
children: /* @__PURE__ */ jsxs(BarChart$1, {
|
|
115
|
+
data,
|
|
116
|
+
stackOffset: variant === "expand" ? "expand" : "none",
|
|
117
|
+
margin,
|
|
118
|
+
barCategoryGap: stacked ? "20%" : "16%",
|
|
119
|
+
children: [
|
|
120
|
+
/* @__PURE__ */ jsxs("defs", { children: [!stacked && rendered.map((entry, slot) => /* @__PURE__ */ jsxs("linearGradient", {
|
|
121
|
+
id: `bar-${reactId}-${slot}`,
|
|
122
|
+
x1: "0",
|
|
123
|
+
y1: "0",
|
|
124
|
+
x2: "0",
|
|
125
|
+
y2: "1",
|
|
126
|
+
children: [/* @__PURE__ */ jsx("stop", {
|
|
127
|
+
offset: "0%",
|
|
128
|
+
stopColor: entry.color,
|
|
129
|
+
stopOpacity: .95
|
|
130
|
+
}), /* @__PURE__ */ jsx("stop", {
|
|
131
|
+
offset: "100%",
|
|
132
|
+
stopColor: entry.color,
|
|
133
|
+
stopOpacity: .5
|
|
134
|
+
})]
|
|
135
|
+
}, entry.key)), texture && lead && /* @__PURE__ */ jsxs("pattern", {
|
|
136
|
+
id: `bar-hatch-${reactId}`,
|
|
137
|
+
patternUnits: "userSpaceOnUse",
|
|
138
|
+
width: 6,
|
|
139
|
+
height: 6,
|
|
140
|
+
patternTransform: "rotate(45)",
|
|
141
|
+
children: [/* @__PURE__ */ jsx("rect", {
|
|
142
|
+
width: 6,
|
|
143
|
+
height: 6,
|
|
144
|
+
fill: lead.color,
|
|
145
|
+
fillOpacity: .22
|
|
146
|
+
}), /* @__PURE__ */ jsx("line", {
|
|
147
|
+
x1: 0,
|
|
148
|
+
y1: 0,
|
|
149
|
+
x2: 0,
|
|
150
|
+
y2: 6,
|
|
151
|
+
stroke: lead.color,
|
|
152
|
+
strokeWidth: 1.2,
|
|
153
|
+
strokeOpacity: .65
|
|
154
|
+
})]
|
|
155
|
+
})] }),
|
|
156
|
+
/* @__PURE__ */ jsx(CartesianGrid, {
|
|
157
|
+
vertical: false,
|
|
158
|
+
stroke: theme.grid,
|
|
159
|
+
strokeDasharray: "2 4"
|
|
160
|
+
}),
|
|
161
|
+
/* @__PURE__ */ jsx(XAxis, {
|
|
162
|
+
dataKey: index,
|
|
163
|
+
...axis,
|
|
164
|
+
interval: "preserveStartEnd",
|
|
165
|
+
minTickGap: 44
|
|
166
|
+
}),
|
|
167
|
+
/* @__PURE__ */ jsx(YAxis, {
|
|
168
|
+
...axis,
|
|
169
|
+
width: yAxisWidth,
|
|
170
|
+
domain: referenceLine && variant !== "expand" ? [0, Math.ceil(Math.max(numericMax, referenceLine.y) * 1.15)] : void 0,
|
|
171
|
+
tickFormatter: (value) => variant === "expand" ? `${Math.round(value * 100)}%` : valueFormatter(value)
|
|
172
|
+
}),
|
|
173
|
+
/* @__PURE__ */ jsx(Tooltip, {
|
|
174
|
+
offset: 12,
|
|
175
|
+
allowEscapeViewBox: {
|
|
176
|
+
x: false,
|
|
177
|
+
y: false
|
|
178
|
+
},
|
|
179
|
+
cursor: {
|
|
180
|
+
fill: lead?.color ?? theme.mutedForeground,
|
|
181
|
+
fillOpacity: theme.isDark ? .1 : .06
|
|
182
|
+
},
|
|
183
|
+
content: /* @__PURE__ */ jsx(ChartTooltipContent, {
|
|
184
|
+
valueFormatter,
|
|
185
|
+
labelFormatter,
|
|
186
|
+
showTotal: withTotal
|
|
187
|
+
})
|
|
188
|
+
}),
|
|
189
|
+
referenceLine?.band && /* @__PURE__ */ jsx(ReferenceArea, {
|
|
190
|
+
y1: referenceLine.y,
|
|
191
|
+
y2: numericMax,
|
|
192
|
+
fill: theme.warning,
|
|
193
|
+
fillOpacity: .06,
|
|
194
|
+
ifOverflow: "extendDomain"
|
|
195
|
+
}),
|
|
196
|
+
referenceLine && /* @__PURE__ */ jsx(ReferenceLine, {
|
|
197
|
+
y: referenceLine.y,
|
|
198
|
+
stroke: theme.warning,
|
|
199
|
+
strokeDasharray: "4 4",
|
|
200
|
+
strokeOpacity: .6,
|
|
201
|
+
label: referenceLine.label ? {
|
|
202
|
+
value: referenceLine.label,
|
|
203
|
+
fill: theme.warning,
|
|
204
|
+
fontSize: 9,
|
|
205
|
+
fontFamily: theme.fontMono,
|
|
206
|
+
position: "insideBottomRight"
|
|
207
|
+
} : void 0
|
|
208
|
+
}),
|
|
209
|
+
rendered.map((entry, slot) => /* @__PURE__ */ jsx(Bar, {
|
|
210
|
+
dataKey: entry.key,
|
|
211
|
+
name: entry.name,
|
|
212
|
+
stackId: stacked ? "stack" : void 0,
|
|
213
|
+
fill: fillFor(entry, slot),
|
|
214
|
+
stroke: entry.color,
|
|
215
|
+
strokeWidth: 0,
|
|
216
|
+
radius: radiusFor(slot),
|
|
217
|
+
maxBarSize: MAX_BAR_SIZE,
|
|
218
|
+
activeBar: {
|
|
219
|
+
fillOpacity: 1,
|
|
220
|
+
stroke: theme.card,
|
|
221
|
+
strokeWidth: 1
|
|
222
|
+
},
|
|
223
|
+
isAnimationActive: animated,
|
|
224
|
+
animationDuration: 650,
|
|
225
|
+
animationEasing: "ease-out",
|
|
226
|
+
children: valueLabels && (!stacked || rendered.length === 1) && /* @__PURE__ */ jsx(LabelList, {
|
|
227
|
+
dataKey: entry.key,
|
|
228
|
+
content: renderValueLabel(entry.color)
|
|
229
|
+
})
|
|
230
|
+
}, entry.key)),
|
|
231
|
+
markers?.map((marker) => /* @__PURE__ */ jsx(ReferenceDot, {
|
|
232
|
+
x: marker.x,
|
|
233
|
+
y: marker.y,
|
|
234
|
+
r: 3.5,
|
|
235
|
+
fill: marker.color ?? theme.foreground,
|
|
236
|
+
stroke: theme.card,
|
|
237
|
+
strokeWidth: 2,
|
|
238
|
+
label: marker.label ? {
|
|
239
|
+
value: marker.label,
|
|
240
|
+
fill: marker.color ?? theme.foreground,
|
|
241
|
+
fontSize: 9,
|
|
242
|
+
fontFamily: theme.fontMono,
|
|
243
|
+
position: "top"
|
|
244
|
+
} : void 0
|
|
245
|
+
}, `${marker.x}-${marker.y}`))
|
|
246
|
+
]
|
|
247
|
+
})
|
|
248
|
+
})
|
|
249
|
+
}), legend && !isEmpty && /* @__PURE__ */ jsx(ChartLegend, {
|
|
250
|
+
items: legendItems,
|
|
251
|
+
style: { paddingLeft: yAxisWidth }
|
|
252
|
+
})]
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
//#endregion
|
|
256
|
+
export { BarChart };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ChartPaletteName } from "../lib/chart-palette.mjs";
|
|
2
|
+
import * as React$1 from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/components/bar-list.d.ts
|
|
5
|
+
interface BarListProps {
|
|
6
|
+
data: ReadonlyArray<Record<string, string | number | null | undefined>>;
|
|
7
|
+
index: string;
|
|
8
|
+
dataKey?: string;
|
|
9
|
+
maxItems?: number;
|
|
10
|
+
palette?: ChartPaletteName;
|
|
11
|
+
loading?: boolean;
|
|
12
|
+
valueFormatter?: (value: number) => string;
|
|
13
|
+
labelFormatter?: (label: string | number) => string;
|
|
14
|
+
className?: string;
|
|
15
|
+
}
|
|
16
|
+
declare function BarList({
|
|
17
|
+
data,
|
|
18
|
+
index,
|
|
19
|
+
dataKey,
|
|
20
|
+
maxItems,
|
|
21
|
+
palette,
|
|
22
|
+
loading,
|
|
23
|
+
valueFormatter,
|
|
24
|
+
labelFormatter,
|
|
25
|
+
className
|
|
26
|
+
}: BarListProps): React$1.JSX.Element;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { BarList, BarListProps };
|