在反应中动态更新 Highcharts 图表
Dynamically update Highcharts chart in react
我在 react-redux 中使用 highcharts-react-official 来创建下钻图表。
然而,当我点击一个栏进行向下钻取时,我还更新了一些导致组件重新呈现的道具 - 这似乎阻止了向下钻取事件。
我从 中了解到我应该使用 shouldComponentUpdate 和 getChart() 来防止重新渲染,而是动态更新数据。
我的问题是 getChart() 似乎不适用于官方 highcharts 反应包。我收到 Uncaught TypeError: this.refs.chart.getChart is not a function
有没有我打算用来获取和动态更新图表的替代方法?或者我可以看一些例子?
这里只包括 render 和 shouldComponentUpdate 部分:
shouldComponentUpdate(nextProps, nextState) {
let chart = this.refs.chart.getChart();
//dynamically update data using nextProps
return false;
}
render () {
const options = {
chart: {
type: 'column',
height: 300,
events: {
drillup: (e) => {this.drilledUp(e)}
}
},
plotOptions: {
series: {
events:{
click: (e) => {this.categoryClicked(e)}
}
}
},
xAxis: {type: "category"},
yAxis: {title: {text: 'Amount Spent ($)'}},
series: [{
name: 'weekly spending',
showInLegend: false,
data: this.props.transactionChartData.series_data,
cursor: 'pointer',
events: {
click: (e)=> {this.weekSelected(e)}
}
}],
drilldown: {
series: this.props.transactionChartData.drilldown_data
}
};
return (
<HighchartsReact
highcharts={Highcharts}
options={options}
ref="chart"
/>
)
}
在 highcharts-react-official v2.0.0
中添加了 allowChartUpdate
选项,这对您的情况应该很有效。通过使用此选项,您可以通过更新组件来阻止更新图表:
categoryClicked() {
this.allowChartUpdate = false;
this.setState({
...
});
}
...
<HighchartsReact
ref={"chartComponent"}
allowChartUpdate={this.allowChartUpdate}
highcharts={Highcharts}
options={...}
/>
此外,要获取图表实例,请使用 refs
:
componentDidMount(){
const chart = this.refs.chartComponent.chart;
}
// react native functional
import React, {useState, useRef,useLayoutEffect} from "react"
import HighchartsReactNative from '@highcharts/highcharts-react-native'
function chartcomponent(props){
const [options, setoptions] = useState({});
const chartRef = useRef(null);
useEffect(() => {
// create the options for your chart.
setoptions({chart:{}, yaxis:{}, xAxis:{},})// etc.
}, []);
useLayoutEffect(() => {
// new point to add, you can get new data via props, fetch, socket, etc.
var x = new Date(hr.timestamp).getTime();
var y = 10
// these are the important parts here:
var series = chartRef.current.props.options.series;
if (!Array.isArray(series)){return;}
var seriesdata = series[0].data;
seriesdata.push([x,y]);
// next line limits points in chart to 10, so new poitn replaces first point
if (seriesdata.length>10){seriesdata.splice(0,1);}
return () => {};
}, [props]);
return(
<View style={styles.charting}><HighchartsReactNative styles={{height:300, width:600}} options={options} ref={chartRef} ></HighchartsReactNative></View>
)
}
我在 react-redux 中使用 highcharts-react-official 来创建下钻图表。
然而,当我点击一个栏进行向下钻取时,我还更新了一些导致组件重新呈现的道具 - 这似乎阻止了向下钻取事件。
我从
我的问题是 getChart() 似乎不适用于官方 highcharts 反应包。我收到 Uncaught TypeError: this.refs.chart.getChart is not a function
有没有我打算用来获取和动态更新图表的替代方法?或者我可以看一些例子?
这里只包括 render 和 shouldComponentUpdate 部分:
shouldComponentUpdate(nextProps, nextState) {
let chart = this.refs.chart.getChart();
//dynamically update data using nextProps
return false;
}
render () {
const options = {
chart: {
type: 'column',
height: 300,
events: {
drillup: (e) => {this.drilledUp(e)}
}
},
plotOptions: {
series: {
events:{
click: (e) => {this.categoryClicked(e)}
}
}
},
xAxis: {type: "category"},
yAxis: {title: {text: 'Amount Spent ($)'}},
series: [{
name: 'weekly spending',
showInLegend: false,
data: this.props.transactionChartData.series_data,
cursor: 'pointer',
events: {
click: (e)=> {this.weekSelected(e)}
}
}],
drilldown: {
series: this.props.transactionChartData.drilldown_data
}
};
return (
<HighchartsReact
highcharts={Highcharts}
options={options}
ref="chart"
/>
)
}
在 highcharts-react-official v2.0.0
中添加了 allowChartUpdate
选项,这对您的情况应该很有效。通过使用此选项,您可以通过更新组件来阻止更新图表:
categoryClicked() {
this.allowChartUpdate = false;
this.setState({
...
});
}
...
<HighchartsReact
ref={"chartComponent"}
allowChartUpdate={this.allowChartUpdate}
highcharts={Highcharts}
options={...}
/>
此外,要获取图表实例,请使用 refs
:
componentDidMount(){
const chart = this.refs.chartComponent.chart;
}
// react native functional
import React, {useState, useRef,useLayoutEffect} from "react"
import HighchartsReactNative from '@highcharts/highcharts-react-native'
function chartcomponent(props){
const [options, setoptions] = useState({});
const chartRef = useRef(null);
useEffect(() => {
// create the options for your chart.
setoptions({chart:{}, yaxis:{}, xAxis:{},})// etc.
}, []);
useLayoutEffect(() => {
// new point to add, you can get new data via props, fetch, socket, etc.
var x = new Date(hr.timestamp).getTime();
var y = 10
// these are the important parts here:
var series = chartRef.current.props.options.series;
if (!Array.isArray(series)){return;}
var seriesdata = series[0].data;
seriesdata.push([x,y]);
// next line limits points in chart to 10, so new poitn replaces first point
if (seriesdata.length>10){seriesdata.splice(0,1);}
return () => {};
}, [props]);
return(
<View style={styles.charting}><HighchartsReactNative styles={{height:300, width:600}} options={options} ref={chartRef} ></HighchartsReactNative></View>
)
}