[在react-highcharts
此处添加解决方案,因为在 Google 上搜索“react-highcharts shrink”时,这会作为第一个 SO 解决方案弹出]
因为react-highcharts
您可以简单地在窗口调整大小事件监听器中调用 React 组件的 forceUpdate() 方法。它将触发 Highcharts 重新绘制到新的更小的区域。另请参阅使用 React 重新渲染浏览器调整大小的视图
注意:我的代码flex-auto
在父元素和 Highcharts DOM 元素中使用(参见 CSS 灵活框布局)进行了测试。知道这是否也适用于其他布局会很有趣。
import React from 'react';
import Highcharts from 'react-highcharts';
...
export default class Chart extends React.Component {
constructor(props) {
...
// install callbacks
this.resize = () => {
// throttle resize events as redraw is expensive
if (this.timeout)
return;
this.timeout = setTimeout(
() => {
// force Highcharts to always adhere
// to changes in the view area
this.forceUpdate();
this.timeout = undefined;
},
50); // milliseconds
};
...
}
...
componentDidMount() {
...
// listen to window resize events
window.addEventListener('resize', this.resize);
...
}
...
componentWillUnmount() {
...
// remove listener when component goes away
window.removeEventListener('resize', this.resize);
if (this.timeout) {
clearTimeout(this.timeout);
this.timeout = undefined;
}
...
}
...
render() {
...
return (
<Highcharts
config={...}
domProps={{
className: 'd-flex flex-auto',
...
}}
...
/>
);
}
...
}