美文网首页
封装Echart饼图组件

封装Echart饼图组件

作者: 曲昶光 | 来源:发表于2021-09-02 16:30 被阅读0次
<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts';
import resize from "./mixins/resize"; // 动态自适应
require('echarts/theme/macarons')
export default {
  mixins: [resize],
  name: 'PieCustom',
  components: {},
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '80vw'
    },
    height: {
      type: String,
      default: '500px'
    },
    pieData: {
      type: Object,
      required: true,
      default: function () {
        return {
          name: null,// 图标名称
          subtext: null,//简介
          data: [{ name: null, value: null }],//图表数据
        }
      }
    }
  },
  data () {
    return {
      chart: null
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  watch: {
    pieData: {
      deep: true,
      handler (val) {
        this.initChart()
      }
    }
  },
  methods: {
    initChart () {
      var that = this
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        title: {
          text: that.pieData.name,
          subtext: that.pieData.subtext,
          left: 'center',
          subtextStyle: {
            verticalAlign: 'top'
          }
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)',
          backgroundColor: '#F6F8FC',
        },
        legend: {
          type: 'scroll',
          orient: 'vertical',
          right: 10,
          top: 20,
          bottom: 20,
          data: that.pieData.legend
        },
        series: [
          {
            name: that.pieData.dataName1,
            type: 'pie',
            // selectedMode: 'single',
            top: 20,
            minAngle: 10,
            radius: [0, '30%'],
            label: {
              position: 'inner',
              fontSize: 14,
            },
            labelLine: {
              show: false
            },
            data: that.pieData.data1,
          },
          {
            name: that.pieData.dataName,
            type: 'pie',
            radius: ['45%', '60%'],
            top: 20,
            minAngle: 10,
            labelLine: {
              length: 30,
            },
            label: {
              formatter: '{a|{a}}{abg|}\n{hr|}\n  {b|{b}:}{c}  {per|{d}%}  ',
              backgroundColor: '#F6F8FC',
              borderColor: '#8C8D8E',
              borderWidth: 1,
              borderRadius: 4,

              rich: {
                a: {
                  color: '#6E7079',
                  lineHeight: 22,
                  align: 'center'
                },
                hr: {
                  borderColor: '#8C8D8E',
                  width: '100%',
                  borderWidth: 1,
                  height: 0
                },
                b: {
                  color: '#4C5058',
                  fontSize: 14,
                  fontWeight: 'bold',
                  lineHeight: 33
                },
                per: {
                  color: '#fff',
                  backgroundColor: '#4C5058',
                  padding: [3, 4],
                  borderRadius: 4
                }
              }
            },
            data: that.pieData.data
          }
        ]
      })
      // 鼠标点击事件
      this.chart.on("click", function (params) {
        if (params) {
          that.$emit('clickEvent', params);
        }
      })
    },

  }
}
</script>

相关文章

网友评论

      本文标题:封装Echart饼图组件

      本文链接:https://www.haomeiwen.com/subject/hrkcwltx.html