React Nivo 虚线

fra*_*003 3 css charts reactjs nivo-react

我正在尝试创建一个 React nivo 折线图,用虚线而不是实线。我研究过图案,但不知道如何制作。任何帮助表示赞赏。

Mic*_*ung 8

nivo 在库中提供了自定义图层功能,您可以利用它来自定义从实线到虚线的线条

这是我为您制作的代码和框。

https://codesandbox.io/s/wonderful-lumiere-ouhwv?file=/src/components/LineChart.js

在ResponsiveLine的layers属性中包含自定义图层

<ResponsiveLine
   ...
   layers={[ ..., DashedSolidLine] }
/>
Run Code Online (Sandbox Code Playgroud)

自定义路径样式

const DashedSolidLine = ({ series, lineGenerator, xScale, yScale }) => {
  return series.map(({ id, data, color }, index) => (
    <path
      ...
      style={
        index % 2 === 0
          ? {
              // simulate line will dash stroke when index is even
              strokeDasharray: "3, 6",
              strokeWidth: 3
            }
          : {
              // simulate line with solid stroke
              strokeWidth: 1
            }
      }
  />
  ));
};
Run Code Online (Sandbox Code Playgroud)