echarts 图表轮播(pie图数据轮播)

本文总结了pie图轮播,难点是鼠标移入移出时候操作。

<template>
  <div class="pie">
    <div class="container" id="chart"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { debounce } from 'lodash'
export default {
  data() {
    return {
      i: 0,
      list: [
        { value: 1048, name: 'Search Engine' },
        { value: 735, name: 'Direct' },
        { value: 580, name: 'Email' },
        { value: 484, name: 'Union Ads' },
        { value: 300, name: 'Video Ads' },
      ],
    }
  },
  mounted() {
    this.initChart()
    this.autoMove()
    this.mouseMove()
	window.addEventListener('resize', debounce(() => this.myChart.resize(), 2000)) // 使用事件捕获,由外层向内
  },
  beforeDestroy() {
  	window.removeEventListener('resize', () => this.myChart.resize())
},
  methods: {
    initChart() {
      this.myChart = echarts.init(document.getElementById('chart'))
      let option = {
        tooltip: {
          trigger: 'item',
        },
        legend: {
          top: '5%',
          left: 'center',
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2,
            },
            label: {
              show: false,
              position: 'center',
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: 'bold',
              },
            },
            labelLine: {
              show: false,
            },
            data: this.list,
          },
        ],
      }
      this.myChart.setOption(option)
    },
    autoMove() {
      this.timer = setInterval(() => {
        this.myChart.dispatchAction({
          type: 'downplay',
          seriesIndex: 0,
        })
        this.myChart.dispatchAction({
          type: 'highlight',
          seriesIndex: 0,
          dataIndex: this.i % this.list.length,
        })
        this.i++
      }, 1000)
    },
    mouseMove() {
      this.myChart.on('mouseover', (params) => {
        window.clearInterval(this.timer)
        //如果鼠标移入正在高亮的数据段时,只显示当前高亮
        this.myChart.dispatchAction({
          type: 'downplay',
          seriesIndex: 0,
        })
        this.myChart.dispatchAction({
          type: 'highlight',
          seriesIndex: 0,
          dataIndex: params.dataIndex,
        })
      })
      // 当鼠标移出,从上次坐标开始, this.i全局标记了
      this.myChart.on('mouseout', this.autoMove)
    },
  },
}
</script>

<style lang="scss" scoped>
.pie {
  height: 100vh;
  .container {
    height: vh(500);
  }
}
</style>

  • 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、付费专栏及课程。

余额充值