I have created a chart using MUI X barchart,
import { BarChart } from '@mui/x-charts/BarChart';
import { ThemeProvider, createTheme, useTheme } from '@mui/material/styles';
and I want to give the bars rounded corners. I tried borderRadius for sx property and series property. but it didn't work. the sx property creates a border around the chart not for every bar. I have the following code so far:
<ThemeProvider theme={darkTheme} >
<BarChart
sx={[{p: 5, border: 1, borderRadius: 3}]}
yAxis={[
{
id: 'barCategories',
data: ['A', 'B', 'C', 'D', 'E'],
scaleType: 'band',
},
]}
series={[
{
data: [50, 20, 85, 60, 20],
color: '#fff',
},
]}
layout="horizontal"
/>
</ThemeProvider>
How can I make the bars looks rounded corners like this:
Because the underlying bar element for MUI Bar Chart is an SVG rect element, you will not be able to apply CSS
border-radius
. But you can provide your own customBar
component that, instead of rendering arect
element, renders apath
element appearing as a rectangle with rounded corners1 and pass that component via theslots
prop.For example:
Which produces:
(I would memo this for better performance -- this demo is just to get you rolling.)
Working CodeSanbox: https://codesandbox.io/s/mui-bar-chart-with-rounded-corders-right-zms2m5?file=/Demo.js
1 You could also create a component that still uses a
rect
and provide aclipPath
for the rounded corners, but IMO this is the simpler option.References:
BarPlotSlotComponentProps