Skip to content

Commit 1b07633

Browse files
authored
feat: add bar charts (#2924)
* feat: line chart pocs * fix: lint * chore: pie charts * feat: add area charts * feat: add line chart * refactor: charts and base components * chore: code refactor * fix: line chart styling and offset colors * fix: legend color in recharts * chore: update legend styles * fix: add color in reference line * chore: update color tokens * chore: add lable in chart * fix: spacing * fix: add custom label * feat: add colors * feat: support new colors in line charts & colors update * chore: update color logic & exmaple color values * chore: update blade color theme * refactor: reference line props * fix: more color props * fix: line chart errors * fix: ts errors * chore: more update * chore: more style changes * chore: change vars * feat: add line charts * chore: update cursor values * chore: revert changes * chore: update spacing * feat: inital code for area charts * chore: area charts * chore: code refactor * chore: refactor responsive container * chore: added more example * chore: update types * chore: make x and y optional * chore: add x and y props * chore: update area charts * feat: initial commit * chore: add spacing b/w charts * fix: gap b/w bar charts * chore: update example * chore: remove cursor from tooltip * fix: make animation work * chore: update final review * feat: update color theme token * feat: expose color theme * feat: update line charts * fix: update snaps * chore: add review notes * fix: change to vairables * chore: more changes * Create calm-peas-work.md * chore: more code changes * fix: update name * feat: update area charts to support color tokens * feat: update bar charts for review * chore: errors in charts * chore: update animation token and colors * fea: update bar charts * Revert "feat: add area charts" (#2936) * chore: update change set * review: update export types * chore: update example * fix: update baseChart components * chore: update forecasted data * chore: add changeset * fix: add bar chart * chore: remove logs from bar chart * refactor: bar charts * fix: add area chart types * feat: add test cases * feat: add bar chart * feat: add changset * fix: update bar chart * fix: opacity * fix: bar chart review * fix: bar chart review * chore: update mcp docs * chore: resolve review comments * fix:linting issue * chore: update lint * chore: rename chartBarwrapper * fix: update opacity * fix: set animation easing to linear * chore: export types * feat: update chart layout * chore: rename to charts * chore: rename story * chore: update barchart stories * fix: add animation * fix: animation speed * fix: animation logic and stacked bar logic * chore: more animation fixes * fix: update animation * chore: update snaps * fix: ts issue * chore: add stacking * refactor: code animation * chore: update snaps * chore: remove animation :( * review: change orientation to layout * chore: update layout * chore: update types
1 parent 8c32d5a commit 1b07633

File tree

21 files changed

+2651
-20
lines changed

21 files changed

+2651
-20
lines changed

.changeset/fluffy-impalas-raise.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@razorpay/blade': minor
3+
---
4+
5+
feat(blade): add BarChart component
6+
7+
[Docs Link](https://blade.razorpay.com/?path=/docs/components-charts-barchart--docs)

.changeset/yellow-lamps-sneeze.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@razorpay/blade-mcp': minor
3+
---
4+
5+
feat(blade-mcp): update knowledgebase with BarChart

packages/blade-mcp/knowledgebase/components/AreaChart.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type ChartAreaWrapperProps = {
4343
children?: React.ReactNode;
4444
colorTheme?: colorTheme;
4545
data: data[];
46-
};
46+
} & BoxProps;
4747

4848
type ChartReferenceLineProps = {
4949
/**
@@ -136,4 +136,4 @@ function BasicAreaChart() {
136136
</Box>
137137
);
138138
}
139-
```
139+
```
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# BarChart
2+
3+
## Component Name
4+
5+
BarChart
6+
7+
## Description
8+
9+
BarChart is a comprehensive data visualization component that renders interactive bar charts with support for grouped, stacked, and vertical layouts. It provides customizable colors, animations, and interactive features like hover states and tooltips. The component is built on top of Recharts and integrates seamlessly with Blade's design system, offering consistent styling and accessibility features. BarChart supports multiple data series, custom color themes, and various chart configurations for displaying categorical data effectively.
10+
11+
## Important Constraints
12+
13+
- `ChartBarWrapper` component only accepts `ChartBar`, `ChartXAxis`, `ChartYAxis`, `ChartCartesianGrid`, `ChartTooltip`, `ChartLegend`, and `ChartReferenceLine` components as children.
14+
- `data` prop is required and must be an array of objects with consistent data structure
15+
- `dataKey` prop is required for each `ChartBar` component and must correspond to a property in the data array
16+
- `stackId` must be consistent across all bars that should be stacked together
17+
- `layout="vertical"` requires `ChartXAxis` to have `type="number"` and `ChartYAxis` to have `type="category"`
18+
- Color tokens must follow the exact format: `chart.background.categorical.{color}.{emphasis}` or `chart.background.sequential.{color}.{number}`
19+
20+
## TypeScript Types
21+
22+
These types define the props that the BarChart component and its subcomponents accept:
23+
24+
```typescript
25+
type ChartBarProps = Omit<RechartsBarProps, 'fill' | 'dataKey' | 'name' | 'label' | 'activeBar'> & {
26+
/**
27+
* The data key of the bar chart.
28+
*/
29+
dataKey: RechartsBarProps['dataKey'];
30+
/**
31+
* The name of the bar chart.
32+
*/
33+
name?: RechartsBarProps['name'];
34+
/**
35+
* The color of the bar chart.
36+
*/
37+
color?: ChartsCategoricalColorToken | ChartSequentialColorToken;
38+
/**
39+
* The stack id of the bar chart.
40+
*/
41+
stackId?: RechartsBarProps['stackId'];
42+
/**
43+
* The active bar of the bar chart.
44+
*/
45+
activeBar?: RechartsBarProps['activeBar'];
46+
/**
47+
* The label of the bar chart.
48+
*/
49+
label?: RechartsBarProps['label'];
50+
/**
51+
* The show legend of the bar chart.
52+
*/
53+
showLegend?: boolean;
54+
};
55+
56+
type data = {
57+
[key: string]: unknown;
58+
};
59+
60+
type ChartBarWrapperProps = {
61+
children?: React.ReactNode;
62+
/**
63+
* The color theme of the bar chart.
64+
*/
65+
colorTheme?: colorTheme;
66+
/**
67+
* The orientation of the bar chart.
68+
*/
69+
layout?: 'horizontal' | 'vertical';
70+
/**
71+
* Chart data to be rendered
72+
*/
73+
data: data[];
74+
} & BoxProps;
75+
76+
77+
type ChartsCategoricalColorToken = `chart.background.categorical.${ChartColorCategories}.${keyof ChartCategoricalEmphasis}`;
78+
79+
type ChartSequentialColorToken = `chart.background.sequential.${Exclude<ChartColorCategories, 'gray'>}.${keyof ChartSequentialEmphasis}`;
80+
81+
type colorTheme = 'default';
82+
83+
type ChartXAxisProps = Omit<RechartsXAxisProps, 'tick' | 'label' | 'dataKey' | 'stroke'> & {
84+
/**
85+
* The label of the x-axis.
86+
*/
87+
label?: string;
88+
/**
89+
* The data key of the x-axis.
90+
*/
91+
dataKey?: string;
92+
};
93+
94+
type ChartYAxisProps = Omit<RechartsYAxisProps, 'tick' | 'label' | 'dataKey' | 'stroke'> & {
95+
/**
96+
* The label of the y-axis.
97+
*/
98+
label?: string;
99+
/**
100+
* The data key of the y-axis.
101+
*/
102+
dataKey?: string;
103+
};
104+
105+
type ChartTooltipProps = ComponentProps<typeof RechartsTooltip>;
106+
107+
type ChartLegendProps = ComponentProps<typeof RechartsLegend>;
108+
109+
type ChartCartesianGridProps = Omit<RechartsCartesianGridProps, 'strokeDasharray' | 'verticalFill' | 'horizontalFill'>;
110+
111+
type ChartReferenceLineProps = {
112+
/**
113+
* The y-coordinate of the reference line.
114+
*/
115+
y?: RechartsReferenceLineProps['y'];
116+
/**
117+
* The x-coordinate of the reference line.
118+
*/
119+
x?: RechartsReferenceLineProps['x'];
120+
/**
121+
* The label of the reference line.
122+
*/
123+
label: string;
124+
};
125+
```
126+
127+
## Example
128+
129+
### Basic BarChart with Multiple Series
130+
131+
```tsx
132+
import React from 'react';
133+
import {
134+
ChartBar,
135+
ChartBarWrapper,
136+
ChartXAxis,
137+
ChartYAxis,
138+
ChartCartesianGrid,
139+
ChartTooltip,
140+
ChartLegend,
141+
} from '@razorpay/blade/components';
142+
143+
const salesData = [
144+
{ month: 'Jan', revenue: 4000, profit: 2000, expenses: 1000 },
145+
{ month: 'Feb', revenue: 3000, profit: 1500, expenses: 800 },
146+
{ month: 'Mar', revenue: 5000, profit: 3000, expenses: 1200 },
147+
{ month: 'Apr', revenue: 4500, profit: 2500, expenses: 1100 },
148+
];
149+
150+
const BasicBarChart = () => {
151+
return (
152+
<div style={{ width: '100%', height: '400px' }}>
153+
<ChartBarWrapper data={salesData}>
154+
<ChartCartesianGrid />
155+
<ChartXAxis dataKey="month" />
156+
<ChartYAxis />
157+
<ChartTooltip />
158+
<ChartLegend />
159+
<ChartBar
160+
dataKey="revenue"
161+
name="Revenue"
162+
color="chart.background.categorical.azure.moderate"
163+
/>
164+
<ChartBar
165+
dataKey="profit"
166+
name="Profit"
167+
color="chart.background.categorical.emerald.moderate"
168+
/>
169+
<ChartBar
170+
dataKey="expenses"
171+
name="Expenses"
172+
color="chart.background.categorical.crimson.moderate"
173+
/>
174+
</ChartBarWrapper>
175+
</div>
176+
);
177+
};
178+
```

packages/blade-mcp/knowledgebase/components/LineChart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type ChartLineWrapperProps = {
7777
*/
7878
data: data[];
7979
children: React.ReactNode;
80-
} & Partial<Omit<BaseBoxProps, keyof FlexboxProps | keyof GridProps>>;
80+
} & BoxProps;
8181

8282
type ChartReferenceLineProps = {
8383
/**

packages/blade/src/components/Charts/AreaChart/AreaChart.stories.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const Page = (): React.ReactElement => {
2121
componentName="AreaChart"
2222
componentDescription="An Area Chart component built on top of Recharts with Blade design system styling."
2323
apiDecisionLink={
24-
'https://github.com/razorpay/blade/blob/5920fbd32c70793454f8c8c6ff544b2a7413afb5/packages/blade/src/components/Charts/_decisions/decisions.md'
24+
'https://github.com/razorpay/blade/blob/master/packages/blade/src/components/Charts/_decisions/decisions.md'
2525
}
2626
figmaURL="https://www.figma.com/design/jubmQL9Z8V7881ayUD95ps/Blade-DSL?node-id=92678-188717&p=f&m=dev"
2727
>
@@ -252,6 +252,14 @@ export const StackedAreaChart: StoryFn<typeof ChartArea> = ({
252252
color="chart.background.categorical.azure.moderate"
253253
{...args}
254254
/>
255+
<ChartArea
256+
dataKey={dataKey}
257+
name={name}
258+
type="monotone"
259+
stackId="1"
260+
color="chart.background.categorical.crimson.moderate"
261+
{...args}
262+
/>
255263
</ChartAreaWrapper>
256264
</Box>
257265
);

packages/blade/src/components/Charts/AreaChart/types.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import type { AreaProps as RechartAreaProps } from 'recharts';
22
import type { ChartsCategoricalColorToken } from '../CommonChartComponents/types';
33
import type { colorTheme } from '../utils';
4-
import type {
5-
BaseBoxProps,
6-
FlexboxProps,
7-
GridProps,
8-
} from '~components/Box/BaseBox/types/propsTypes';
4+
import type { BoxProps } from '~components/Box';
95

106
type ChartAreaProps = {
117
/**
@@ -68,6 +64,6 @@ type ChartAreaWrapperProps = {
6864
* Chart data to be rendered
6965
*/
7066
data: data[];
71-
} & Partial<Omit<BaseBoxProps, keyof FlexboxProps | keyof GridProps>>;
67+
} & BoxProps;
7268

7369
export type { ChartAreaProps, ChartAreaWrapperProps };
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react';
2+
import type { ChartBarProps, ChartBarWrapperProps } from './types';
3+
import { Text } from '~components/Typography';
4+
import { throwBladeError } from '~utils/logger';
5+
6+
const ChartBar = (_prop: ChartBarProps): React.ReactElement => {
7+
throwBladeError({
8+
message: 'ChartBar is not yet implemented for native',
9+
moduleName: 'ChartBar',
10+
});
11+
12+
return <Text>ChartBar is not available for Native mobile apps.</Text>;
13+
};
14+
15+
const ChartBarWrapper = (_prop: ChartBarWrapperProps): React.ReactElement => {
16+
throwBladeError({
17+
message: 'ChartBarWrapper is not yet implemented for native',
18+
moduleName: 'ChartBarWrapper',
19+
});
20+
21+
return <Text>ChartLine is not available for Native mobile apps.</Text>;
22+
};
23+
24+
export type { ChartBarProps, ChartBarWrapperProps };
25+
export { ChartBar, ChartBarWrapper };

0 commit comments

Comments
 (0)