我已经用梅柱图创建了一个图表,

import { BarChart } from '@mui/x-charts/BarChart';
import { ThemeProvider, createTheme, useTheme } from '@mui/material/styles';

我想让wine 吧变成圆角.我try 了SX属性和Series属性的borderRadius.但这并没有奏效.SX属性在图表周围创建边框,而不是 for each 条形图创建边框. 到目前为止,我有以下代码:

 <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>

and it looks like this: enter image description here

我怎么才能让wine 吧看起来像这样的圆角:

enter image description here

推荐答案

因为MUI Bar Chart的底层条元素是SVG rect元素,所以您将不能应用CSS border-radius.但您可以提供您自己的定制Bar component,它不呈现rect元素,而是呈现为圆角矩形1,并通过slotsprops 传递该组件.

例如:

<BarChart
  ...
  layout="horizontal"
  slots={{
    bar: (props) => {
      const radius = 7;
      const {x, y, height, width, ownerState, ...restProps} = props
      
      // Path of a rectangle with rounded corners on the right
      // for horizontal layout
      const d = `M${x},${y} h${width - radius} a${radius},${radius} 0 0 1 ${radius},${radius}v ${height - 2 * radius} a${radius},${radius} 0 0 1 ${-radius},${radius} h${radius - width}z`
      return <path d={d} fill={ownerState.color}
      {...restProps} />
    }
  }}
    />

这将产生:

example bar chart with rounded corners

(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您也可以创建仍使用rect并为圆角提供clipPath的组件,但我认为这是更简单的选项.

参考资料:

Reactjs相关问答推荐

react 下拉菜单任务

使用下一步中的路由/导航不会立即加载路由

Next.js Next-Auth登录函数重定向到http://localhost:3000/api/auth/error

无法使用 Suspense 和 Await 获取数据

React useContext 的未定义返回值

组件安装后调用自定义钩子

useEffect 内的 onClick 和 addEventListener 之间的 click 事件顺序差异

在一个事件处理程序中进行多次调度是个好主意吗?

解决在React测试库中的act()警告

React 应用程序可以嵌入到 PowerPoint 中吗?

如何在屏幕上使用相同的可重用

React Native应用如何订阅Supabase的实时数据?

我如何在所有组件中使用 Chakra UI toast?

react 事件顺序

如何使用 UseState 正确测试组件

如何在 JSX 中使用 -webkit- 前缀?

react 路由dom没有路由匹配位置错误/在路由中制作组件

为什么重新渲染不会影响 setTimeout 的计数?

在 React 中使用使用状态挂钩合并两个数组

警告:您在没有 `onChange` 处理程序的情况下为表单字段提供了 `value` 属性