库似乎不支持用 FlexibleXYPlot 或 XYPlot 包装径向图(请参阅问题Responsive RadialChart "react-vis": "^1.7.7",从未解决,我无法让它工作),在 RadialChart 图表的顶部需要放置宽度和高度。
现在基于以下问题,FlexibleXYPlot 需要高度道具,已制作 pr 来解决此问题,但从未合并。基于Alex Sandiiarov 实现,可以使用以下代码使饼图响应:
import React, { Component } from 'react';
import ResizeObserver from 'resize-observer-polyfill';
import { RadialChart } from 'react-vis';
class App extends Component {
constructor(props) {
super(props);
this.state = {
height: 0,
width: 0
};
}
componentDidMount() {
//When mounted set our start size
this.setSize();
//Lets observe changes with resize-observer-polyfill
this.observer = new ResizeObserver(entries => this.setSize());
//Lets observe our node
this.observer.observe(this.node);
}
componentWillUnmount() {
this.observer.disconnect();
}
setSize = () => {
const { height, width } = this.node.getBoundingClientRect();
this.setState({ height, width });
};
render() {
const { height, width } = this.state;
return (
<div
ref={node => {
if (node) {
this.node = node;
}
}}
style={{ width: '100vw', height: '100vh' }}
>
<RadialChart
className={'donut-chart-example'}
getAngle={d => d.theta}
data={[
{ theta: 2, className: 'custom-class' },
{ theta: 6 },
{ theta: 2 },
{ theta: 3 },
{ theta: 1 }
]}
width={width}
height={height}
padAngle={0.04}
colorType="literal"
/>
</div>
);
}
}
export default App;
希望这可以帮助您或为您指明有用的方向:)