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

<script>
import * as echarts from 'echarts'
import { fontSize } from '../../util'
export default {
  name: 'ComContractBarChart', //合同结算柱状图
  props: {
    chartData: {}
  },
  data() {
    return { myChart: null }
  },
  watch: {
    chartData: {
      handler(n, o) {
        this.initialChart()
      }
    }
  },
  mounted() {
    //图表大小自适应
    window.addEventListener('resize', this.onResize)
  },
  methods: {
    destroyChart() {
      if (this.myChart != null) {
        this.myChart.dispose()
        this.myChart = null
      }
    },
    onResize() {
      this.initialChart()
    },
    initialChart() {
      this.destroyChart()
      const { xData, seriesData } = this.chartData || {}
      let option = {
        grid: {
          top: '30%',
          right: '3%',
          left: '5%',
          bottom: '15%'
          //   containLabel: true
        },
        tooltip: {
          confine: true,
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              type: 'solid',
              color: 'rgba(255, 255, 255, 0.3)'
            }
          },
          textStyle: {
            color: '#fff'
          },
          borderWidth: 0,
          backgroundColor: 'rgba(50,50,50,0.7)'
        },
        xAxis: {
          type: 'category',
          axisTick: { show: false },
          axisLine: { show: false },
          axisLabel: {
            fontSize: 14,
            fontFamily: 'Source Han Sans CN',
            fontWeight: 300,
            color: '#FFFFFF'
          },
          data: xData
        },
        yAxis: {
          type: 'value',
          name: '万元',
          nameGap: fontSize(35),
          nameTextStyle: {
            align: 'right',
            fontSize: fontSize(14),
            fontFamily: 'Source Han Sans CN',
            fontWeight: 300,
            color: '#FFFFFF'
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.2)'
            }
          },
          axisLabel: {
            fontSize: fontSize(14),
            fontFamily: 'Source Han Sans CN',
            fontWeight: 300,
            color: '#FFFFFF'
          }
        },
        series: [
          {
            data: seriesData,
            type: 'bar',
            barWidth: 10,
            itemStyle: {
              borderRadius: 5,
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: '#0A85F7'
                },
                {
                  offset: 1,
                  color: '#05C7DA'
                }
              ])
            }
          }
        ]
      }
      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 {
  width: 100%;
  height: 100%;
}
</style>