6 javascript reactjs react-chartjs
我调用了update()函数,但是它不起作用。
TypeError:line.update不是函数。
为什么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;
Run Code Online (Sandbox Code Playgroud)
Dhr*_*rma 14
所以根据https://www.npmjs.com/package/react-chartjs-2
可以使用诸如的 ref 访问图表参考
chartReference = {};
componentDidMount() {
console.log(this.chartReference); // returns a Chart.js instance reference
}
render() {
return (<Doughnut ref={(reference) => this.chartReference = reference } data={data} />)
}
Run Code Online (Sandbox Code Playgroud)
所以你可以做的是在你的图表中放置一个引用并在你喜欢的任何地方访问它。
<Line
data={this.state}
height={5}
width={20}
ref = {(reference) => this.reference = reference}
/>
Run Code Online (Sandbox Code Playgroud)
在您希望引起更新的方法中,您可以访问此引用及其 chartInstance 并在此实例上调用更新函数。
let lineChart = this.reference.chartInstance
lineChart.update();
Run Code Online (Sandbox Code Playgroud)
您需要更新图表,线条只是图表上的配置设置,此更新需要流回处理程序
为了让你走上正确的道路,这里有一个例子来说明我的意思
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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7441 次 |
| 最近记录: |