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

<script>
import * as echarts from 'echarts'

export default {
  name: 'BarChart',
  props: {
    config: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      chart: null,
      mockData: {
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: [120, 200, 150, 80, 70, 110, 130],
          type: 'bar',
          itemStyle: {
            color: '#409EFF'
          }
        }]
      }
    }
  },
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.chart)
      this.chart.setOption(this.mockData)
      window.addEventListener('resize', this.resizeHandler)
    },
    resizeHandler() {
      this.chart.resize()
    }
  },
  beforeUnmount() {
    window.removeEventListener('resize', this.resizeHandler)
    if (this.chart) {
      this.chart.dispose()
    }
  }
}
</script>

<style scoped>
.bar-chart {
  width: 100%;
  height: 100%;
}
</style>
