echarts 环形图

环形图封装成了一个组件 组件名diagran.vue

<!--环形图-->
<template>
  <div class='chartPreDonut' ref='chartPreDonut'></div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  props: ['option', 'unit', 'title', 'center', 'legend', 'legendTop'],
  data () {
    return {
      chartDom: null,
      chartData: this.option
    }
  },
  watch: {
    option: {
      handler (val) {
        this.chartData = val
        this.$nextTick(() => {
          this.chartInit()
        })
      },
      deep: true,
      immediate: true
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.chartInit()
    })
  },
  methods: {
    chartInit () {
      this.chartDom = echarts.init(this.$refs.chartPreDonut)

      const options = {
        tooltip: {
          trigger: 'item',
          formatter: (params) => {
            return `${params.name}:${params.value}${this.unit || ''}${params.data.rate ? `<br /> 占比${params.percent}%` : ''}`
          }
        },
        series: [
          {
            type: 'pie',
            radius: ['30%', '50%'],
            clockwise: true,
            avoidLabelOverlap: true,
            hoverOffset: 15,
            label: {
              show: true,
              position: 'outside',
              formatter: (params) => {
                return `${params.name}:${params.value}${this.unit || ''}${params.data.rate ? `\n占比${params.percent}%` : ''}`
              },
              rich: {
                hr: {
                  backgroundColor: 't',
                  borderRadius: 3,
                  width: 3,
                  height: 3,
                  padding: [3, 3, 0, -12]
                },
                a: {
                  padding: [-30, 15, -20, 15]
                }
              }
            },
            labelLine: {
              normal: {
                length: 5,
                length2: 5,
                lineStyle: {
                  width: 1
                }
              }
            },
            center: this.center || ['50%', '50%'],
            data: this.chartData
          }
        ]
      }

      if (this.legend) {
        options.legend = {
          type: 'scroll',
          orient: 'vertical',
          right: 10,
          top: this.legendTop || 100,
          bottom: 20,
          data: this.chartData.map(m => { return m.name })
        }
      }

      if (this.title) {
        options.title = {
          text: this.title,
          top: '44%',
          textAlign: 'center',
          left: '49.50%',
          textStyle: {
            color: '#104fa7',
            fontSize: 15,
            fontWeight: '400'
          }
        }
      }

      this.chartDom.setOption(options)
      window.addEventListener('resize', () => {
        this.chartDom.resize()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.chartPreDonut {
  width: 100%;
  height: 100%;
}
</style>

 页面引入组件

<template>
  <div>
    <div class="left-charts">
      <Diagran :option='option' unit="人次" />
    </div>
  </div>
</template>

<script>
import Diagran from './diagran.vue'
export default {
  data () {
    return {
      option: [
        {
          "value": 11111,
          "name": "测试1"
        },
        {
          "value": 22222,
          "name": "测试2"
        },
        {
          "value": 33333,
          "name": "测试3"
        },
        {
          "value": 444444,
          "name": "测试4"
        },
        {
          "value": 555555,
          "name": "测试5"
        },
        {
          "value": 66666,
          "name": "测试6"
        },
        {
          "value": 77777,
          "name": "测试7"
        },
        {
          "value": 88888,
          "name": "测试8"
        }
      ]
    }
  },
  components: {
    Diagran
  }
}
</script>

<style lang="less" scoped>
.left-charts {
  width: calc(100% - 140px);
  height: 500px;
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学不会•

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值