Skip to content

The Open Standard for Generative UI

Area Chart

i-subhama9c32d620159
diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/AreaChart/AreaChart.tsx b/frontend-sdk/packages/react-ui/src/components/Charts/AreaChart/AreaChart.tsx
index e69de29bb..8b8f23b0a 100644
--- a/frontend-sdk/packages/react-ui/src/components/Charts/AreaChart/AreaChart.tsx
+++ b/frontend-sdk/packages/react-ui/src/components/Charts/AreaChart/AreaChart.tsx
@@ -0,0 +1,144 @@
+import React from "react";
+import { Area, LabelList, AreaChart as RechartsAreaChart, XAxis } from "recharts";
+import {
+ ChartConfig,
+ ChartContainer,
+ ChartLegend,
+ ChartLegendContent,
+ ChartTooltip,
+ ChartTooltipContent,
+} from "../Charts";
+import { cartesianGrid } from "../cartesianGrid";
+import { getDistributedColors, getPalette } from "../utils/PalletUtils";
+
+type AreaChartData = Array<Record<string, string | number>>;
+
+interface AreaChartProps<T extends AreaChartData> {
+ data: T;
+ categoryKey: keyof T[number];
+ theme?: "ocean" | "orchid" | "emerald" | "sunset" | "spectrum" | "vivid";
+ variant?: "linear" | "natural" | "step";
+ grid?: boolean;
+ label?: boolean;
+ legend?: boolean;
+ width?: number;
+ height?: number;
+ opacity?: number;
+ icons?: Partial<Record<keyof T[number], React.ComponentType>>;
+}
+
+export const AreaChart = <T extends AreaChartData>({
+ data,
+ categoryKey,
+ theme = "ocean",
+ variant = "natural",
+ grid = true,
+ label = true,
+ legend = true,
+ width = 800,
+ height = 400,
+ opacity = 0.5,
+ icons = {},
+}: AreaChartProps<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],
+ },
+ }),
+ {},
+ );
+
+ const getAxisAngle = (data: T) => {
+ return data.length <= 6 ? 0 : -90;
+ };
+ const getTickMargin = (data: T) => {
+ return data.length <= 6 ? 10 : 15;
+ };
+
+ return (
+ <ChartContainer config={chartConfig}>
+ <RechartsAreaChart
+ accessibilityLayer
+ width={width}
+ height={height}
+ data={data}
+ margin={{
+ top: label ? 30 : 20,
+ }}
+ >
+ {grid && cartesianGrid()}
+ <XAxis
+ dataKey={categoryKey as string}
+ tickLine={false}
+ tickMargin={getTickMargin(data)}
+ axisLine={false}
+ angle={getAxisAngle(data)}
+ textAnchor="middle"
+ tickFormatter={(value) => value.slice(0, 4)}
+ />
+
+ <ChartTooltip content={<ChartTooltipContent indicator="dot" />} />
+ {dataKeys.map((key) => {
+ const color = `var(--color-${key})`;
+ if (label) {
+ return (
+ <Area
+ key={key}
+ dataKey={key}
+ type={variant}
+ stroke={color}
+ fill={color}
+ fillOpacity={opacity}
+ stackId="a"
+ dot={{
+ fill: color,
+ }}
+ activeDot={{
+ r: 6,
+ }}
+ >
+ {label && (
+ <LabelList
+ position="top"
+ offset={12}
+ className="crayon-chart-label-list"
+ fontSize={12}
+ />
+ )}
+ </Area>
+ );
+ }
+ return (
+ <Area
+ key={key}
+ dataKey={key}
+ type={variant}
+ stroke={color}
+ fill={color}
+ fillOpacity={opacity}
+ stackId="a"
+ dot={{
+ fill: color,
+ }}
+ activeDot={{
+ r: 6,
+ }}
+ />
+ );
+ })}
+ {legend && <ChartLegend content={<ChartLegendContent />} />}
+ </RechartsAreaChart>
+ </ChartContainer>
+ );
+};
diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/BarChart/BarChart.tsx b/frontend-sdk/packages/react-ui/src/components/Charts/BarChart/BarChart.tsx
index 6c63045cd..b4b4a0684 100644
--- a/frontend-sdk/packages/react-ui/src/components/Charts/BarChart/BarChart.tsx
+++ b/frontend-sdk/packages/react-ui/src/components/Charts/BarChart/BarChart.tsx
@@ -90,12 +90,13 @@ export const BarChart = <T extends BarChartData>({
<ChartTooltip content={<ChartTooltipContent />} />
{dataKeys.map((key) => {
+ const color = `var(--color-${key})`;
if (label) {
return (
<Bar
key={key}
dataKey={key}
- fill={`var(--color-${key})`}
+ fill={color}
radius={radius}
stackId={variant === "stacked" ? "a" : undefined}
>
@@ -114,7 +115,7 @@ export const BarChart = <T extends BarChartData>({
<Bar
key={key}
dataKey={key}
- fill={`var(--color-${key})`}
+ fill={color}
radius={radius}
stackId={variant === "stacked" ? "a" : undefined}
/>
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 50a1ca6b9..4fb90a3eb 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
@@ -7,6 +7,7 @@ import { Card } from "../../Card";
import { Footer } from "../../Footer/Footer";
import { Header } from "../../Header";
import { IconButton } from "../../IconButton";
+import { AreaChart } from "../AreaChart/AreaChart";
import { BarChart } from "../BarChart";
const chartData = [
@@ -18,6 +19,15 @@ const chartData = [
{ month: "June", desktop: 214, mobile: 140 },
];
+const areaChartData = [
+ { month: "January", desktop: 150, mobile: 90 },
+ { month: "February", desktop: 280, mobile: 180 },
+ { month: "March", desktop: 220, mobile: 140 },
+ { month: "April", desktop: 180, mobile: 160 },
+ { month: "May", desktop: 250, mobile: 120 },
+ { month: "June", desktop: 300, mobile: 180 },
+];
+
const icons = {
desktop: Monitor,
mobile: TabletSmartphone,
@@ -36,7 +46,7 @@ const BarChartComponent = () => {
<BarChart
data={chartData}
categoryKey="month"
- theme="spectrum"
+ theme="sunset"
icons={icons}
variant="stacked"
/>
@@ -47,6 +57,32 @@ const BarChartComponent = () => {
);
};
+const AreaChartComponent = () => {
+ return (
+ <Card style={{ width: "500px" }}>
+ <Header
+ actions={[
+ <IconButton variant="tertiary" key="download-btn" size="small" icon={<TrendingUp />} />,
+ ]}
+ title="Area Chart - Multiple"
+ subtitle="January - June 2024"
+ />
+ <AreaChart
+ data={areaChartData}
+ categoryKey="month"
+ theme="ocean"
+ icons={icons}
+ variant="natural"
+ width={460}
+ height={300}
+ />
+ <Footer>
+ <Button iconRight={<Download />}>Download</Button>
+ </Footer>
+ </Card>
+ );
+};
+
const meta = {
title: "Components/Charts/BarChart",
component: BarChartComponent,
@@ -59,14 +95,12 @@ const meta = {
export default meta;
type Story = StoryObj<typeof meta>;
-export const Default: Story = {};
+export const BarChartStory: Story = {
+ name: "Bar Chart",
+ render: () => <BarChartComponent />,
+};
-export const CustomData: Story = {
- args: {
- data: [
- { month: "July", desktop: 250, mobile: 150 },
- { month: "August", desktop: 320, mobile: 180 },
- { month: "September", desktop: 280, mobile: 160 },
- ],
- },
+export const AreaChartStory: Story = {
+ name: "Area Chart",
+ render: () => <AreaChartComponent />,
};