关于引入Echarts的方法在此
直通车在此
接下来就是添加点击事件,获取X轴的数据
<div echarts #charts [options]="chartOption" class="charts"></div>
import { NgxEchartsService } from 'ngx-echarts';
@ViewChild('charts', { static: false }) charts: ElementRef;
constructor(
private es: NgxEchartsService
) { }
this.es.getInstanceByDom(this.charts.nativeElement).setOption(this.chartOption)
let myChart = this.es.getInstanceByDom(this.charts.nativeElement)
myChart.getZr().on('click', function (params) {
var pointInPixel = [params.offsetX, params.offsetY];
if (myChart.containPixel('grid', pointInPixel)) {
/*此处添加具体执行代码*/
var pointInGrid = myChart.convertFromPixel({ seriesIndex: 0 }, pointInPixel);
//X轴序号
var xIndex = pointInGrid[0];
//获取当前图表的option
var op = myChart.getOption();
//获得图表中我们想要的数据---懒得写循环了,复制了一下,下面就不简写了,默认说我们的折现有三条吧
var xValue = op.xAxis[0].data[xIndex];
var value = op.series[0].data[xIndex];
var name = op.series[0].name;
var value1 = op.series[1].data[xIndex];
var name1 = op.series[1].name;
var value2 = op.series[2].data[xIndex];
var name2 = op.series[2].name;
console.log(op);
console.log('xValue: '+xValue + ", series[0].name0: "+name +' , value: '+ value + "%");
console.log('xValue: '+xValue + ", series[1].name0: "+name1 +' , value1: '+ value1 + "%");
console.log('xValue: '+xValue + ", series[2].name0: "+name2 +' , value2: '+ value2 + "%");
}
});