@mbao01/common 0.0.44 → 0.0.45
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/types/components/Chart/Chart.d.ts +2 -0
- package/dist/types/components/Chart/ChartContext.d.ts +2 -0
- package/dist/types/components/Chart/components/ChartLegend.d.ts +4 -0
- package/dist/types/components/Chart/components/ChartStyle.d.ts +2 -0
- package/dist/types/components/Chart/components/ChartTooltip.d.ts +4 -0
- package/dist/types/components/Chart/components/index.d.ts +3 -0
- package/dist/types/components/Chart/constants.d.ts +22 -0
- package/dist/types/components/Chart/helpers.d.ts +11 -0
- package/dist/types/components/Chart/hooks/index.d.ts +1 -0
- package/dist/types/components/Chart/hooks/useChart/index.d.ts +1 -0
- package/dist/types/components/Chart/hooks/useChart/useChart.d.ts +1 -0
- package/dist/types/components/Chart/index.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/activeShapeArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/animationArgs.d.ts +12 -0
- package/dist/types/components/Chart/stories/args/areaArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/areaChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/barArgs.d.ts +5 -0
- package/dist/types/components/Chart/stories/args/barChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/cartesianChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/cartesianSharedArgs.d.ts +11 -0
- package/dist/types/components/Chart/stories/args/chartArgs.d.ts +6 -0
- package/dist/types/components/Chart/stories/args/dotArgs.d.ts +10 -0
- package/dist/types/components/Chart/stories/args/events.d.ts +804 -0
- package/dist/types/components/Chart/stories/args/labelListArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/legendArgs.d.ts +3 -0
- package/dist/types/components/Chart/stories/args/lineArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/lineChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/pieArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/pieChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/polarChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/polarSharedArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/radarArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/radarChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/radialBarArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/radialBarChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/rectangleArgs.d.ts +9 -0
- package/dist/types/components/Chart/stories/args/referenceSharedArgs.d.ts +4 -0
- package/dist/types/components/Chart/stories/args/sectorArgs.d.ts +7 -0
- package/dist/types/components/Chart/stories/args/sharedAxisArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/stylesArgs.d.ts +6 -0
- package/dist/types/components/Chart/stories/args/textArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/tooltipArgs.d.ts +4 -0
- package/dist/types/components/Chart/stories/args/types.d.ts +13 -0
- package/dist/types/components/Chart/stories/args/xAxisArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/yAxisArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/examples/AreaChart.d.ts +16 -0
- package/dist/types/components/Chart/stories/examples/BarChart.d.ts +20 -0
- package/dist/types/components/Chart/stories/examples/LineChart.d.ts +16 -0
- package/dist/types/components/Chart/stories/examples/PieChart.d.ts +19 -0
- package/dist/types/components/Chart/stories/examples/RadarChart.d.ts +24 -0
- package/dist/types/components/Chart/stories/examples/RadialChart.d.ts +17 -0
- package/dist/types/components/Chart/stories/examples/Tooltip.d.ts +15 -0
- package/dist/types/components/Chart/stories/helpers/index.d.ts +9 -0
- package/dist/types/components/Chart/types.d.ts +37 -0
- package/package.json +5 -3
- package/src/components/Chart/Chart.tsx +26 -0
- package/src/components/Chart/ChartContext.tsx +4 -0
- package/src/components/Chart/components/ChartLegend.tsx +52 -0
- package/src/components/Chart/components/ChartStyle.tsx +32 -0
- package/src/components/Chart/components/ChartTooltip.tsx +126 -0
- package/src/components/Chart/components/index.ts +3 -0
- package/src/components/Chart/constants.ts +78 -0
- package/src/components/Chart/helpers.ts +27 -0
- package/src/components/Chart/hooks/index.ts +1 -0
- package/src/components/Chart/hooks/useChart/index.ts +1 -0
- package/src/components/Chart/hooks/useChart/useChart.ts +12 -0
- package/src/components/Chart/index.ts +2 -0
- package/src/components/Chart/stories/args/activeShapeArgs.ts +34 -0
- package/src/components/Chart/stories/args/animationArgs.ts +67 -0
- package/src/components/Chart/stories/args/areaArgs.ts +16 -0
- package/src/components/Chart/stories/args/areaChartArgs.ts +19 -0
- package/src/components/Chart/stories/args/barArgs.ts +123 -0
- package/src/components/Chart/stories/args/barChartArgs.ts +65 -0
- package/src/components/Chart/stories/args/cartesianChartArgs.ts +42 -0
- package/src/components/Chart/stories/args/cartesianSharedArgs.ts +136 -0
- package/src/components/Chart/stories/args/chartArgs.ts +244 -0
- package/src/components/Chart/stories/args/dotArgs.ts +46 -0
- package/src/components/Chart/stories/args/events.ts +343 -0
- package/src/components/Chart/stories/args/index.ts +178 -0
- package/src/components/Chart/stories/args/labelListArgs.ts +124 -0
- package/src/components/Chart/stories/args/legendArgs.ts +205 -0
- package/src/components/Chart/stories/args/lineArgs.ts +87 -0
- package/src/components/Chart/stories/args/lineChartArgs.ts +17 -0
- package/src/components/Chart/stories/args/pieArgs.ts +18 -0
- package/src/components/Chart/stories/args/pieChartArgs.ts +7 -0
- package/src/components/Chart/stories/args/polarChartArgs.ts +86 -0
- package/src/components/Chart/stories/args/polarSharedArgs.ts +42 -0
- package/src/components/Chart/stories/args/radarArgs.ts +26 -0
- package/src/components/Chart/stories/args/radarChartArgs.ts +26 -0
- package/src/components/Chart/stories/args/radialBarArgs.ts +36 -0
- package/src/components/Chart/stories/args/radialBarChartArgs.ts +16 -0
- package/src/components/Chart/stories/args/rectangleArgs.ts +35 -0
- package/src/components/Chart/stories/args/referenceSharedArgs.ts +75 -0
- package/src/components/Chart/stories/args/sectorArgs.ts +106 -0
- package/src/components/Chart/stories/args/sharedAxisArgs.ts +332 -0
- package/src/components/Chart/stories/args/stylesArgs.ts +258 -0
- package/src/components/Chart/stories/args/textArgs.ts +97 -0
- package/src/components/Chart/stories/args/tooltipArgs.ts +41 -0
- package/src/components/Chart/stories/args/types.ts +23 -0
- package/src/components/Chart/stories/args/xAxisArgs.ts +109 -0
- package/src/components/Chart/stories/args/yAxisArgs.ts +73 -0
- package/src/components/Chart/stories/examples/AreaChart.tsx +156 -0
- package/src/components/Chart/stories/examples/BarChart.tsx +425 -0
- package/src/components/Chart/stories/examples/LineChart.tsx +144 -0
- package/src/components/Chart/stories/examples/PieChart.tsx +238 -0
- package/src/components/Chart/stories/examples/RadarChart.tsx +261 -0
- package/src/components/Chart/stories/examples/RadialChart.tsx +239 -0
- package/src/components/Chart/stories/examples/Tooltip.tsx +68 -0
- package/src/components/Chart/stories/helpers/index.tsx +75 -0
- package/src/components/Chart/types.ts +39 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { Label, LabelList, PolarGrid, PolarRadiusAxis, RadialBar, RadialBarChart } from "recharts";
|
|
2
|
+
import { Chart } from "../../Chart";
|
|
3
|
+
import { ChartTooltip, ChartTooltipContent } from "../../components";
|
|
4
|
+
import { type ChartConfig } from "../../types";
|
|
5
|
+
import { RadialBarChartProps } from "../args";
|
|
6
|
+
|
|
7
|
+
const chartConfig = {
|
|
8
|
+
visitors: {
|
|
9
|
+
label: "Visitors",
|
|
10
|
+
},
|
|
11
|
+
chrome: {
|
|
12
|
+
label: "Chrome",
|
|
13
|
+
color: "hsl(var(--chart-1))",
|
|
14
|
+
},
|
|
15
|
+
safari: {
|
|
16
|
+
label: "Safari",
|
|
17
|
+
color: "hsl(var(--chart-2))",
|
|
18
|
+
},
|
|
19
|
+
firefox: {
|
|
20
|
+
label: "Firefox",
|
|
21
|
+
color: "hsl(var(--chart-3))",
|
|
22
|
+
},
|
|
23
|
+
edge: {
|
|
24
|
+
label: "Edge",
|
|
25
|
+
color: "hsl(var(--chart-4))",
|
|
26
|
+
},
|
|
27
|
+
other: {
|
|
28
|
+
label: "Other",
|
|
29
|
+
color: "hsl(var(--chart-5))",
|
|
30
|
+
},
|
|
31
|
+
} satisfies ChartConfig;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* ## Parent
|
|
35
|
+
*
|
|
36
|
+
* The RadialBarChart can be used within: `<Chart />`
|
|
37
|
+
*
|
|
38
|
+
* ## Children
|
|
39
|
+
*
|
|
40
|
+
* The RadialBarChart can be used with the following child components: `<PolarAngleAxis />`, `<PolarRadiusAxis />`,
|
|
41
|
+
* `<PolarGrid />`, `<ChartLegend />`, `<ChartTooltip />`, `<RadialBar />`, `<Customized />`
|
|
42
|
+
*/
|
|
43
|
+
export const RadialChartExample = (props: Partial<RadialBarChartProps>) => {
|
|
44
|
+
return (
|
|
45
|
+
<Chart config={chartConfig} className="mx-auto aspect-square h-[250px]">
|
|
46
|
+
<RadialBarChart {...props.radialBarChart}>
|
|
47
|
+
<ChartTooltip
|
|
48
|
+
cursor={false}
|
|
49
|
+
content={<ChartTooltipContent hideLabel nameKey="browser" />}
|
|
50
|
+
/>
|
|
51
|
+
<RadialBar dataKey="visitors" {...props.radialBar} />
|
|
52
|
+
</RadialBarChart>
|
|
53
|
+
</Chart>
|
|
54
|
+
);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const LabeledRadialChartExample = (props: Partial<RadialBarChartProps>) => {
|
|
58
|
+
return (
|
|
59
|
+
<Chart config={chartConfig} className="mx-auto aspect-square h-[250px]">
|
|
60
|
+
<RadialBarChart {...props.radialBarChart}>
|
|
61
|
+
<ChartTooltip
|
|
62
|
+
cursor={false}
|
|
63
|
+
content={<ChartTooltipContent hideLabel nameKey="browser" />}
|
|
64
|
+
/>
|
|
65
|
+
<RadialBar dataKey="visitors" {...props.radialBar}>
|
|
66
|
+
<LabelList
|
|
67
|
+
position="insideStart"
|
|
68
|
+
dataKey="browser"
|
|
69
|
+
className="fill-white capitalize mix-blend-luminosity"
|
|
70
|
+
fontSize={11}
|
|
71
|
+
/>
|
|
72
|
+
</RadialBar>
|
|
73
|
+
</RadialBarChart>
|
|
74
|
+
</Chart>
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const GridRadialChartExample = (props: Partial<RadialBarChartProps>) => {
|
|
79
|
+
return (
|
|
80
|
+
<Chart config={chartConfig} className="mx-auto aspect-square h-[250px]">
|
|
81
|
+
<RadialBarChart {...props.radialBarChart}>
|
|
82
|
+
<ChartTooltip
|
|
83
|
+
cursor={false}
|
|
84
|
+
content={<ChartTooltipContent hideLabel nameKey="browser" />}
|
|
85
|
+
/>
|
|
86
|
+
<PolarGrid gridType="circle" />
|
|
87
|
+
<RadialBar dataKey="visitors" {...props.radialBar} />
|
|
88
|
+
</RadialBarChart>
|
|
89
|
+
</Chart>
|
|
90
|
+
);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const TextRadialChartExample = (props: Partial<RadialBarChartProps>) => {
|
|
94
|
+
const chartData = [{ browser: "safari", visitors: 200, fill: "var(--color-safari)" }];
|
|
95
|
+
const chartConfig = {
|
|
96
|
+
visitors: {
|
|
97
|
+
label: "Visitors",
|
|
98
|
+
},
|
|
99
|
+
safari: {
|
|
100
|
+
label: "Safari",
|
|
101
|
+
color: "hsl(var(--chart-2))",
|
|
102
|
+
},
|
|
103
|
+
} satisfies ChartConfig;
|
|
104
|
+
|
|
105
|
+
return (
|
|
106
|
+
<Chart config={chartConfig} className="mx-auto aspect-square h-[250px]">
|
|
107
|
+
<RadialBarChart {...props.radialBarChart}>
|
|
108
|
+
<PolarGrid
|
|
109
|
+
gridType="circle"
|
|
110
|
+
radialLines={false}
|
|
111
|
+
stroke="none"
|
|
112
|
+
className="first:fill-base-200 last:fill-base-100"
|
|
113
|
+
polarRadius={[86, 74]}
|
|
114
|
+
/>
|
|
115
|
+
<RadialBar dataKey="visitors" {...props.radialBar} />
|
|
116
|
+
<PolarRadiusAxis tick={false} tickLine={false} axisLine={false}>
|
|
117
|
+
<Label
|
|
118
|
+
content={({ viewBox }) => {
|
|
119
|
+
if (viewBox && "cx" in viewBox && "cy" in viewBox) {
|
|
120
|
+
return (
|
|
121
|
+
<text x={viewBox.cx} y={viewBox.cy} textAnchor="middle" dominantBaseline="middle">
|
|
122
|
+
<tspan
|
|
123
|
+
x={viewBox.cx}
|
|
124
|
+
y={viewBox.cy}
|
|
125
|
+
className="fill-foreground text-4xl font-bold"
|
|
126
|
+
>
|
|
127
|
+
{chartData[0].visitors.toLocaleString()}
|
|
128
|
+
</tspan>
|
|
129
|
+
<tspan x={viewBox.cx} y={(viewBox.cy || 0) + 24} className="fill-base-content">
|
|
130
|
+
Visitors
|
|
131
|
+
</tspan>
|
|
132
|
+
</text>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
}}
|
|
136
|
+
/>
|
|
137
|
+
</PolarRadiusAxis>
|
|
138
|
+
</RadialBarChart>
|
|
139
|
+
</Chart>
|
|
140
|
+
);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export const ShapeRadialChartExample = (props: Partial<RadialBarChartProps>) => {
|
|
144
|
+
const chartData = [{ browser: "safari", visitors: 1260, fill: "var(--color-safari)" }];
|
|
145
|
+
const chartConfig = {
|
|
146
|
+
visitors: {
|
|
147
|
+
label: "Visitors",
|
|
148
|
+
},
|
|
149
|
+
safari: {
|
|
150
|
+
label: "Safari",
|
|
151
|
+
color: "hsl(var(--chart-2))",
|
|
152
|
+
},
|
|
153
|
+
} satisfies ChartConfig;
|
|
154
|
+
|
|
155
|
+
return (
|
|
156
|
+
<Chart config={chartConfig} className="mx-auto aspect-square h-[250px]">
|
|
157
|
+
<RadialBarChart {...props.radialBarChart}>
|
|
158
|
+
<PolarGrid
|
|
159
|
+
gridType="circle"
|
|
160
|
+
radialLines={false}
|
|
161
|
+
stroke="none"
|
|
162
|
+
className="first:fill-base-200 last:fill-base-100"
|
|
163
|
+
polarRadius={[86, 74]}
|
|
164
|
+
/>
|
|
165
|
+
<RadialBar dataKey="visitors" {...props.radialBar} />
|
|
166
|
+
<PolarRadiusAxis tick={false} tickLine={false} axisLine={false}>
|
|
167
|
+
<Label
|
|
168
|
+
content={({ viewBox }) => {
|
|
169
|
+
if (viewBox && "cx" in viewBox && "cy" in viewBox) {
|
|
170
|
+
return (
|
|
171
|
+
<text x={viewBox.cx} y={viewBox.cy} textAnchor="middle" dominantBaseline="middle">
|
|
172
|
+
<tspan
|
|
173
|
+
x={viewBox.cx}
|
|
174
|
+
y={viewBox.cy}
|
|
175
|
+
className="fill-base-content text-4xl font-bold"
|
|
176
|
+
>
|
|
177
|
+
{chartData[0].visitors.toLocaleString()}
|
|
178
|
+
</tspan>
|
|
179
|
+
<tspan x={viewBox.cx} y={(viewBox.cy || 0) + 24} className="fill-base-content">
|
|
180
|
+
Visitors
|
|
181
|
+
</tspan>
|
|
182
|
+
</text>
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
}}
|
|
186
|
+
/>
|
|
187
|
+
</PolarRadiusAxis>
|
|
188
|
+
</RadialBarChart>
|
|
189
|
+
</Chart>
|
|
190
|
+
);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
export const StackedRadialChartExample = (props: Partial<RadialBarChartProps>) => {
|
|
194
|
+
const chartData = [{ month: "january", desktop: 1260, mobile: 570 }];
|
|
195
|
+
const chartConfig = {
|
|
196
|
+
desktop: {
|
|
197
|
+
label: "Desktop",
|
|
198
|
+
color: "hsl(var(--chart-1))",
|
|
199
|
+
},
|
|
200
|
+
mobile: {
|
|
201
|
+
label: "Mobile",
|
|
202
|
+
color: "hsl(var(--chart-2))",
|
|
203
|
+
},
|
|
204
|
+
} satisfies ChartConfig;
|
|
205
|
+
|
|
206
|
+
const totalVisitors = chartData[0].desktop + chartData[0].mobile;
|
|
207
|
+
|
|
208
|
+
return (
|
|
209
|
+
<Chart config={chartConfig} className="mx-auto aspect-square h-[250px]">
|
|
210
|
+
<RadialBarChart {...props.radialBarChart}>
|
|
211
|
+
<ChartTooltip cursor={false} content={<ChartTooltipContent hideLabel />} />
|
|
212
|
+
<PolarRadiusAxis tick={false} tickLine={false} axisLine={false}>
|
|
213
|
+
<Label
|
|
214
|
+
content={({ viewBox }) => {
|
|
215
|
+
if (viewBox && "cx" in viewBox && "cy" in viewBox) {
|
|
216
|
+
return (
|
|
217
|
+
<text x={viewBox.cx} y={viewBox.cy} textAnchor="middle">
|
|
218
|
+
<tspan
|
|
219
|
+
x={viewBox.cx}
|
|
220
|
+
y={(viewBox.cy || 0) - 16}
|
|
221
|
+
className="fill-foreground text-2xl font-bold"
|
|
222
|
+
>
|
|
223
|
+
{totalVisitors.toLocaleString()}
|
|
224
|
+
</tspan>
|
|
225
|
+
<tspan x={viewBox.cx} y={(viewBox.cy || 0) + 4} className="fill-base-content">
|
|
226
|
+
Visitors
|
|
227
|
+
</tspan>
|
|
228
|
+
</text>
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
}}
|
|
232
|
+
/>
|
|
233
|
+
</PolarRadiusAxis>
|
|
234
|
+
<RadialBar dataKey="desktop" fill="var(--color-desktop)" {...props.radialBar} />
|
|
235
|
+
<RadialBar dataKey="mobile" fill="var(--color-mobile)" {...props.radialBar} />
|
|
236
|
+
</RadialBarChart>
|
|
237
|
+
</Chart>
|
|
238
|
+
);
|
|
239
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Bar, BarChart, XAxis } from "recharts";
|
|
2
|
+
import type { ChartConfig } from "../../types";
|
|
3
|
+
import { Chart } from "../../Chart";
|
|
4
|
+
import { ChartTooltip, ChartTooltipContent } from "../../components";
|
|
5
|
+
import { type ChartTooltipContentProps } from "../../types";
|
|
6
|
+
|
|
7
|
+
const chartData = [
|
|
8
|
+
{ date: "2024-07-15", running: 450, swimming: 300 },
|
|
9
|
+
{ date: "2024-07-16", running: 380, swimming: 420 },
|
|
10
|
+
{ date: "2024-07-17", running: 520, swimming: 120 },
|
|
11
|
+
{ date: "2024-07-18", running: 140, swimming: 550 },
|
|
12
|
+
{ date: "2024-07-19", running: 600, swimming: 350 },
|
|
13
|
+
{ date: "2024-07-20", running: 480, swimming: 400 },
|
|
14
|
+
];
|
|
15
|
+
export const tooltipChartConfig = {
|
|
16
|
+
activities: {
|
|
17
|
+
label: "Activities",
|
|
18
|
+
},
|
|
19
|
+
running: {
|
|
20
|
+
label: "Running",
|
|
21
|
+
color: "hsl(var(--chart-3))",
|
|
22
|
+
},
|
|
23
|
+
swimming: {
|
|
24
|
+
label: "Swimming",
|
|
25
|
+
color: "hsl(var(--chart-2))",
|
|
26
|
+
},
|
|
27
|
+
} satisfies ChartConfig;
|
|
28
|
+
|
|
29
|
+
export const BarChartExample = (props: ChartTooltipContentProps) => {
|
|
30
|
+
return (
|
|
31
|
+
<Chart config={tooltipChartConfig} className="h-[250px] w-full">
|
|
32
|
+
<BarChart accessibilityLayer data={chartData}>
|
|
33
|
+
<XAxis
|
|
34
|
+
dataKey="date"
|
|
35
|
+
tickLine={false}
|
|
36
|
+
tickMargin={10}
|
|
37
|
+
axisLine={false}
|
|
38
|
+
tickFormatter={(value: string) => {
|
|
39
|
+
return new Date(value).toLocaleDateString("en-US", {
|
|
40
|
+
weekday: "short",
|
|
41
|
+
});
|
|
42
|
+
}}
|
|
43
|
+
/>
|
|
44
|
+
<Bar
|
|
45
|
+
isAnimationActive={false}
|
|
46
|
+
dataKey="running"
|
|
47
|
+
stackId="a"
|
|
48
|
+
fill="var(--color-running)"
|
|
49
|
+
radius={[0, 0, 4, 4]}
|
|
50
|
+
/>
|
|
51
|
+
<Bar
|
|
52
|
+
isAnimationActive={false}
|
|
53
|
+
dataKey="swimming"
|
|
54
|
+
stackId="a"
|
|
55
|
+
fill="var(--color-swimming)"
|
|
56
|
+
radius={[4, 4, 0, 0]}
|
|
57
|
+
/>
|
|
58
|
+
|
|
59
|
+
<ChartTooltip
|
|
60
|
+
content={<ChartTooltipContent {...props} />}
|
|
61
|
+
isAnimationActive={false}
|
|
62
|
+
cursor={false}
|
|
63
|
+
defaultIndex={1}
|
|
64
|
+
/>
|
|
65
|
+
</BarChart>
|
|
66
|
+
</Chart>
|
|
67
|
+
);
|
|
68
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { ArgTypes, StoryContext, StoryFn } from "@storybook/react";
|
|
2
|
+
import type { ComponentProps, ElementType } from "react";
|
|
3
|
+
|
|
4
|
+
export const categorizeArgs = (args: ArgTypes, category: string) => {
|
|
5
|
+
return Object.entries(args).reduce((acc, [name, arg]) => {
|
|
6
|
+
const categoryName = `${category} ${name}`;
|
|
7
|
+
acc[categoryName] = {
|
|
8
|
+
...arg,
|
|
9
|
+
name,
|
|
10
|
+
table: {
|
|
11
|
+
...arg.table,
|
|
12
|
+
category,
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
return acc;
|
|
16
|
+
}, {} as ArgTypes);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const getCategorizedArgs = <T,>(args: ArgTypes<T>) => {
|
|
20
|
+
return Object.entries(args).reduce((acc, [categoryName, value]) => {
|
|
21
|
+
const [category, name] = categoryName.split(" ");
|
|
22
|
+
acc[category] = {
|
|
23
|
+
...acc[category],
|
|
24
|
+
[name]: value,
|
|
25
|
+
};
|
|
26
|
+
return acc;
|
|
27
|
+
}, {} as ArgTypes);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const withArgs = <T extends object>(Component: StoryFn, context: StoryContext<T>) => {
|
|
31
|
+
context.args = getCategorizedArgs(context.args) as T;
|
|
32
|
+
|
|
33
|
+
return <Component />;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const convertStringToJSON = (str: string) => {
|
|
37
|
+
let newStr = str.replace(/'/g, '"');
|
|
38
|
+
newStr = newStr.replace(/(\w+)\s*:/g, '"$1":');
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
return JSON.parse(newStr) as Record<string, unknown>;
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
43
|
+
} catch (e) {
|
|
44
|
+
return str;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// A helper function to generate Args for a story based on ArgsTypes objects
|
|
49
|
+
export const getArgsFromArgTypes = <T extends ArgTypes>(argsTypes: T) => {
|
|
50
|
+
const args = (Object.keys(argsTypes) as (keyof T)[]).reduce(
|
|
51
|
+
(acc, key) => {
|
|
52
|
+
const defaultValue =
|
|
53
|
+
(argsTypes[key].defaultValue as string) ?? argsTypes[key].table?.defaultValue?.summary;
|
|
54
|
+
if (defaultValue) {
|
|
55
|
+
try {
|
|
56
|
+
acc[key] = convertStringToJSON(String(defaultValue));
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
58
|
+
} catch (_e) {
|
|
59
|
+
acc[key] = defaultValue;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return acc;
|
|
63
|
+
},
|
|
64
|
+
{} as Record<keyof T, unknown>
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
return args;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export const renderer = <T extends ElementType>(Component: T) => {
|
|
71
|
+
const component = <G,>(props: G) => <Component {...(props as ComponentProps<T>)} />;
|
|
72
|
+
component.displayName =
|
|
73
|
+
(Component as React.FC<unknown>).displayName || (Component as React.FC<unknown>).name;
|
|
74
|
+
return component;
|
|
75
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { ComponentProps, ComponentType, ReactNode } from "react";
|
|
2
|
+
import { LegendProps, ResponsiveContainer, Tooltip } from "recharts";
|
|
3
|
+
import { type Theme } from "../../utilities";
|
|
4
|
+
|
|
5
|
+
export type ChartConfig = {
|
|
6
|
+
[k in string]: {
|
|
7
|
+
label?: ReactNode;
|
|
8
|
+
icon?: ComponentType;
|
|
9
|
+
} & ({ color?: string; theme?: never } | { color?: never; theme: Record<Theme, string> });
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type ChartContextProps = {
|
|
13
|
+
config: ChartConfig;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type ChartProps = ComponentProps<"div"> & {
|
|
17
|
+
config: ChartConfig;
|
|
18
|
+
children: ComponentProps<typeof ResponsiveContainer>["children"];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type ChartStyleProps = {
|
|
22
|
+
id: string;
|
|
23
|
+
config: ChartConfig;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type ChartTooltipContentProps = ComponentProps<typeof Tooltip> &
|
|
27
|
+
ComponentProps<"div"> & {
|
|
28
|
+
hideLabel?: boolean;
|
|
29
|
+
hideIndicator?: boolean;
|
|
30
|
+
indicator?: "line" | "dot" | "dashed";
|
|
31
|
+
nameKey?: string;
|
|
32
|
+
labelKey?: string;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type ChartLegendContentProps = ComponentProps<"div"> &
|
|
36
|
+
Pick<LegendProps, "payload" | "verticalAlign"> & {
|
|
37
|
+
hideIcon?: boolean;
|
|
38
|
+
nameKey?: string;
|
|
39
|
+
};
|