StillWorking commited on
Commit
decffdf
·
verified ·
1 Parent(s): c7ebecd

Upload frontend/src/components/ui/chart.tsx with huggingface_hub

Browse files
Files changed (1) hide show
  1. frontend/src/components/ui/chart.tsx +331 -0
frontend/src/components/ui/chart.tsx ADDED
@@ -0,0 +1,331 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import * as React from "react";
2
+ import * as RechartsPrimitive from "recharts";
3
+
4
+ import { cn } from "@/lib/utils";
5
+
6
+ // Format: { THEME_NAME: CSS_SELECTOR }
7
+ const THEMES = { light: "", dark: ".dark" } as const;
8
+
9
+ export type ChartConfig = {
10
+ [k in string]: {
11
+ label?: React.ReactNode;
12
+ icon?: React.ComponentType;
13
+ } & (
14
+ | { color?: string; theme?: never }
15
+ | { color?: never; theme: Record<keyof typeof THEMES, string> }
16
+ );
17
+ };
18
+
19
+ type ChartContextProps = {
20
+ config: ChartConfig;
21
+ };
22
+
23
+ const ChartContext = React.createContext<ChartContextProps | null>(null);
24
+
25
+ function useChart() {
26
+ const context = React.useContext(ChartContext);
27
+
28
+ if (!context) {
29
+ throw new Error("useChart must be used within a <ChartContainer />");
30
+ }
31
+
32
+ return context;
33
+ }
34
+
35
+ const ChartContainer = React.forwardRef<
36
+ HTMLDivElement,
37
+ React.ComponentProps<"div"> & {
38
+ config: ChartConfig;
39
+ children: React.ComponentProps<typeof RechartsPrimitive.ResponsiveContainer>["children"];
40
+ }
41
+ >(({ id, className, children, config, ...props }, ref) => {
42
+ const uniqueId = React.useId();
43
+ const chartId = `chart-${id || uniqueId.replace(/:/g, "")}`;
44
+
45
+ return (
46
+ <ChartContext.Provider value={{ config }}>
47
+ <div
48
+ data-chart={chartId}
49
+ ref={ref}
50
+ className={cn(
51
+ "flex aspect-video justify-center text-xs [&_.recharts-cartesian-axis-tick_text]:fill-muted-foreground [&_.recharts-cartesian-grid_line[stroke='#ccc']]:stroke-border/50 [&_.recharts-curve.recharts-tooltip-cursor]:stroke-border [&_.recharts-dot[stroke='#fff']]:stroke-transparent [&_.recharts-layer]:outline-none [&_.recharts-polar-grid_[stroke='#ccc']]:stroke-border [&_.recharts-radial-bar-background-sector]:fill-muted [&_.recharts-rectangle.recharts-tooltip-cursor]:fill-muted [&_.recharts-reference-line_[stroke='#ccc']]:stroke-border [&_.recharts-sector[stroke='#fff']]:stroke-transparent [&_.recharts-sector]:outline-none [&_.recharts-surface]:outline-none",
52
+ className,
53
+ )}
54
+ {...props}
55
+ >
56
+ <ChartStyle id={chartId} config={config} />
57
+ <RechartsPrimitive.ResponsiveContainer>{children}</RechartsPrimitive.ResponsiveContainer>
58
+ </div>
59
+ </ChartContext.Provider>
60
+ );
61
+ });
62
+ ChartContainer.displayName = "Chart";
63
+
64
+ const ChartStyle = ({ id, config }: { id: string; config: ChartConfig }) => {
65
+ const colorConfig = Object.entries(config).filter(([, config]) => config.theme || config.color);
66
+
67
+ if (!colorConfig.length) {
68
+ return null;
69
+ }
70
+
71
+ return (
72
+ <style
73
+ dangerouslySetInnerHTML={{
74
+ __html: Object.entries(THEMES)
75
+ .map(
76
+ ([theme, prefix]) => `
77
+ ${prefix} [data-chart=${id}] {
78
+ ${colorConfig
79
+ .map(([key, itemConfig]) => {
80
+ const color = itemConfig.theme?.[theme as keyof typeof itemConfig.theme] || itemConfig.color;
81
+ return color ? ` --color-${key}: ${color};` : null;
82
+ })
83
+ .join("\n")}
84
+ }
85
+ `,
86
+ )
87
+ .join("\n"),
88
+ }}
89
+ />
90
+ );
91
+ };
92
+
93
+ const ChartTooltip = RechartsPrimitive.Tooltip;
94
+
95
+ const ChartTooltipContent = React.forwardRef<
96
+ HTMLDivElement,
97
+ React.ComponentProps<typeof RechartsPrimitive.Tooltip> &
98
+ React.ComponentProps<"div"> & {
99
+ hideLabel?: boolean;
100
+ hideIndicator?: boolean;
101
+ indicator?: "line" | "dot" | "dashed";
102
+ nameKey?: string;
103
+ labelKey?: string;
104
+ }
105
+ >(
106
+ (
107
+ {
108
+ active,
109
+ payload,
110
+ className,
111
+ indicator = "dot",
112
+ hideLabel = false,
113
+ hideIndicator = false,
114
+ label,
115
+ labelFormatter,
116
+ labelClassName,
117
+ formatter,
118
+ color,
119
+ nameKey,
120
+ labelKey,
121
+ },
122
+ ref,
123
+ ) => {
124
+ const { config } = useChart();
125
+
126
+ const tooltipLabel = React.useMemo(() => {
127
+ if (hideLabel || !payload?.length) {
128
+ return null;
129
+ }
130
+
131
+ const [item] = payload;
132
+ const key = `${labelKey || item?.dataKey || item?.name || "value"}`;
133
+ const itemConfig = getPayloadConfigFromPayload(config, item, key);
134
+ const value =
135
+ !labelKey && typeof label === "string"
136
+ ? config[label as keyof typeof config]?.label || label
137
+ : itemConfig?.label;
138
+
139
+ if (labelFormatter) {
140
+ return (
141
+ <div className={cn("font-medium", labelClassName)}>{labelFormatter(value, payload)}</div>
142
+ );
143
+ }
144
+
145
+ if (!value) {
146
+ return null;
147
+ }
148
+
149
+ return <div className={cn("font-medium", labelClassName)}>{value}</div>;
150
+ }, [label, labelFormatter, payload, hideLabel, labelClassName, config, labelKey]);
151
+
152
+ if (!active || !payload?.length) {
153
+ return null;
154
+ }
155
+
156
+ const nestLabel = payload.length === 1 && indicator !== "dot";
157
+
158
+ return (
159
+ <div
160
+ ref={ref}
161
+ className={cn(
162
+ "grid min-w-[8rem] items-start gap-1.5 rounded-lg border border-border/50 bg-background px-2.5 py-1.5 text-xs shadow-xl",
163
+ className,
164
+ )}
165
+ >
166
+ {!nestLabel ? tooltipLabel : null}
167
+ <div className="grid gap-1.5">
168
+ {payload
169
+ .filter((item) => item.type !== "none")
170
+ .map((item, index) => {
171
+ const key = `${nameKey || item.name || item.dataKey || "value"}`;
172
+ const itemConfig = getPayloadConfigFromPayload(config, item, key);
173
+ const indicatorColor = color || item.payload.fill || item.color;
174
+
175
+ return (
176
+ <div
177
+ key={item.dataKey}
178
+ className={cn(
179
+ "flex w-full flex-wrap items-stretch gap-2 [&>svg]:h-2.5 [&>svg]:w-2.5 [&>svg]:text-muted-foreground",
180
+ indicator === "dot" && "items-center",
181
+ )}
182
+ >
183
+ {formatter && item?.value !== undefined && item.name ? (
184
+ formatter(item.value, item.name, item, index, item.payload)
185
+ ) : (
186
+ <>
187
+ {itemConfig?.icon ? (
188
+ <itemConfig.icon />
189
+ ) : (
190
+ !hideIndicator && (
191
+ <div
192
+ className={cn(
193
+ "shrink-0 rounded-[2px] border-(--color-border) bg-(--color-bg)",
194
+ {
195
+ "h-2.5 w-2.5": indicator === "dot",
196
+ "w-1": indicator === "line",
197
+ "w-0 border-[1.5px] border-dashed bg-transparent":
198
+ indicator === "dashed",
199
+ "my-0.5": nestLabel && indicator === "dashed",
200
+ },
201
+ )}
202
+ style={
203
+ {
204
+ "--color-bg": indicatorColor,
205
+ "--color-border": indicatorColor,
206
+ } as React.CSSProperties
207
+ }
208
+ />
209
+ )
210
+ )}
211
+ <div
212
+ className={cn(
213
+ "flex flex-1 justify-between leading-none",
214
+ nestLabel ? "items-end" : "items-center",
215
+ )}
216
+ >
217
+ <div className="grid gap-1.5">
218
+ {nestLabel ? tooltipLabel : null}
219
+ <span className="text-muted-foreground">
220
+ {itemConfig?.label || item.name}
221
+ </span>
222
+ </div>
223
+ {item.value && (
224
+ <span className="font-mono font-medium tabular-nums text-foreground">
225
+ {item.value.toLocaleString()}
226
+ </span>
227
+ )}
228
+ </div>
229
+ </>
230
+ )}
231
+ </div>
232
+ );
233
+ })}
234
+ </div>
235
+ </div>
236
+ );
237
+ },
238
+ );
239
+ ChartTooltipContent.displayName = "ChartTooltip";
240
+
241
+ const ChartLegend = RechartsPrimitive.Legend;
242
+
243
+ const ChartLegendContent = React.forwardRef<
244
+ HTMLDivElement,
245
+ React.ComponentProps<"div"> &
246
+ Pick<RechartsPrimitive.LegendProps, "payload" | "verticalAlign"> & {
247
+ hideIcon?: boolean;
248
+ nameKey?: string;
249
+ }
250
+ >(({ className, hideIcon = false, payload, verticalAlign = "bottom", nameKey }, ref) => {
251
+ const { config } = useChart();
252
+
253
+ if (!payload?.length) {
254
+ return null;
255
+ }
256
+
257
+ return (
258
+ <div
259
+ ref={ref}
260
+ className={cn(
261
+ "flex items-center justify-center gap-4",
262
+ verticalAlign === "top" ? "pb-3" : "pt-3",
263
+ className,
264
+ )}
265
+ >
266
+ {payload
267
+ .filter((item) => item.type !== "none")
268
+ .map((item) => {
269
+ const key = `${nameKey || item.dataKey || "value"}`;
270
+ const itemConfig = getPayloadConfigFromPayload(config, item, key);
271
+
272
+ return (
273
+ <div
274
+ key={item.value}
275
+ className={cn(
276
+ "flex items-center gap-1.5 [&>svg]:h-3 [&>svg]:w-3 [&>svg]:text-muted-foreground",
277
+ )}
278
+ >
279
+ {itemConfig?.icon && !hideIcon ? (
280
+ <itemConfig.icon />
281
+ ) : (
282
+ <div
283
+ className="h-2 w-2 shrink-0 rounded-[2px]"
284
+ style={{
285
+ backgroundColor: item.color,
286
+ }}
287
+ />
288
+ )}
289
+ {itemConfig?.label}
290
+ </div>
291
+ );
292
+ })}
293
+ </div>
294
+ );
295
+ });
296
+ ChartLegendContent.displayName = "ChartLegend";
297
+
298
+ // Helper to extract item config from a payload.
299
+ function getPayloadConfigFromPayload(config: ChartConfig, payload: unknown, key: string) {
300
+ if (typeof payload !== "object" || payload === null) {
301
+ return undefined;
302
+ }
303
+
304
+ const payloadPayload =
305
+ "payload" in payload && typeof payload.payload === "object" && payload.payload !== null
306
+ ? payload.payload
307
+ : undefined;
308
+
309
+ let configLabelKey: string = key;
310
+
311
+ if (key in payload && typeof payload[key as keyof typeof payload] === "string") {
312
+ configLabelKey = payload[key as keyof typeof payload] as string;
313
+ } else if (
314
+ payloadPayload &&
315
+ key in payloadPayload &&
316
+ typeof payloadPayload[key as keyof typeof payloadPayload] === "string"
317
+ ) {
318
+ configLabelKey = payloadPayload[key as keyof typeof payloadPayload] as string;
319
+ }
320
+
321
+ return configLabelKey in config ? config[configLabelKey] : config[key as keyof typeof config];
322
+ }
323
+
324
+ export {
325
+ ChartContainer,
326
+ ChartTooltip,
327
+ ChartTooltipContent,
328
+ ChartLegend,
329
+ ChartLegendContent,
330
+ ChartStyle,
331
+ };