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

<script>
import echarts from 'echarts'
import {debounce} from '../utils/debounce'

export default {
  name: 'barChart',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData:function () {
      this.$nextTick(()=>{
        this.initChart()
      })
    }
  },
  mounted() {
    // this.initChart()
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHandler)
    }

    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOptions({xData, yData} = {}) {
      this.chart.setOption({
        xAxis: {
          boundaryGap: true,
          axisLabel : {
            formatter: '{value}',
            textStyle: {
              color: '#59AAF7'
            }
          },
          axisLine:{
            lineStyle:{
              color:'rgba(89,170,247,.3)'
            }
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false
          },
          data: [
            {
              value: xData[0],
              textStyle: {
                fontSize: 12,
                color: '#59AAF7'
              }
            },
            {
              value: xData[1],
              textStyle: {
                fontSize: 12,
                color: '#FFB900'
              }
            },
            {
              value: xData[2],
              textStyle: {
                fontSize: 12,
                color: '#FD4B15'
              }
            }
          ]
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 10,
          top: 20,
          containLabel: true
        },
        tooltip: {

        },
        yAxis: {
          axisLabel : {
            formatter: '{value}',
            textStyle: {
              color: '#fff'
            }
          },
          axisLine:{
            lineStyle:{
              color:'rgba(89,170,247,.3)'
            }
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false
          },
          splitNumber:3
        },
        series: [{
          type: 'bar',
          barWidth:25,
          data: [
            {
              name: xData[0],
              value: yData[0],
              label: {
                show: true,
                position: 'top',
                textStyle: {
                  fontSize: 14,
                  color: '#01AEFE'
                }
              },
              itemStyle: {
                normal: {
                  color: new echarts.graphic.LinearGradient(
                    0, 0, 0, 1,
                    [
                      { offset: 0, color: '#635BE6' },
                      { offset: 1, color: '#6DB7EF' }
                    ]
                  )
                }
              }
            },
            {
              name: xData[1],
              value: yData[1],
              label: {
                show: true,
                position: 'top',
                textStyle: {
                  fontSize: 14,
                  color: '#FFB700'
                }
              },
              itemStyle: {
                normal: {
                  color: new echarts.graphic.LinearGradient(
                    0, 0, 0, 1,
                    [
                      { offset: 0, color: '#F9B205' },
                      { offset: 1, color: '#F2E34A' }
                    ]
                  )
                }
              }
            },
            {
              name: xData[2],
              value: yData[2],
              label: {
                show: true,
                position: 'top',
                textStyle: {
                  fontSize: 14,
                  color: '#FD4B15'
                }
              },
              itemStyle: {
                normal: {
                  color: new echarts.graphic.LinearGradient(
                    0, 0, 0, 1,
                    [
                      { offset: 0, color: '#F36C00' },
                      { offset: 1, color: '#F4C5A8' }
                    ]
                  )
                }
              }
            }
          ]
        }]
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    }
  }
}
</script>
