echarts 柱状图根据数据设置不同颜色/点击切换颜色

本文介绍如何使用ECharts创建一个交互式的柱状图,并详细解释了配置项的意义及实现点击事件的方法。
var ProfileChart = echarts.init(document.getElementById('activityAppliucation'));
    var option = {
        backgroundColor : '#fff',
        tooltip : {
            trigger : 'axis',
        },
        grid : {
            left : '1%',
            right : '4%',
            bottom : '5%',
            top : '10%',
            containLabel : true
        },
        xAxis : [ {
            type : 'category',
            axisTick: {
                show: false
            },
            axisLine: {
                show:false,
                lineStyle: {
                    color: '#57617B'
                }
            },
            data : xdata
        } ],
        yAxis: {
            type: 'value',
            axisTick: {
                show: false
            },
            axisLine: {
                show:false,
                lineStyle: {
                    color: '#57617B'
                }
            },
            splitLine: {
                lineStyle: {
                    width:1,
                    color: '#DBDBDB'
                }
            }
        },
        series : [ {
            name : '',
            type : 'bar',
            barWidth: 25,
            itemStyle: {
                color: function (params){
                    if(params.dataIndex == echartsIndex) {
                        return '#c44545';
                    } else {
                        return '#0F8FFF';
                    }
                },
                barBorderRadius: 16
            },
            data : ydata
        } ]
    };
    ProfileChart.setOption(option,true);
    ProfileChart.off('click');
    ProfileChart.getZr().on('click', (params) => {
        const pointInPixel = [params.offsetX, params.offsetY];
        //点击第几个柱子
        let index;
        if (ProfileChart.containPixel('grid', pointInPixel)) {
            index = ProfileChart.convertFromPixel({seriesIndex: 0}, [params.offsetX, params.offsetY])[0];
        }
        chartState = true;
        echartsIndex = index;
        activityTable(visitTrendData[index].contentType);
        appActivityVisitTrendChild(visitTrendData[index].contentType);
        //柱形图重构
        ProfileChart.setOption(option,true);
    })
    window.addEventListener("resize", function () {
        ProfileChart.resize();
    })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值