<template>
  <div class="chart" ref="chart"></div>
</template>

<script>
import * as echarts from 'echarts'
import { fontSize } from '@/views/groupPage/util'
export default {
  name: 'ComProgressBarChart', //进度横向柱状图
  props: {
    chartData: {},
    title: { type: String, required: true }
  },
  data() {
    return { myChart: null }
  },
  watch: {
    chartData(val) {
      this.$nextTick(() => {
        this.initialChart()
      })
    }
  },
  mounted() {
    this.initialChart()
    //图表大小自适应
    window.addEventListener('resize', this.onResize)
  },
  methods: {
    destroyChart() {
      if (this.myChart != null) {
        this.myChart.dispose()
        this.myChart = null
      }
    },
    onResize() {
      if (this.myChart) {
        this.myChart.resize()
      }
    },
    initialChart() {
      let titleData = []
      let yAxisData = []
      let finishData = []
      let totalData = []
      let res = [
        { name: '设计入库', percent: '86.47%', totalAmout: '24.395', finishAmout: '21.095' },
        { name: '扫码施工', percent: '75.88%', totalAmout: '17.8288', finishAmout: '13.5288' }
      ]
      res.forEach((item) => {
        titleData.push(item.name)
        yAxisData.push(item.name)
        finishData.push(item.finishAmout)
        totalData.push(item.totalAmout)
      })
      let option = {
        backgroundColor: 'transparent',
        title: {
          text: this.title,
          textStyle: {
            fontSize: fontSize(14),
            color: '#2BA7FF'
          },
          left: '0',
          top: '0'
        },
        grid: {
          top: '30%',
          left: '-10%',
          right: '10%',
          bottom: '-20%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'none'
          },
          borderWidth: 0,
          textStyle: {
            color: '#fff'
          },
          backgroundColor: 'rgba(50,50,50,0.7)',
          formatter: function (params) {
            return (
              params[0].name +
              '<br/>' +
              "<span style='display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:rgba(36,207,233,0.9)'></span>" +
              params[0].seriesName +
              ' : ' +
              params[0].data +
              '亿' +
              '<br/>'
            )
          }
        },
        xAxis: {
          show: false,
          type: 'value'
        },
        yAxis: [
          {
            type: 'category',
            inverse: true,
            show: false,
            data: titleData
          },
          {
            position: 'left',
            type: 'category',
            inverse: true,
            axisTick: 'none',
            axisLine: 'none',
            show: true,
            axisLabel: {
              color: '#ffffff',
              formatter: function (value) {
                let str
                str = `{b|${value}}`
                return str
              },
              rich: {
                a: {
                  color: '#FFFFFF',
                  fontWeight: 500,
                  fontFamily: 'Source Han Sans CN'
                },
                b: {
                  color: '#FEFFFF',
                  fontWeight: 400,
                  fontFamily: 'Source Han Sans CN',
                  fontSize: fontSize(14)
                }
              }
            },
            data: yAxisData
          }
        ],
        series: [
          {
            type: 'bar',
            name: '长度',
            zlevel: 1,
            label: {
              show: true,
              position: 'right',
              fontSize: fontSize(14),
              color: '#FFFFFF',
              formatter: function (params) {
                return params.value + 'km'
              }
            },
            itemStyle: {
              borderRadius: 30,
              color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                {
                  offset: 0,
                  color: 'rgba(13, 128, 241, 1)'
                },
                {
                  offset: 1,
                  color: 'rgba(2, 179, 195, 1)'
                }
              ])
            },
            barWidth: 8,
            data: finishData
          }
        ]
      }
      this.creatChart(option, this.$refs.chart)
    },
    creatChart(option, ref) {
      this.myChart = echarts.init(ref) //this.$refs.chart
      this.myChart.resize()
      this.myChart.setOption(option, {
        notMerge: true
      })
    }
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.onResize)
    this.destroyChart()
  }
}
</script>

<style lang='scss' scoped>
.chart {
  height: 100%;
  width: 100%;
}
</style>