The Open Standard for Generative UI
Pie Chart Component
i-subham406d6aaae444
diff --git a/frontend-sdk/packages/react-ui/src/components/Card/stories/card.stories.tsx b/frontend-sdk/packages/react-ui/src/components/Card/stories/card.stories.tsxindex 54f796c07..fc135cbe1 100644--- a/frontend-sdk/packages/react-ui/src/components/Card/stories/card.stories.tsx+++ b/frontend-sdk/packages/react-ui/src/components/Card/stories/card.stories.tsx@@ -1,12 +1,12 @@import type { Meta, StoryObj } from "@storybook/react";import { FormControl, Label } from "../../FormControl";import "../../FormControl/formControl.scss";+import { Header } from "../../Header";+import "../../Header/header.scss";import { Input } from "../../Input";import "../../Input/input.scss";import { Card } from "../Card";import "../card.scss";-import { Header } from "../../Header";-import "../../Header/header.scss";const meta: Meta<typeof Card> = {title: "Components/Card",diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/PieChart.tsx b/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/PieChart.tsxnew file mode 100644index 000000000..6267d25a1--- /dev/null+++ b/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/PieChart.tsx@@ -0,0 +1,122 @@+import { Cell, Pie, PieChart as RechartsPieChart } from "recharts";+import {+ ChartConfig,+ ChartContainer,+ ChartLegend,+ ChartLegendContent,+ ChartTooltip,+ ChartTooltipContent,+} from "../Charts";+import { getDistributedColors, getPalette } from "../utils/PalletUtils";++type PieChartData = Array<Record<string, string | number>>;++interface PieChartProps<T extends PieChartData> {+ data: T;+ categoryKey: keyof T[number];+ dataKey: keyof T[number];+ theme?: "ocean" | "orchid" | "emerald" | "sunset" | "spectrum" | "vivid";+ variant?: "pie" | "donut";+ format?: "percentage" | "number";+ legend?: boolean;+ label?: boolean;+ outerRadius?: number;+ innerRadius?: number;+ width?: number;+ height?: number;+}++// Helper function to calculate percentage+const calculatePercentage = (value: number, total: number): number => {+ if (total === 0) {+ return 0;+ }+ return Number(((value / total) * 100).toFixed(2));+};++export const PieChart = <T extends PieChartData>({+ data,+ categoryKey,+ dataKey,+ theme = "ocean",+ variant = "pie",+ format = "number",+ legend = true,+ label = true,+ outerRadius = 100,+ innerRadius = variant === "donut" ? 60 : 0,+ width = 800,+ height = 400,+}: PieChartProps<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],+ },+ }),+ {},+ );++ // Custom label renderer+ const renderCustomLabel = ({ payload, cx, cy, x, y, textAnchor, dominantBaseline }: any) => {+ if (payload.percentage <= 10) return null;+ const displayValue = format === "percentage" ? payload.percentage : payload[dataKey];+ const formattedValue =+ String(displayValue).length > 7 ? `${String(displayValue).slice(0, 7)}...` : displayValue;++ return (+ <g>+ <text+ cx={cx}+ cy={cy}+ x={x}+ y={y}+ textAnchor={textAnchor}+ dominantBaseline={dominantBaseline}+ >+ {formattedValue}+ {format === "percentage" ? "%" : ""}+ </text>+ </g>+ );+ };++ return (+ <ChartContainer config={chartConfig} className="crayon-pie-chart-container">+ <RechartsPieChart width={width} height={height}>+ <ChartTooltip content={<ChartTooltipContent showPercentage={format === "percentage"} />} />+ {legend && <ChartLegend content={<ChartLegendContent nameKey={String(categoryKey)} />} />}+ <Pie+ data={transformedData}+ dataKey={format === "percentage" ? "percentage" : String(dataKey)}+ nameKey={String(categoryKey)}+ labelLine={false}+ outerRadius={outerRadius}+ innerRadius={innerRadius}+ label={label ? renderCustomLabel : false}+ >+ {transformedData.map((_, index) => (+ <Cell key={`cell-${index}`} fill={colors[index % colors.length]} />+ ))}+ </Pie>+ </RechartsPieChart>+ </ChartContainer>+ );+};diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/index.ts b/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/index.tsnew file mode 100644index 000000000..223a80128--- /dev/null+++ b/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/index.ts@@ -0,0 +1 @@+export * from "./PieChart";diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/pieChart.scss b/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/pieChart.scssnew file mode 100644index 000000000..5f8686431--- /dev/null+++ b/frontend-sdk/packages/react-ui/src/components/Charts/PieChart/pieChart.scss@@ -0,0 +1,10 @@+@use "sass:map";+@use "../../../cssUtils.scss" as cssUtils;++.crayon-pie-chart {+ &-container {+ min-height: 400px;+ margin-left: auto;+ margin-right: auto;+ }+}diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/charts.scss b/frontend-sdk/packages/react-ui/src/components/Charts/charts.scssindex c5ad746e1..9c44f2e3e 100644--- a/frontend-sdk/packages/react-ui/src/components/Charts/charts.scss+++ b/frontend-sdk/packages/react-ui/src/components/Charts/charts.scss@@ -5,7 +5,7 @@// Container styles&-container {display: flex;- aspect-ratio: 16 / 9;+ aspect-ratio: 1/1;justify-content: center;font-size: 12px;line-height: 16px;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.tsxindex b7f65fabf..1e8cf653e 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@@ -1,7 +1,5 @@import type { Meta, StoryObj } from "@storybook/react";import { Download, Monitor, TabletSmartphone, TrendingUp } from "lucide-react";-import "../charts.scss";-import { Button } from "../../Button";import { Card } from "../../Card";import { Footer } from "../../Footer/Footer";@@ -9,7 +7,10 @@ import { Header } from "../../Header";import { IconButton } from "../../IconButton";import { AreaChart } from "../AreaChart";import { BarChart } from "../BarChart";+import "../charts.scss";import { LineChart } from "../LineChart";+import { PieChart } from "../PieChart";+import "../PieChart/pieChart.scss";import { RadarChart } from "../RadarChart";const chartData = [{ month: "January", desktop: 186, mobile: 80 },@@ -47,6 +48,15 @@ const radarChartData = [{ month: "June", desktop: 214, mobile: 140 },];+const pieChartData = [+ { 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,@@ -147,7 +157,7 @@ const RadarChartComponent = () => {icons={icons}variant="area"width={460}- height={300}+ height={400}strokeWidth={3}areaOpacity={1}/>@@ -157,6 +167,32 @@ const RadarChartComponent = () => {</Card>);};++const PieChartComponent = () => {+ 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"+ />+ <PieChart+ data={pieChartData}+ categoryKey="month"+ dataKey="desktop"+ theme="vivid"+ format="percentage"+ variant="donut"+ />+ <Footer>+ <Button iconRight={<Download />}>Download</Button>+ </Footer>+ </Card>+ );+};+const meta = {title: "Components/Charts/BarChart",component: BarChartComponent,@@ -188,3 +224,8 @@ export const RadarChartStory: Story = {name: "Radar Chart",render: () => <RadarChartComponent />,};++export const PieChartStory: Story = {+ name: "Pie Chart",+ render: () => <PieChartComponent />,+};