echarts 环形图 自动实现某数据集高亮效果

本文介绍如何使用ECharts的dispatchAction API为环形图饼图的特定数据集添加高亮效果,通过自定义属性highlight保持数据集高亮,并提供代码示例。

有时候做echarts 环形图 饼图 某个数据集需要高亮效果 如下

可以通过 echartsdispatchActionAPI 来实现

相关代码如下

/**
 * 定制高亮环形图.
 * Author: Notfound404
 * Description:
 * Date: 2018/4/4
 * Time: 10:07
 */

var circleCharts = echarts.init(document.getElementById('circle'));


circleCharts.setOption({
    title: {
        text: '{a|80}{b|%}',
        x: 'center',
        y: 'center',
        textStyle: {
            rich: {
                a: {
                    fontWeight: 'bold',
                    fontSize: '18'
                },
                b: {
                    verticalAlign: 'bottom',
                    fontSize: '10'
                }
            }
        }
    },
    toolbox: {
        show: false
    },

    grid: {
        top: '5%',
        left: '2%',
        bottom: '10%',
        containLabel: true
    },

    tooltip: {
        show: false
    },

    series: [
        {
            name: '',
            type: 'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            // 高亮偏移值
            hoverOffset: 5,
            label: {
                normal: {
                    show: false
                }
            },

            labelLine: {
                normal: {
                    show: false
                }
            },
            //   highlight 自定义属性  有此属性该数据集保持高亮
            data: [
                {value: "11", name: "baidu"}
                ,
                {value: "5", name: "google", highlight: true}
            ]
        }
    ]
});

/**
 * echarts 执行action
 * @param instance  ecahrts 实例
 * @param type  action 类型
 * @param index   对应的 数据索引
 */
function chartAction(instance, type, index) {
    instance.dispatchAction({
        type: type,
        dataIndex: index
    })
}

/**
 * 初始高亮状态  
 */
circleCharts.on('finished', function(e) {
    var data = this.getOption().series[0].data;
    for (var i in data) {
        if (data) {
            if (data[i].highlight) {
                chartAction(this, 'highlight', i);
            }
        }
    }
});



/**
 * 鼠标移出 判断   有高亮标识的数据集 保持高亮
 */
circleCharts.on('mouseout', function (e) {
    var highlight = e.data.highlight;

    if (highlight) {
        chartAction(this, 'highlight', e.dataIndex)
    }
})

/**
 *  鼠标hover 动作判断   非高亮标识的数据集 保持原状
 */
circleCharts.on('mouseover', function (e) {
    var highlight = e.data.highlight;
    if (!highlight) {
        chartAction(this, 'downplay', e.dataIndex)
    }
})


转载于:https://my.oschina.net/10000000000/blog/2874049

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值