echarts 饼图轮播

饼环图每项进行轮播

定义 轮播方法
echarts更多图表行为链接:https://echarts.apache.org/zh/api.html#action

// 轮播
carouselEcharts() {
 /**
     * carouselIndex  当前轮播下标
     * optionDataLength  传入echarts的options数据有几项
     */
// 获取echarts实例
      const myChart = this.$refs.myChart.chart;
      this.dispatTimer = setInterval(() => {
      // 高亮
        myChart.dispatchAction({
          type: 'highlight',
          seriesIndex: 0,
          dataIndex: this.carouselIndex,
        });
        // 取消高亮
        myChart.dispatchAction({
          type: 'downplay',
          seriesIndex: 0,
          dataIndex: this.carouselIndex === 0 ? this.optionDataLength - 1 : this.carouselIndex - 1,
        });
        this.carouselIndex += 1;

        if (this.carouselIndex >= this.optionDataLength) {
          this.carouselIndex = 0;
        }
      }, 3000);
    }

在vue文件中的mounted生命周期中调用

  mounted() {
    this.$nextTick(() => {
      const myChart = this.$refs.myChart.chart;
      this.carouselToolTip();
      // 鼠标移上去时停止轮播
      myChart.on('mouseover', (ev) => {
        clearInterval(this.dispatTimer);
        this.dispatTimer = null;
        // 修复停止轮播时的那一项 鼠标移上去时无响应
        if (ev.dataIndex === this.carouselIndex - 1) {
          myChart.dispatchAction({
            type: 'highlight',
            seriesIndex: 0,
            dataIndex: ev.dataIndex,
          });
        } else {
          myChart.dispatchAction({
            type: 'downplay',
            seriesIndex: 0,
            dataIndex: this.carouselIndex === 0 ? this.optionDataLength - 1 : this.carouselIndex - 1,
          });
        }
      });
      // 鼠标移出图表时取消当前高亮并停止轮播
      myChart.on('globalout', () => {
        myChart.dispatchAction({
          type: 'downplay',
          seriesIndex: 0,
          dataIndex: this.carouselIndex === 0 ? this.optionDataLength - 1 : this.carouselIndex - 1,
        });
        this.carouselToolTip();
      });
    });
  },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值