单击图表上的数据点时,echarts for-react调用组件

时间:2018-10-11 05:03:12

标签: reactjs echarts

我正在使用 echarts 进行反应,而使用 echarts 进行反应。我想在单击图形上的点时调用一个组件,如何实现这一点,还有什么是图例,我阅读了文档,但无法理解。

2 个答案:

答案 0 :(得分:1)

您可以将对象作为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 
}

答案 1 :(得分:1)

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;