echarts饼图轮播效果

echarts饼图轮播效果


echarts提供的dispatchAction方法可以控制饼图某一段“highlight”’高亮,或“downplay”取消高亮。利用定时器控制饼图循环高亮。

循环好做,主要是鼠标移入饼图也会触发高亮导致冲突,显示效果出现问题。需要同时监听echarts的mouseover事件,鼠标移入做特殊处理。

				var option;
				var ele = document.getElementById("chart");
                echarts.dispose(ele);
                this.chart= echarts.init(ele);
                this.chart.setOption(option);

                var i = 0;
                var looper = () => {
                    i++;
                    if (i == data.length) {
                        i = 0;
                    }
                    this.chart.dispatchAction({
                        type: 'downplay',
                        seriesIndex: 0,
                        dataIndex: i == 0 ? data.length - 1 : i - 1,
                    });
                    this.chart.dispatchAction({type: 'highlight', seriesIndex: 0, dataIndex: i});
                    return looper;
                };
                this.timer= setInterval(looper(), 3000);

                this.chart.on('mouseover', params => {
                    window.clearInterval(this.timer);
                    //如果鼠标移入正在高亮的数据段时,不取消高亮
                    if (i != params.dataIndex) {
                        this.chart.dispatchAction({
                            type: 'downplay',
                            seriesIndex: 0,
                            dataIndex: i,
                        });
                    }
                    //鼠标移出后从鼠标位置开始循环
                    i = params.dataIndex;
                });

                this.chart.on('mouseout', params => {
                    this.timer= setInterval(looper(), 3000);
                });
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现echarts饼图轮播,可以使用echarts提供的graphic组件和动画效果。具体步骤如下: 1. 在option中设置graphic组件,用于绘制饼图轮播按钮。 2. 在series中设置data数据,用于绘制饼图。 3. 使用setInterval函数和echarts提供的dispatchAction方法,实现轮播效果。 以下是一个简单的示例代码: ``` option = { graphic: { type: 'group', left: 'center', top: 'middle', children: [ { type: 'pie', id: 'pie', z: 10, label: { show: false }, labelLine: { show: false }, emphasis: { scale: true }, data: [] }, { type: 'circle', shape: { cx: 0, cy: 0, r: 30 }, style: { fill: '#fff', stroke: '#999', lineWidth: 2 }, onclick: function () { // 点击按钮时触发轮播 var pie = myChart.getOption().graphic.children[0]; var currentIndex = pie.currentIndex || 0; var dataLength = option.series[0].data.length; myChart.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: currentIndex }); currentIndex = (currentIndex + 1) % dataLength; myChart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: currentIndex }); myChart.dispatchAction({ type: 'showTip', seriesIndex: 0, dataIndex: currentIndex }); pie.currentIndex = currentIndex; } } ] }, series: [ { type: 'pie', radius: ['40%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold' } }, labelLine: { show: false }, data: [ {value: 335, name: '直接访问'}, {value: 310, name: '邮件营销'}, {value: 234, name: '联盟广告'}, {value: 135, name: '视频广告'}, {value: 1548, name: '搜索引擎'} ] } ] }; // 使用setInterval函数实现轮播 setInterval(function () { var pie = myChart.getOption().graphic.children[0]; var currentIndex = pie.currentIndex || 0; var dataLength = option.series[0].data.length; myChart.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: currentIndex }); currentIndex = (currentIndex + 1) % dataLength; myChart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: currentIndex }); myChart.dispatchAction({ type: 'showTip', seriesIndex: 0, dataIndex: currentIndex }); pie.currentIndex = currentIndex; }, 2000); // 渲染图表 var myChart = echarts.init(document.getElementById('main')); myChart.setOption(option); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值