Skip to content

The Open Standard for Generative UI

Radial Chart

i-subham975ee8aaeb19
diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/RadialChart/RadialChart.tsx b/frontend-sdk/packages/react-ui/src/components/Charts/RadialChart/RadialChart.tsx
new file mode 100644
index 000000000..eccd7072a
--- /dev/null
+++ b/frontend-sdk/packages/react-ui/src/components/Charts/RadialChart/RadialChart.tsx
@@ -0,0 +1,132 @@
+import { Cell, LabelList, PolarGrid, RadialBar, RadialBarChart } from "recharts";
+import {
+ ChartConfig,
+ ChartContainer,
+ ChartLegend,
+ ChartLegendContent,
+ ChartTooltip,
+ ChartTooltipContent,
+} from "../Charts";
+import { getDistributedColors, getPalette } from "../utils/PalletUtils";
+
+type RadialChartData = Array<Record<string, string | number>>;
+
+interface RadialChartProps<T extends RadialChartData> {
+ data: T;
+ categoryKey: keyof T[number];
+ dataKey: keyof T[number];
+ theme?: "ocean" | "orchid" | "emerald" | "sunset" | "spectrum" | "vivid";
+ variant?: "semicircle" | "circular";
+ format?: "percentage" | "number";
+ legend?: boolean;
+ label?: boolean;
+ grid?: boolean;
+ outerRadius?: number;
+ innerRadius?: number;
+ width?: number;
+ height?: number;
+}
+
+// Helper function to calculate percentage (same as PieChart)
+const calculatePercentage = (value: number, total: number): number => {
+ if (total === 0) return 0;
+ return Number(((value / total) * 100).toFixed(2));
+};
+
+export const RadialChart = <T extends RadialChartData>({
+ data,
+ categoryKey,
+ dataKey,
+ theme = "ocean",
+ variant = "semicircle",
+ format = "number",
+ legend = true,
+ label = true,
+ grid = true,
+ outerRadius = 120,
+ innerRadius = 30,
+ width = 800,
+ height = 400,
+}: RadialChartProps<T>) => {
+ // Calculate total for percentage calculations
+ const total = data.reduce((sum, item) => sum + Number(item[dataKey]), 0);
+
+ // Transform data with percentages
+ const transformedData = data.map((item) => ({
+ ...item,
+ percentage: calculatePercentage(Number(item[dataKey as string]), total),
+ originalValue: item[dataKey as string],
+ }));
+
+ // Get color palette and distribute colors
+ const palette = getPalette(theme);
+ const colors = getDistributedColors(palette, data.length);
+
+ // Create chart configuration
+ const chartConfig = data.reduce<ChartConfig>(
+ (config, item, index) => ({
+ ...config,
+ [String(item[categoryKey])]: {
+ label: String(item[categoryKey as string]),
+ color: colors[index],
+ },
+ }),
+ {},
+ );
+
+ const getFontSize = (dataLength: number) => {
+ return dataLength <= 5 ? 12 : 7;
+ };
+
+ return (
+ <ChartContainer config={chartConfig} className="crayon-radial-chart-container">
+ <RadialBarChart
+ width={width}
+ height={height}
+ data={transformedData}
+ startAngle={variant === "circular" ? -90 : 0}
+ endAngle={variant === "circular" ? 270 : 180}
+ innerRadius={innerRadius}
+ outerRadius={outerRadius}
+ >
+ {grid && <PolarGrid gridType="circle" />}
+ <ChartTooltip
+ cursor={false}
+ content={
+ <ChartTooltipContent
+ showPercentage={format === "percentage"}
+ nameKey={String(categoryKey)}
+ />
+ }
+ />
+ {legend && (
+ <ChartLegend
+ content={
+ <ChartLegendContent nameKey={String(categoryKey)} className="flex-wrap gap-2" />
+ }
+ />
+ )}
+ <RadialBar
+ dataKey={format === "percentage" ? "percentage" : String(dataKey)}
+ background={!grid}
+ cornerRadius={10}
+ >
+ {Object.entries(chartConfig).map(([key, config]) => (
+ <Cell key={key} fill={config.color} />
+ ))}
+ {label && (
+ <LabelList
+ dataKey={String(categoryKey)}
+ position="insideStart"
+ offset={12}
+ fill="#000000"
+ className="capitalize"
+ fontSize={getFontSize(data.length)}
+ formatter={(value: string) => (value.length > 8 ? `${value.slice(0, 8)}...` : value)}
+ />
+ )}
+ </RadialBar>
+ </RadialBarChart>
+ </ChartContainer>
+ );
+};
diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/RadialChart/index.ts b/frontend-sdk/packages/react-ui/src/components/Charts/RadialChart/index.ts
new file mode 100644
index 000000000..d5578ec7c
--- /dev/null
+++ b/frontend-sdk/packages/react-ui/src/components/Charts/RadialChart/index.ts
@@ -0,0 +1 @@
+export * from "./RadialChart";
diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/stories/charts.stories.tsx b/frontend-sdk/packages/react-ui/src/components/Charts/stories/charts.stories.tsx
index 1e8cf653e..bf677d6f7 100644
--- a/frontend-sdk/packages/react-ui/src/components/Charts/stories/charts.stories.tsx
+++ b/frontend-sdk/packages/react-ui/src/components/Charts/stories/charts.stories.tsx
@@ -12,6 +12,8 @@ import { LineChart } from "../LineChart";
import { PieChart } from "../PieChart";
import "../PieChart/pieChart.scss";
import { RadarChart } from "../RadarChart";
+import { RadialChart } from "../RadialChart";
+
const chartData = [
{ month: "January", desktop: 186, mobile: 80 },
{ month: "February", desktop: 305, mobile: 200 },
@@ -57,6 +59,15 @@ const pieChartData = [
{ month: "June", desktop: 214 },
];
+const radialChartData = [
+ { month: "January", desktop: 186 },
+ { month: "February", desktop: 305 },
+ { month: "March", desktop: 237 },
+ { month: "April", desktop: 73 },
+ { month: "May", desktop: 209 },
+ { month: "June", desktop: 214 },
+];
+
const icons = {
desktop: Monitor,
mobile: TabletSmartphone,
@@ -193,6 +204,32 @@ const PieChartComponent = () => {
);
};
+const RadialChartComponent = () => {
+ return (
+ <Card style={{ width: "500px" }}>
+ <Header
+ actions={[
+ <IconButton variant="tertiary" key="download-btn" size="small" icon={<TrendingUp />} />,
+ ]}
+ title="Pie Chart - Multiple"
+ subtitle="January - June 2024"
+ />
+ <RadialChart
+ data={pieChartData}
+ categoryKey="month"
+ dataKey="desktop"
+ theme="vivid"
+ format="percentage"
+ variant="circular"
+ width={460}
+ height={400}
+ />
+ <Footer>
+ <Button iconRight={<Download />}>Download</Button>
+ </Footer>
+ </Card>
+ );
+};
const meta = {
title: "Components/Charts/BarChart",
component: BarChartComponent,
@@ -229,3 +266,8 @@ export const PieChartStory: Story = {
name: "Pie Chart",
render: () => <PieChartComponent />,
};
+
+export const RadialChartStory: Story = {
+ name: "Radial Chart",
+ render: () => <RadialChartComponent />,
+};