Skip to main content

Grouped Bar Chart

A grouped bar chart is used to compare values across different categories, with bars grouped together for subcategories. This chart is effective for showing relationships between two categorical variables and a quantitative variable.

Installation

The GroupedBarChart component is part of the @rexora/charts package. You can install it using npm or yarn:

bash
npm install @rexora/charts
# or
yarn add @rexora/charts

Usage

Here's a basic example of how to use the GroupedBarChart component:

tsx
import React from 'react';
import { GroupedBarChart } from '@rexora/charts';

const data = [
{ category: 'A', subcategory: 'X', value: 20 },
{ category: 'A', subcategory: 'Y', value: 25 },
{ category: 'B', subcategory: 'X', value: 15 },
{ category: 'B', subcategory: 'Y', value: 30 },
];

const MyGroupedBarChart = () => {
return (
<div>
<h2>Grouped Bar Chart Example</h2>
<GroupedBarChart data={data} width={600} height={400} />
</div>
);
};

export default MyGroupedBarChart;

Props and Options

The GroupedBarChart component accepts the following props:

Prop NameTypeDescriptionDefault
dataArray<Object>The data to be visualized. Each object should contain properties for the main category, subcategory, and value.[]
widthnumberThe width of the chart in pixels.auto
heightnumberThe height of the chart in pixels.auto
optionsObjectAn object for additional configuration options. (Details below){}

Options Object:

Option NameTypeDescriptionDefault
marginObjectMargins for the chart (top, right, bottom, left).{ top: 20, right: 30, bottom: 40, left: 40 }
categoryFieldstringThe field name in the data for the main category.'category'
subcategoryFieldstringThe field name in the data for the subcategory.'subcategory'
valueFieldstringThe field name in the data for the value.'value'
colorsArray<string>An array of colors to use for the subcategories.D3 default color scheme
xAxisLabelstringLabel for the x-axis.''
yAxisLabelstringLabel for the y-axis.''
tooltipbooleanWhether to enable tooltips on hover.true
tooltipFormatter(d: Object) => stringA function to format the tooltip content.default formatter

Development

The GroupedBarChart component is built using React and D3. You can find the source code in packages/charts/src/charts/GroupedBarChart.tsx. Feel free to contribute or customize it to your needs.