apps/www/content/docs/charts/bar-chart.mdx
import { Chart, useChart } from "@chakra-ui/charts"
import { Bar, BarChart, CartesianGrid, XAxis, YAxis } from "recharts"
<Chart.Root>
<BarChart>
<CartesianGrid />
<XAxis />
<YAxis />
<Bar />
</BarChart>
</Chart.Root>
Here's an example of coloring the bars based on the data.
Use the
shapeprop on theBarcomponent to customize the fill color for each bar.
<Bar
dataKey="allocation"
shape={(props) => <Rectangle {...props} fill="red" />}
/>
Render the LabelList component from recharts to display the label of the
bar.
Use the formatter provided from the useChart hook to format the value axis.
<YAxis
tickFormatter={chart.formatNumber({
style: "currency",
currency: "USD",
notation: "compact",
})}
/>
To remove the gap between the bars, set the barCategoryGap prop to 0 on the
BarChart component.
Compose the LabelList and shape prop to render bars upward or downward based
on the value.
<Bar
dataKey="views"
shape={(props) => (
<Rectangle
{...props}
fill={chart.color(props.payload.views > 0 ? "green.solid" : "red.solid")}
/>
)}
>
<LabelList dataKey="views" position="top" />
</Bar>
Pass the layout="vertical" prop to the BarChart component to render the bars
horizontally.
Pass the radius prop to the Bar component to render the bars with rounded
corners.
Passing an array of values to the dataKey prop will render a range bar that
indicates the lower and upper bounds of the values.
const chart = useChart({
data: [
{ name: "UK", value: [10, 20] },
// ...
],
})
Render multiple Bar components to create a bar chart with multiple series.
Pass the layout prop to the Legend component from recharts to configure
the position of the legend.
Set the stackOffset prop to expand on the BarChart component to render the
bars with value normalized to 100%.
Render multiple Bar components and set their stackId prop to the same value
to stack them.
Render multiple Bar components with different stackId props to create a bar
chart with some series stacked and some not.
Use the ReferenceLine component from recharts to make reference to a
specific value on the chart.
For those mathematics wiz, you can compose the barchart to create a histogram.
<ExampleTabs name="charts/bar-chart-histogram" />Here's an example of rendering images as the XAxis tick by leveraging svg
foreignObject.
Combine the bar chart with the ErrorBar and Bar components to create a
candlestick chart.
Here's an example of composing the BarChart, Card and SegmentGroup
components.