Skip to content

The Open Standard for Generative UI

Rader Chart

i-subham1a7b8398824c
diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/RaderChart/RaderChart.tsx b/frontend-sdk/packages/react-ui/src/components/Charts/RaderChart/RaderChart.tsx
new file mode 100644
index 000000000..ee41ebc93
--- /dev/null
+++ b/frontend-sdk/packages/react-ui/src/components/Charts/RaderChart/RaderChart.tsx
@@ -0,0 +1,101 @@
+import React from "react";
+import { PolarAngleAxis, PolarGrid, Radar, RadarChart as RechartsRadarChart } from "recharts";
+import {
+ ChartConfig,
+ ChartContainer,
+ ChartLegend,
+ ChartLegendContent,
+ ChartTooltip,
+ ChartTooltipContent,
+} from "../Charts";
+import { getDistributedColors, getPalette } from "../utils/PalletUtils";
+
+type RaderChartData = Array<Record<string, string | number>>;
+
+interface RaderChartProps<T extends RaderChartData> {
+ data: T;
+ categoryKey: keyof T[number];
+ theme?: "ocean" | "orchid" | "emerald" | "sunset" | "spectrum" | "vivid";
+ variant?: "line" | "area";
+ grid?: boolean;
+ legend?: boolean;
+ width?: number;
+ height?: number;
+ strokeWidth?: number;
+ areaOpacity?: number;
+ icons?: Partial<Record<keyof T[number], React.ComponentType>>;
+}
+
+export const RaderChart = <T extends RaderChartData>({
+ data,
+ categoryKey,
+ theme = "ocean",
+ variant = "line",
+ grid = true,
+ legend = true,
+ width = 800,
+ height = 400,
+ strokeWidth = 2,
+ areaOpacity = 0.5,
+ icons = {},
+}: RaderChartProps<T>) => {
+ // excluding the categoryKey
+ const dataKeys = Object.keys(data[0] || {}).filter((key) => key !== categoryKey);
+
+ const palette = getPalette(theme);
+ const colors = getDistributedColors(palette, dataKeys.length);
+
+ // Create Config
+ const chartConfig: ChartConfig = dataKeys.reduce(
+ (config, key, index) => ({
+ ...config,
+ [key]: {
+ label: key,
+ icon: icons[key],
+ color: colors[index],
+ },
+ }),
+ {},
+ );
+
+ return (
+ <ChartContainer config={chartConfig}>
+ <RechartsRadarChart
+ width={width}
+ height={height}
+ data={data}
+ margin={{ top: 10, right: 10, bottom: 10, left: 10 }}
+ >
+ {grid && <PolarGrid className="crayon-chart-polar-grid" stroke="currentColor" />}
+ <PolarAngleAxis dataKey={categoryKey as string} />
+
+ <ChartTooltip cursor={false} content={<ChartTooltipContent indicator="line" />} />
+ {dataKeys.map((key) => {
+ const color = `var(--color-${key})`;
+ if (variant === "line") {
+ return (
+ <Radar
+ key={key}
+ dataKey={key}
+ fill={color}
+ fillOpacity={0}
+ stroke={color}
+ strokeWidth={strokeWidth}
+ />
+ );
+ } else {
+ return <Radar key={key} dataKey={key} fill={color} fillOpacity={areaOpacity} />;
+ }
+ })}
+
+ {legend && (
+ <ChartLegend
+ content={
+ <ChartLegendContent className="-translate-y-2 flex-wrap gap-2 [&>*]:basis-1/4 [&>*]:justify-center" />
+ }
+ />
+ )}
+ </RechartsRadarChart>
+ </ChartContainer>
+ );
+};
diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/RaderChart/index.ts b/frontend-sdk/packages/react-ui/src/components/Charts/RaderChart/index.ts
new file mode 100644
index 000000000..55779ccf4
--- /dev/null
+++ b/frontend-sdk/packages/react-ui/src/components/Charts/RaderChart/index.ts
@@ -0,0 +1 @@
+export * from "./RaderChart";
diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/charts.scss b/frontend-sdk/packages/react-ui/src/components/Charts/charts.scss
index ba255d290..c5ad746e1 100644
--- a/frontend-sdk/packages/react-ui/src/components/Charts/charts.scss
+++ b/frontend-sdk/packages/react-ui/src/components/Charts/charts.scss
@@ -212,6 +212,10 @@
stroke: map.get(cssUtils.$colors, "strokes", "default");
}
+.crayon-chart-polar-grid {
+ stroke: map.get(cssUtils.$colors, "strokes", "default");
+}
+
.crayon-chart-label-list {
fill: map.get(cssUtils.$colors, "fills", "secondary");
}
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 f5e1090ec..4d9d27bd3 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
@@ -10,6 +10,7 @@ import { IconButton } from "../../IconButton";
import { AreaChart } from "../AreaChart";
import { BarChart } from "../BarChart";
import { LineChart } from "../LineChart";
+import { RaderChart } from "../RaderChart";
const chartData = [
{ month: "January", desktop: 186, mobile: 80 },
{ month: "February", desktop: 305, mobile: 200 },
@@ -37,6 +38,15 @@ const lineChartData = [
{ month: "June", desktop: 214, mobile: 140 },
];
+const radarChartData = [
+ { month: "January", desktop: 186, mobile: 80 },
+ { month: "February", desktop: 305, mobile: 200 },
+ { month: "March", desktop: 237, mobile: 120 },
+ { month: "April", desktop: 73, mobile: 190 },
+ { month: "May", desktop: 209, mobile: 130 },
+ { month: "June", desktop: 214, mobile: 140 },
+];
+
const icons = {
desktop: Monitor,
mobile: TabletSmartphone,
@@ -120,6 +130,33 @@ const LineChartComponent = () => {
);
};
+const RaderChartComponent = () => {
+ return (
+ <Card style={{ width: "500px" }}>
+ <Header
+ actions={[
+ <IconButton variant="tertiary" key="download-btn" size="small" icon={<TrendingUp />} />,
+ ]}
+ title="Rader Chart - Multiple"
+ subtitle="January - June 2024"
+ />
+ <RaderChart
+ data={radarChartData}
+ categoryKey="month"
+ theme="vivid"
+ icons={icons}
+ variant="area"
+ width={460}
+ height={300}
+ strokeWidth={3}
+ areaOpacity={1}
+ />
+ <Footer>
+ <Button iconRight={<Download />}>Download</Button>
+ </Footer>
+ </Card>
+ );
+};
const meta = {
title: "Components/Charts/BarChart",
component: BarChartComponent,
@@ -146,3 +183,8 @@ export const LineChartStory: Story = {
name: "Line Chart",
render: () => <LineChartComponent />,
};
+
+export const RaderChartStory: Story = {
+ name: "Rader Chart",
+ render: () => <RaderChartComponent />,
+};