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

问题描述 投票:0回答:2

我正在使用echarts与echarts作出反应。我想点击一个组件点击图表上的一个点,如何实现这一点,以及什么是传说,我阅读文档但无法理解它。

reactjs echarts
2个回答
1
投票

您可以将对象作为onEvents prop传递给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
投票

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;
© www.soinside.com 2019 - 2024. All rights reserved.