我正在使用echarts和react,使用echarts来做出反应。我想调用一个组件,点击图形上的一个点,如何实现这一点,还有什么是传说,我阅读了文档,但无法理解它。
发布于 2018-10-17 10:19:49
您可以将一个对象作为onEvents支柱传递给ReactEcharts组件。
<ReactEcharts ref={(e) => { this.echarts_react = e; }}
onEvents= {this._onEvents}
/>在这里,_onEvents可以像
_onEvents = {
'click': this.onChartClick,
'dataZoom': this.onDataZoom,
}和
onChartClick = (params)=>{
// Do what you want to do on click event. params depend on the type of chart
}发布于 2021-10-31 18:49:02
我发现这两个答案都不完整,所以这是我的。
我正在使用echarts v5。
TLDR:如果使用类组件,不要忘记将回调绑定到对象
功能组件
import React from 'react';
import ReactECharts from 'echarts-for-react';
function MyComp(props) {
const options = { ... }
const onChartClick = (params) => {
console.log('Chart clicked', params);
};
const onEvents = {
click: onChartClick,
};
return (
<ReactECharts
option={options}
onEvents={onEvents}
/>
);
}类组件
import React from 'react';
import ReactECharts from 'echarts-for-react';
class MyComp extends React.Component {
constructor(props) {
super(props);
this.options = { ... };
this.onChartClick = this.onChartClick.bind(this);
}
onChartClick = (params) => {
console.log('Chart clicked', params);
};
onEvents = {
click: this.onChartClick
}
render() {
return (
<ReactECharts
option={this.options}
onEvents={this.onEvents}
/>
);
}
}发布于 2018-12-27 09:08:20
onEvents支柱不能正常工作,尝试获取echarts的引用,并将您的事件添加到zr对象中,如下所示:
import React, {Component} from 'react';
import './App.css';
import ReactEcharts from "echarts-for-react";
class App extends Component {
constructor(props, context) {
super(props, context);
this.state = {
graphOption: {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [{
data: [820, 932, 901, 934, 1290, 1330, 1320],
type: 'line'
}]
}
}
}
componentDidMount() {
this.echartsInstance = this.echartsReactRef.getEchartsInstance();
this.zr = this.echartsInstance.getZr();
this.zr.on('click', this.onChartClick);
}
onChartClick = (...rest) => {
console.log('App:onClickChart', rest);
};
render() {
return (
<div className="App">
<header className="App-header">
<ReactEcharts
style={{height: '100vh', width: '100vw'}}
ref={(e) => {
this.echartsReactRef = e;
}}
option={this.state.graphOption}
/>
</header>
</div>
);
}
}
export default App;https://stackoverflow.com/questions/52752552
复制相似问题