如何在 react-chartjs-2 中更新图表?
How to Update chart in react-chartjs-2?
我调用了update()
函数,但是它不起作用。
TypeError: line.update is not a function.
为什么 update()
不是函数?
我在 http://jsfiddle.net/zpnx8ppb/26/ 上看到了这个例子,更新功能确实有效
这是我的代码:
import React, { Component } from 'react';
import { Line } from 'react-chartjs-2';
import Chart from 'chart.js';
const line = {
labels: [],
datasets: [
{
label: 'My First dataset',
fill: false,
data: []
}
]
};
setInterval(function(){
line.labels.push(Math.floor(Math.random() * 100));
line.datasets[0].data.push(Math.floor(Math.random() * 100));
line.update();
}, 5000);
class LineChart extends Component {
render() {
return (
<div className="chart">
<Line
data={this.state}
height={5}
width={20}
/>
</div>
)
}
}
export default LineChart;
您需要更新图表,线只是图表上的配置设置,此更新需要流回处理程序
为了让您走上正确的道路,下面是我的意思的一个例子
var config = {};
class Chart extends Component {
constructor() {
super();
this.ctx = document.getElementById(this._rootNodeID).getContext("2d");
this.chart = new Chart(ctx, config);
}
changeHandler(value) {
this.chart.update();
}
render() {
return (
<canvas id={this._rootNodeID}>
<LineChart value={this.state.value}
config={this.config}
onChange={this.changeHandler}/>
</canvas>
);
}
}
const line = {
labels: [],
datasets: [
{
label: 'My First dataset',
fill: false,
data: []
}
]
};
class LineChart extends Component {
constructor(props) {
super(props);
this.props.config = line;
setInterval(function(){
this.props.config.labels.push(Math.floor(Math.random() * 100));
this.props.config.datasets[0].data.push(Math.floor(Math.random() * 100));
this.props.changeHandler();
}, 5000);
}
render() {
return (
<div className="chart">
<Line
data={this.state}
height={5}
width={20}
/>
</div>
)
}
}
export default Chart;
export default LineChart;
我建议不要使用 setInterval,尝试将图表实例传递给调度程序等,并在请求完成时更新它
ChartJS.data = response.data;
ChartJS.update();
所以根据https://www.npmjs.com/package/react-chartjs-2
可以使用
等引用访问图表引用
chartReference = {};
componentDidMount() {
console.log(this.chartReference); // returns a Chart.js instance reference
}
render() {
return (<Doughnut ref={(reference) => this.chartReference = reference } data={data} />)
}
所以你可以做的是在你的图表中放置一个引用,然后在你喜欢的任何地方访问它。
<Line
data={this.state}
height={5}
width={20}
ref = {(reference) => this.reference = reference}
/>
在您希望引起更新的方法中,您可以访问此引用和它的 chartInstance 并在此实例上调用更新函数。
let lineChart = this.reference.chartInstance
lineChart.update();
我调用了update()
函数,但是它不起作用。
TypeError: line.update is not a function.
为什么 update()
不是函数?
我在 http://jsfiddle.net/zpnx8ppb/26/ 上看到了这个例子,更新功能确实有效
这是我的代码:
import React, { Component } from 'react';
import { Line } from 'react-chartjs-2';
import Chart from 'chart.js';
const line = {
labels: [],
datasets: [
{
label: 'My First dataset',
fill: false,
data: []
}
]
};
setInterval(function(){
line.labels.push(Math.floor(Math.random() * 100));
line.datasets[0].data.push(Math.floor(Math.random() * 100));
line.update();
}, 5000);
class LineChart extends Component {
render() {
return (
<div className="chart">
<Line
data={this.state}
height={5}
width={20}
/>
</div>
)
}
}
export default LineChart;
您需要更新图表,线只是图表上的配置设置,此更新需要流回处理程序
为了让您走上正确的道路,下面是我的意思的一个例子
var config = {};
class Chart extends Component {
constructor() {
super();
this.ctx = document.getElementById(this._rootNodeID).getContext("2d");
this.chart = new Chart(ctx, config);
}
changeHandler(value) {
this.chart.update();
}
render() {
return (
<canvas id={this._rootNodeID}>
<LineChart value={this.state.value}
config={this.config}
onChange={this.changeHandler}/>
</canvas>
);
}
}
const line = {
labels: [],
datasets: [
{
label: 'My First dataset',
fill: false,
data: []
}
]
};
class LineChart extends Component {
constructor(props) {
super(props);
this.props.config = line;
setInterval(function(){
this.props.config.labels.push(Math.floor(Math.random() * 100));
this.props.config.datasets[0].data.push(Math.floor(Math.random() * 100));
this.props.changeHandler();
}, 5000);
}
render() {
return (
<div className="chart">
<Line
data={this.state}
height={5}
width={20}
/>
</div>
)
}
}
export default Chart;
export default LineChart;
我建议不要使用 setInterval,尝试将图表实例传递给调度程序等,并在请求完成时更新它
ChartJS.data = response.data;
ChartJS.update();
所以根据https://www.npmjs.com/package/react-chartjs-2
可以使用
等引用访问图表引用chartReference = {};
componentDidMount() {
console.log(this.chartReference); // returns a Chart.js instance reference
}
render() {
return (<Doughnut ref={(reference) => this.chartReference = reference } data={data} />)
}
所以你可以做的是在你的图表中放置一个引用,然后在你喜欢的任何地方访问它。
<Line
data={this.state}
height={5}
width={20}
ref = {(reference) => this.reference = reference}
/>
在您希望引起更新的方法中,您可以访问此引用和它的 chartInstance 并在此实例上调用更新函数。
let lineChart = this.reference.chartInstance
lineChart.update();