import ReactEcharts, { ReactEchartsPropsTypes } from 'echarts-for-react'; import React, { Component } from 'react'; import { GlobalItemProp } from '../../App'; import { chartHandleMap } from './ChartDatasetMap'; import echarts, { ECharts } from 'echarts'; import "echarts/map/js/china" import { ComponentNameEnum } from '../chartFactory.d'; interface Prop { id? : string ; name : string ; data: GlobalItemProp ; remoteData: any ; style : React.CSSProperties ; // res的格式参考:https://echarts.apache.org/zh/api.html#events onChartClick ?: (res:any)=>void ; } interface State { } export default class ChartComponentHOC extends Component { chart : ECharts|null = null ; onEvent = (e:any)=>{ this.forceUpdate(); } onClick =(res:any)=>{ // window.open('https://www.baidu.com/s?wd=helo'); // console.log("hehehehehehhehehehehehhehehehehehhehehehehehhehehehehehhehehehehehhehehehehehheheheheheh") this.props.onChartClick && this.props.onChartClick({ option : res, remoteData : this.props.remoteData, data : this.props.data, name : this.props.name, key : this.props.id, }); } componentDidMount (){ // console.log('did mount'); // console.log(this.chart) const myChart :any = this.chart; console.log(this.props); const name : string = this.props.name; if(name == ComponentNameEnum.漏斗图 || name==ComponentNameEnum.全国地图 || name==ComponentNameEnum.饼图){ myChart.on("click",(param:any)=>{ this.onClick(param) }) }else{ myChart.getZr().on("click",(params:any)=>{ // console.log(p); var pointInPixel= [params.offsetX, params.offsetY]; const option = myChart.getOption(); if (myChart.containPixel('grid',pointInPixel)) { var xIndex=myChart.convertFromPixel({seriesIndex:0},[params.offsetX, params.offsetY])[0]; var ds = option.series[0].data[xIndex];//myChart.getOption().series[0].data[xIndex] // this._setRowColor(ds);//需要执行的方法 this.onClick({data:ds}) } }) } } componentDidUpdate(){ // console.log('did update'+this.chart); } componentWillReceiveProps(nextProp:Prop){ const {remoteData,data,name} = nextProp; const v : ReactEchartsPropsTypes = chartHandleMap(remoteData,data,name); this.chart?.setOption(v.option) } render(){ const {data,style,remoteData,name} = this.props; const v : ReactEchartsPropsTypes = { ...chartHandleMap(remoteData,data,name), // onEvents:{ // 'click':this.onClick.bind(this) // } }; // console.log(style) return { this.chart=ref&&ref.getEchartsInstance() // this.chart = ref; }} /> } }