echarts 环形图

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

javascript 复制代码
<!--环形图-->
<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>

页面引入组件

javascript 复制代码
<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>
相关推荐
专注API从业者6 小时前
Python + 淘宝 API 开发:自动化采集商品数据的完整流程
大数据·运维·前端·数据挖掘·自动化
你的人类朋友7 小时前
【Node&Vue】JS是编译型语言还是解释型语言?
javascript·node.js·编程语言
烛阴7 小时前
TypeScript高手密技:解密类型断言、非空断言与 `const` 断言
前端·javascript·typescript
样子20188 小时前
Uniapp 之renderjs解决swiper+多个video卡顿问题
前端·javascript·css·uni-app·html
Nicholas688 小时前
flutterAppBar之SystemUiOverlayStyle源码解析(一)
前端
黑客飓风8 小时前
JavaScript 性能优化实战大纲
前端·javascript·性能优化
emojiwoo10 小时前
【前端基础知识系列六】React 项目基本框架及常见文件夹作用总结(图文版)
前端·react.js·前端框架
张人玉10 小时前
XML 序列化与操作详解笔记
xml·前端·笔记
杨荧10 小时前
基于Python的宠物服务管理系统 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python·信息可视化
YeeWang11 小时前
🎉 Eficy 让你的 Cherry Studio 直接生成可预览的 React 页面
前端·javascript