<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
// require('echarts/theme/macarons') // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
    height: {
      deep: true,
      handler(val) {
        console.log(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({
      title,
      xAxis,
      series,
      legend,
      unit,
      rotate,
      time,
      serviceData,
      serviceData2,
    } = {}) {
      this.chart.setOption({
         title: [
    {
        text: '75.62%',
        subtext: '质押率50%',
        textStyle:{
            fontSize:20,
            color:"#ff7e4b"
        },
        subtextStyle: {
            fontSize: 18,
            color: '#bbbaba'
        },
        textAlign:"center",
        x: '34.5%',
        y: '43%',
    }],
    tooltip: {
        trigger: 'item',
        formatter:function (parms){
          var str= 
            parms.marker+""+parms.data.name+"</br>"+
            "价值："+ parms.data.value+"</br>"+
            "占比："+ parms.percent+"%";
            return  str ;
        }
    },
    legend: {
        type:"plain",
        icon:'rect',
        orient: 'vertical',
        left:'70%',
        align:'left',
        top:'middle',
        layout:'horizontal',
        itemGap:30,
        padding:[20,29,40,0],
        textStyle: {
            color:'#333',
            fontSize:14,
            padding:[0,10]
        },
        height:'auto',
        itemWidth:20,
        itemHeight:20
    },
    series: [
        {
            name:'标题',
            type:'pie',
            center: ['35%', '50%'],
            radius: ['40%', '65%'],
             clockwise: false, //饼图的扇区是否是顺时针排布
            avoidLabelOverlap: false,
            label: {
                normal: {
                    show: false,
                }
            },
             
            labelLine: {
                normal: {
                  length:5,
                  length2:3,
                  smooth:true,
                }
            },
            data: [
               {value:335, legendname:'种类01',name:"账户余额",itemStyle:{color:"#5087ec"}},
               {value:310, legendname:'种类02',name:"应收账款",itemStyle:{color:"#68bbc4"}},
               {value:234, legendname:'种类03',name:"物质库存价值",itemStyle:{color:"#58a55c"}},
               {value:154, legendname:'种类04',name:"在途货物价值",itemStyle:{color:"#f2bd42"}},
               {value:335, legendname:'种类05',name:"预付款",itemStyle:{color:"#ee752f"}},

            ]
        }
        ]
      });
      this.chart.on("click", function (param) {
        this.callParents(param);
      });
    },
    // 调用父组件
    callParents(name) {
      this.$emit("pieChartFunction", name);
    },
  },
};
</script>
