echarts柱状图点击事件

常规事件
但是此方法在没有数据时点击事件不生效。

 	myChart.on('click',params => {
            console.log(params.value);
      });
                    ```
## 百度出的事件

```javascript
    async function initEchart() {
        var chartDom = document.getElementById('mychart');
        var myChart = echarts.init(chartDom);
        let data = [];
        let valueData=[];
        let nameData = [];
        await proxy.api.apiInfogetDustRelatedProcess().then((res) => {
            if (res.success) {
                for (let i of res.data) {
                    valueData.push(i.dustRelatedProcessType);
                    nameData.push(proxy.funEcho(i.dustRelatedProcessType, state.dust_technology));
                    i.industryType = proxy.funEcho(i.dustRelatedProcessType, state.dust_technology);
                    data.push(i.dustRelatedProcessNum);
                }
            }
        })
        const option = {
            tooltip: {
                trigger: 'axis',
            },
            xAxis: {
            //valueData 我自己存数据需要用的单独加的 
                valueData:valueData,
                type: 'category',
                data: nameData,
                axisLabel: {
                    show: true,
                    color: "#6BABF2",
                    formatter: function (value) {
                        var str = "";
                        var num = 2; //每行显示字数 
                        var valLength = value.length; //该项x轴字数  
                        var rowNum = Math.ceil(valLength / num); // 行数  

                        if (rowNum > 1) {
                            for (var i = 0; i < rowNum; i++) {
                                var temp = "";
                                var start = i * num;
                                var end = start + num;

                                temp = value.substring(start, end) + "\n";
                                str += temp;
                            }
                            return str;
                        } else {
                            return value;
                        }
                    }
                },
                axisTick: {
                    show: false,
                },
                axisLine: {
                    show: true,
                    lineStyle: {
                        color: "rgba(95, 180, 237, 0.32)",
                    },
                },
            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    show: true,
                    color: "#6BABF2",
                },
                splitLine: {
                    lineStyle: {
                        color: 'rgba(255,255,255,0.1)'
                    }
                }
            },
            grid: {
                top: '15',
                left: '0',
                right: '0',
                bottom: '15',
                containLabel: true
            },
            series: [
            {
                data: data,
                type: 'bar',
                showBackground: true,
                backgroundStyle: {
                    color: 'rgba(180, 180, 180, 0.2)'
                },
                barWidth: '15',
                itemStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 1, color: "RGBA(16, 42, 93, 0.5)" },
                        { offset: 0, color: "RGBA(2, 173, 245, 1)" },
                    ]),
                },
            }]
        };
        option && myChart.setOption(option);
        //主要代码
        myChart.getZr().on('click',params=>{
            let pointInPixel= [params.offsetX,params.offsetY];
            if(myChart.containPixel('grid',pointInPixel)){
                let pointInGrid=myChart.convertFromPixel({seriesIndex: 0},pointInPixel);
                let xIndex=pointInGrid[0];
//valueData 是我自己单独用来存取数据的   可以打印option查看所有
            console.log(xIndex,"xindex",option.xAxis.valueData[xIndex]);
                let value=option.xAxis.valueData[xIndex];
                openEnterprisesList('dustTechnology',value)    
            }
        })
    }
  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值