echarts饼图内容循环播放实现

12 篇文章 0 订阅
1 篇文章 0 订阅

echarts饼图内容循环播放实现

效果展示

在这里插入图片描述

思路

先实现普通的饼图,再处理数据内容:使用for循环+延时器实现数据分割,数据处理好后再进行渲染。

难点

数据分割

代码实现


<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
require("echarts/theme/blue"); // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
  },
  components: {},
  data() {
    return {
      chart: null,
      seriesList: [],
    };
  },
  watch: {},
  created() {
    this.seriesList = [
      { value: 57, name: "昆明" },
      { value: 21, name: "玉溪" },
      { value: 9, name: "红河" },
      { value: 9, name: "文山" },
      { value: 8, name: "版纳" },
      { value: 8, name: "楚雄" },
      { value: 7, name: "大理" },
      { value: 6, name: "保山" },
      { value: 8, name: "临沧" },
      { value: 6, name: "普洱" },
      { value: 5, name: "怒江" },
      { value: 5, name: "曲靖" },
      { value: 3, name: "瑞丽" },
      { value: 2, name: "迪庆" },
      { value: 1, name: "德宏" },
      { value: 1, name: "丽江" },
      { value: 1, name: "昭通" },
    ];
    //设置了一个定时器,65秒执行一次
    this.timer = setInterval(() => {
      this.initChart();
    }, 65000);
  },
  mounted() {
    this.initChart();
  },
  destroyed() {
    clearInterval(this.timer);
  },

  computed: {},

  methods: {
    initChart() {
      const seriesListCount = this.seriesList;
      const myChart = this.$echarts.init(this.$el, "blue");

      const option = {
        title: {
          x: "left",
          y: 1,
          text: "作业总数:",
          textStyle: {
            color: "#40c2ff",
            fontSize: "15",
            fontFamily: "微软雅黑",
            fontWeight: "bold",
          },
        },
        tooltip: {
          trigger: "axis",
          backgroundColor: "rgba(255,255,255,0.7)",
          textStyle: {
            color: "#000", // 自定义文字颜色
            fontSize: 16, // 自定义文字大小
          },
        },
        tooltip: {
          trigger: "item",
          backgroundColor: "rgba(255,255,255,0.7)",
          textStyle: {
            color: "#000", // 自定义文字颜色
            fontSize: 16, // 自定义文字大小
          },
          formatter: "<strong>作业总数</strong> <br/>{b} : {c} ",
        },
        legend: {
          show: false,
        },
        series: [
          {
            name: "Access From",
            type: "pie",
            radius: ["0", "70%"],
            center: ["45%", "50%"],
            itemStyle: {
              normal: {
                label: {
                  show: true,
                  position: "outside",
                  textStyle: {
                    fontSize: "12",
                    color: "#fff",
                  },
                  formatter: "{b} : {c}",
                },
                labelLine: {
                  show: false, // 是否显示视觉引导线。
                  length: 10, // 在 label 位置 设置为'outside'的时候会显示视觉引导线。
                  length2: 0, // 视觉引导项第二段的长度。
                  lineStyle: {
                    // 视觉引导线的样式
                    color: "#fff",
                    width: 1,
                  },
                },
              },
            },
            data: this.seriesList,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };
      //for循环延迟执行;
      if (seriesListCount.length > 0) {
        for (var j = 0; j < seriesListCount.length - 4; j++) {
          (function (j) {
            setTimeout(function () {
              let i = j + 5;
              let sss = seriesListCount;
              const sliceA = sss.slice(j, i);
              option.series[0].data = sliceA;
              myChart.setOption(option);
              if (j == seriesListCount.length - 5) {
                j = 0;
              }
            }, 5000 * j);
          })(j);
        }
      }

      myChart.setOption(option, true);
    },
  },
};
</script>
<style lang='scss' scoped>
</style>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现echarts饼图的自动播放效果,可以使用echarts提供的动画和定时器功能。首先,确保你已经引入了echarts库。 然后,使用以下步骤来实现自动播放效果: 1. 创建一个echarts实例: this.pieChart = this.$echarts.init(document.getElementById(this.id)); 2. 定义初始的饼图数据: var data = [ { value: 335, name: '数据1' }, { value: 310, name: '数据2' }, { value: 234, name: '数据3' }, { value: 135, name: '数据4' }, { value: 1548, name: '数据5' } ]; 3. 设置饼图的配置项,包括自动播放的参数: var option = { series: [ { type: 'pie', radius: ['50%', '70%'], animation: true, // 开启动画效果 animationType: 'scale', // 设置动画类型 animationEasing: 'elasticOut', // 设置动画缓动效果 animationDelay: function (idx) { // 设置动画延迟时间 return idx * 200; }, data: data } ] }; 4. 在echarts实例中设置配置项: this.pieChart.setOption(option); 5. 使用定时器来实现自动播放效果: var currentIndex = -1; setInterval(function () { var dataLen = option.series.data.length; // 取消之前高亮的图形 this.pieChart.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: currentIndex }); currentIndex = (currentIndex + 1) % dataLen; // 高亮当前图形 this.pieChart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: currentIndex }); // 显示提示框 this.pieChart.dispatchAction({ type: 'showTip', seriesIndex: 0, dataIndex: currentIndex }); }, 2000); 通过以上步骤,你就可以实现echarts饼图的自动播放效果了。定时器每隔2秒钟切换到下一个数据,并高亮该数据的图形,并显示提示框。你可以根据需要调整定时器的时间间隔和饼图的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值