Skip to main content

Bar Chart

The BarChart component is a flexible, customizable bar chart built with recharts. It supports horizontal or vertical layout, stacked bars, custom labels, tooltips, legends, and border radius for bars.

import { BarChart } from "tawuniya/components";

👉 View in Storybook

Props​

PropTypeDefaultDescription
dataT[]-Array of data objects.
seriesChartConfig-Defines each bar series with color, name, etc.
categoryKeykeyof T & string-Key used for the category axis (x-axis for vertical, y-axis for horizontal).
horizontalbooleanfalseIf true, chart is rendered vertically.
showCartesianGridbooleanfalseShow the grid lines.
tickMarginnumber10Margin between ticks and axis.
showXAxisbooleantrueDisplay the X-axis.
showYAxisbooleantrueDisplay the Y-axis.
hideTooltipLabelbooleanfalseHide the label in tooltips.
showCursorbooleanfalseShow a cursor line when hovering.
stackedbooleanfalseStack multiple series bars.
labelListLabelListItem[][]List of labels to display on bars with position, fontSize, offset, and custom class.
withLegendbooleanfalseShow chart legend.
borderRadiusnumber | [number, number, number, number]8Border radius of bars. Can be a single number or array for each corner.
marginPartial<Margin>{ top: 20, right: 30, left: 20, bottom: 5 }Chart margin.
classNamestring-Custom class for the chart container.
wrapperClassNamestring-Custom class for the outer wrapper.

Usage​

Basic Example​

const data = [
{ category: "Jan", mobile: 40, desktop: 30 },
{ category: "Feb", mobile: 35, desktop: 45 },
];

const series = {
mobile: { color: "var(--color-primary)", name: "Mobile" },
desktop: { color: "var(--color-success)", name: "Desktop" },
};

<BarChart data={data} series={series} categoryKey="category" />;

Horizontal Bars​

<BarChart
data={data}
series={series}
categoryKey="category"
horizontal
/>;

Stacked Bars with Labels​

<BarChart
data={data}
series={series}
categoryKey="category"
stacked
labelList={[{ position: "top", fontSize: 12 }]}
/>;

With Legend​

<BarChart
data={data}
series={series}
categoryKey="category"
withLegend
/>;

Custom Border Radius​

<BarChart
data={data}
series={series}
categoryKey="category"
borderRadius={[8, 8, 0, 0]}
/>;

With Cartesian Grid and Tooltip​

<BarChart
data={data}
series={series}
categoryKey="category"
showCartesianGrid
showCursor
/>;

Features​

  • Supports vertical or horizontal layout.
  • Supports stacked and grouped bars.
  • Fully customizable tooltips, legends, and labels.
  • Customizable bar border radius.
  • Optional Cartesian grid.
  • Smooth integration with TailwindCSS via className.

Notes​

  • labelList allows multiple labels on each bar.
  • borderRadius works differently for stacked vs. non-stacked bars.
  • series order determines rendering order and stacking sequence.