The Open Standard for Generative UI
Line Chart Component
i-subham9a0f94bfd768
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.tsxindex 8b8f23b0a..e01d80f36 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@@ -74,7 +74,9 @@ export const AreaChart = <T extends AreaChartData>({height={height}data={data}margin={{- top: label ? 30 : 20,+ top: label ? 20 : 10,+ left: 12,+ right: 12,}}>{grid && cartesianGrid()}diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/AreaChart/index.ts b/frontend-sdk/packages/react-ui/src/components/Charts/AreaChart/index.tsindex e69de29bb..da5fa4e47 100644--- a/frontend-sdk/packages/react-ui/src/components/Charts/AreaChart/index.ts+++ b/frontend-sdk/packages/react-ui/src/components/Charts/AreaChart/index.ts@@ -0,0 +1 @@+export * from "./AreaChart";diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/LineChart/LineChart.tsx b/frontend-sdk/packages/react-ui/src/components/Charts/LineChart/LineChart.tsxnew file mode 100644index 000000000..124ca4ff3--- /dev/null+++ b/frontend-sdk/packages/react-ui/src/components/Charts/LineChart/LineChart.tsx@@ -0,0 +1,140 @@+import React from "react";+import { LabelList, Line, LineChart as RechartsLineChart, XAxis } from "recharts";+import {+ ChartConfig,+ ChartContainer,+ ChartLegend,+ ChartLegendContent,+ ChartTooltip,+ ChartTooltipContent,+} from "../Charts";+import { cartesianGrid } from "../cartesianGrid";+import { getDistributedColors, getPalette } from "../utils/PalletUtils";++type LineChartData = Array<Record<string, string | number>>;++interface LineChartProps<T extends LineChartData> {+ 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;+ strokeWidth?: number;+ icons?: Partial<Record<keyof T[number], React.ComponentType>>;+}++export const LineChart = <T extends LineChartData>({+ data,+ categoryKey,+ theme = "ocean",+ variant = "natural",+ grid = true,+ label = true,+ legend = true,+ width = 800,+ height = 400,+ strokeWidth = 2,+ icons = {},+}: LineChartProps<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}>+ <RechartsLineChart+ accessibilityLayer+ width={width}+ height={height}+ data={data}+ margin={{+ top: label ? 20 : 10,+ left: 12,+ right: 12,+ }}+ >+ {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="line" />} />+ {dataKeys.map((key) => {+ const color = `var(--color-${key})`;+ if (label) {+ return (+ <Line+ key={key}+ dataKey={key}+ type={variant}+ stroke={color}+ fill={color}+ strokeWidth={strokeWidth}+ dot={{+ fill: color,+ }}+ activeDot={{+ r: 6,+ }}+ >+ {label && (+ <LabelList+ position="top"+ offset={12}+ className="crayon-chart-label-list"+ fontSize={12}+ />+ )}+ </Line>+ );+ }+ return (+ <Line+ key={key}+ dataKey={key}+ type={variant}+ stroke={color}+ fill={color}+ strokeWidth={strokeWidth}+ dot={{+ fill: color,+ }}+ />+ );+ })}+ {legend && <ChartLegend content={<ChartLegendContent />} />}+ </RechartsLineChart>+ </ChartContainer>+ );+};diff --git a/frontend-sdk/packages/react-ui/src/components/Charts/LineChart/index.ts b/frontend-sdk/packages/react-ui/src/components/Charts/LineChart/index.tsnew file mode 100644index 000000000..e873ee101--- /dev/null+++ b/frontend-sdk/packages/react-ui/src/components/Charts/LineChart/index.ts@@ -0,0 +1 @@+export * from "./LineChart";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 d68ef26ad..f5e1090ec 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,9 +7,9 @@ import { Card } from "../../Card";import { Footer } from "../../Footer/Footer";import { Header } from "../../Header";import { IconButton } from "../../IconButton";-import { AreaChart } from "../AreaChart/AreaChart";+import { AreaChart } from "../AreaChart";import { BarChart } from "../BarChart";-+import { LineChart } from "../LineChart";const chartData = [{ month: "January", desktop: 186, mobile: 80 },{ month: "February", desktop: 305, mobile: 200 },@@ -28,6 +28,15 @@ const areaChartData = [{ month: "June", desktop: 300, mobile: 180 },];+const lineChartData = [+ { 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,@@ -84,6 +93,33 @@ const AreaChartComponent = () => {);};+const LineChartComponent = () => {+ return (+ <Card style={{ width: "500px" }}>+ <Header+ actions={[+ <IconButton variant="tertiary" key="download-btn" size="small" icon={<TrendingUp />} />,+ ]}+ title="Line Chart - Multiple"+ subtitle="January - June 2024"+ />+ <LineChart+ data={lineChartData}+ categoryKey="month"+ theme="emerald"+ icons={icons}+ variant="step"+ width={460}+ height={300}+ strokeWidth={3}+ />+ <Footer>+ <Button iconRight={<Download />}>Download</Button>+ </Footer>+ </Card>+ );+};+const meta = {title: "Components/Charts/BarChart",component: BarChartComponent,@@ -105,3 +141,8 @@ export const AreaChartStory: Story = {name: "Area Chart",render: () => <AreaChartComponent />,};++export const LineChartStory: Story = {+ name: "Line Chart",+ render: () => <LineChartComponent />,+};