echarts:饼图/自动轮播

图例:



父组件所有代码:

引入:子组件:

代码:
import PieChart from '../components/pieChart.vue'
    PieChart,

div代码: 
<PieChart

              :id="'pjqdPie'"

              :autoPlay="true"

              :pieData="pjqdList"

              :pieCenter="['50%', '50%']"

              :pieRadius="['75%', '90%']"

              :seriesData="seriesData"

              :options="autoPieOpt" />

 

data:



data代码: 

   seriesData: [
        {
          label: {
            show: false,
            position: 'center'
          },
          emphasis: {
            label: {
              show: true,
              formatter: (params) => {
                const str = `{a|${formatCurrency(params.value)}件}` + '\n' + `{b|${params.name}}`
                return str
              },
              rich: {
                a: {
                  color: '#ffff5d',
                  fontSize: 35,
                  fontWeight: 'bolder',
                  lineHeight: 60, 

                },
                b: {
                  color: '#fff',
                  fontSize: 25
                }
              }
            }
          },
          labelLine: {
            show: false
          }
        }
      ],
      autoPieOpt: {
        tooltip: {
          show: false
        },
        color: [
          {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: '#FFCE57' // 0% 处的颜色
              },
              {
                offset: 1,
                color: '#FFA700' // 100% 处的颜色
              }
            ],
            global: false // 缺省为 false
          },
          {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: '#3DB7CE' // 0% 处的颜色
              },
              {
                offset: 1,
                color: '#067EA2' // 100% 处的颜色
              }
            ],
            global: false // 缺省为 false
          },
          {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: '#1A2444' // 0% 处的颜色
              },
              {
                offset: 1,
                color: '#C03D5F' // 100% 处的颜色
              }
            ],
            global: false // 缺省为 false
          },
          {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: '#fff' // 0% 处的颜色
              },
              {
                offset: 1,
                color: '#35B9FD' // 100% 处的颜色
              }
            ],
            global: false // 缺省为 false
          }
        ]
      },
      

 
子组件所有代码:

<template>
  <div class="WH pieBox">
    <div class="WH" :id="id"></div>
    <span class="chartName">{{ pieName }}</span>
  </div>
</template>

<script>
export default {
  name: 'PieChart',
  components: {},
  props: {
    id: {
      type: String,
      require: true,
      default: ''
    },
    pieName: {
      type: String,
      require: false,
      default: ''
    },
    pieRadius: {
      type: Array,
      require: false,
      default: () => ['30%', '70%']
    },
    pieCenter: {
      type: Array,
      require: false,
      default: () => ['50%', '50%']
    },
    pieData: {
      type: Array,
      require: false,
      default: () => [
        { value: 1048, name: '规自' },
        { value: 735, name: '保密' },
        { value: 580, name: '台办' }
      ]
    },

    options: {
      type: Object,
      require: false,
      default: () => {}
    },
    autoPlay: {
      type: Boolean,
      require: false,
      default: false
    },
    seriesData: {
      type: Array,
      require: false,
      default: () => []
    }
  },
  data() {
    return {
      pieChart: null,
      timer: null
    }
  },
  watch: {
    pieData: {
      handler() {
        this.initChart(this.options)
      },
      deep: true
    },
    options: {
      handler() {
        this.initChart(this.options)
      },
      deep: true
    },
    seriesData: {
      handler() {
        this.initChart(this.options)
      },
      deep: true
    }
  },
  computed: {},
  mounted() {
    this.pieChart = this.$echarts.init(document.getElementById(this.id))
    this.$nextTick(() => {
      this.initChart(this.options)
    })
  },
  beforeDestroy() {
    clearInterval(this.timer)
    this.timer = null
  },
  methods: {
    initChart(opt) {
      this.pieChart && this.pieChart.clear()
      if (this.autoPlay) {
        let ind = 0
        if (this.timer) {
          clearInterval(this.timer)
        }
        this.timer = setInterval(() => {
          this.pieChart.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: ind })
          ind = (ind + 1) % this.pieData.length
          this.pieChart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: ind })
        }, 3000)
        this.pieChart.on('mouseover', (params) => {
          clearInterval(this.timer)
          this.pieChart.dispatchAction({
            type: 'downplay',
            seriesIndex: 0,
            dataIndex: ind
          })
          this.pieChart.dispatchAction({
            type: 'highlight',
            seriesIndex: 0,
            dataIndex: params.dataIndex
          })
          ind = params.dataIndex
        })
        this.pieChart.on('mouseout', (params) => {
          ind = params.dataIndex
          if (this.timer) {
            clearInterval(this.timer)
          }
          this.timer = setInterval(() => {
            this.pieChart.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: ind })
            ind = (ind + 1) % this.pieData.length
            this.pieChart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: ind })
          }, 3000)
        })
      }
      const opts = {
        tooltip: {
          trigger: 'item'
        }
      }
      const series = [
        {
          name: this.pieName,
          type: 'pie',
          radius: this.pieRadius,
          center: this.pieCenter,
          avoidLabelOverlap: false,
          selectedMode: false,
          label: {
            show: false,
            position: 'center',
            color: '#fff',
            fontSize: 14
          },
          emphasis: {
            label: {
              show: true,
              position: 'inside',
              color: '#fff',
              fontSize: 14
            }
          },
          labelLine: {
            show: true
          },
          data: [...this.pieData]
        }
      ]
      this.seriesData.length > 0 &&
        this.seriesData.forEach((item, ind) => {
          series[ind] = Object.assign(series[ind], item)
        })
      Object.assign(opts, { series: series })
      Object.assign(opts, opt)
      this.pieChart.setOption({ ...opts }, true)
      // 视口大小变化  charts图表自适应
      window.addEventListener('resize', () => {
        this.pieChart && this.pieChart.resize()
      })
    }
  }
}
</script>
<style lang="less" scoped>
.pieBox {
  position: relative;
  .chartName {
    width: 100%;
    text-align: center;
    color: #fff;
    position: absolute;
    bottom: 8px;
  }
}
</style>

 看这个文件:


 


                
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值