<template>
      <div :id="chartId" class="chart-container"></div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  props: {
    title: {
      type: String,
      default: ''
    },
    unit: {
      type: String,
      default: '万(㎡)'
    },
    tiplabel: {
      type: String,
      default: ''
    },
    chartId: {
      type: String,
      required: true
    },
    data: {
      type: Array,
      default: () => {
        return []
      }
    },
    showPercent: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      myChart: undefined
    }
  },
  watch: {
    data: {
      handler: function(newVal, oldVal) {
        this.init()
        this.setOptions()
      },
      deep: true
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.myChart = echarts.init(document.getElementById(this.chartId))
      this.setOptions()
      window.addEventListener('resize', () => {
        setTimeout(() => {
          this.myChart.resize()
        }, 300)
      })
    },

    setOptions() {
      if(!this.myChart) {
        return
      }

      let option

      if (this.data && this.data.length == 0 ) {
        option = {
          title: {
            text: '暂无数据',
            x: 'center',
            y: 'center',
            textStyle: {
              fontSize: 18,
              fontWeight: 'normal',
              color: '#1578D9'
            }
          }
        }
      } else {
        option = {
          title: {
            text: this.title ? this.title : '',
            x: 'center',
            top: '20',
            textStyle: {
              fontSize: 18,
              fontWeight: 'normal',
              color: '#000'
            }
          },
          legend: {
            show: true,
            right: '10',
            orient: 'vertical',
          },
          tooltip: {
            trigger: 'item',
            backgroundColor: '#ffffff',
            borderColor:'#1578D9',
            formatter: (params) => {
              let unit = this.unit
              let tiplabel = this.tiplabel
              let res=""
              res+='<div style="font-size: 14px;text-align: left;color: #000000;">'+ '<div><span>'+params.name+'</span></div>'+ '<div ><span>'+tiplabel+'：'+params.value+unit+'</span></div>'+'<div><span>比例：'+params.percent+'%'+'</span></div>'  +'</div>'
              return res
            },
          },
          series: [
            {
              type: 'pie',
              data: this.data,
              radius: ['0%', '50%'],
              label: {
                formatter: (series) => {
                  console.log("series",series)
                  const data = series.data
                  return `{a|${data.name}}\n{b|${data.value}}{c|${this.unit}},{d|${series.percent}%}`
                },
                show: true,
                verticalAlign: 'middle',
                borderWidth: 0,
                width: 100,
                // backgroundColor: '#1578D9',
                borderStatus: 30,
                rich: {
                  a: {
                    padding: [12, 0, 2, 0],
                    // color: '#eeeded',
                    fontSize: 16,
                    align: 'center',
                  },
                  b: {
                    padding: [2, 0, 8, 0],
                    // color: '#f2f2f2',
                    fontSize: 16,
                    align: 'center',
                  }
                }
              },
              labelLine: {
                show: true
              },
            }
          ]
        }
      }


      // 使用刚指定的配置项和数据显示图表。
      // option.series[0].data.map((item,index)=>{
      //   if(item.value == "0"){
      //     item.value=''
      //   }
      //   return item
      // })
      this.myChart.clear()
      this.myChart.setOption(option)
    }
  }
}
</script>

<style scoped lang="scss">
.chart-container {
  height: 100%;
}
</style>
