React Victory 给出错误 Invalid prop `data` of type `object` provided to `VictoryLabel`, expected `array`

React Victory giving error Invalid prop `data` of type `object` supplied to `VictoryLabel`, expected `array`

我在 React 中使用 Victory 图表,我对发生的事情感到非常困惑。我收到以下错误:

Invalid prop `data` of type `object` supplied to `VictoryLabel`, expected `array`

我的很简单,像这样:

public render() {
  return (
    <vic.VictoryChart>
      <vic.VictoryBar
        data={[
          { x: 0, y: 100 },
          { x: 1, y: 150 },
          { x: 2, y: 200 },
          { x: 3, y: 50 },
          { x: 4, y: 500 },
        ]}
        labelComponent={
          <vic.VictoryLabel text={d => d.y} />
        }
      />
    </vic.VictoryChart>
  );
}

有人可以告诉我这里的问题吗?我不知道为什么会收到错误消息!

胜利文档说:

The labelComponent prop takes a component instance which will be used to render labels for the component

您仍然需要使用 VictoryBarlabels 属性单独定义标签。 labelComponent 的目的是进一步自定义定位或添加工具提示标签等内容。

来自文档的示例:

<VictoryBar
  data={sampleData}
  labels={(d) => d.y}
  style={{ labels: { fill: "white" } }}
  labelComponent={<VictoryLabel dy={30}/>}
/>

这里 labelComponent 道具用于移动每个标签的 y-axis 但我们仍然必须使用单独的道具定义标签值。希望这对您有所帮助!