<template>
  <div :id="domId" style="height: 100%;"></div>
</template>

<script>
import { generateUUID } from '@/utils/index'
export default {
  name: 'bar-chart',
  props: {
    legend: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: ''
    },
    dataItem: {
      type: Array,
      default: () => {}
    }
  },
  data () {
    return {
      domId: generateUUID(),
      chart: null,
      option: {
        title: {
          text: this.title,
          top: '5',
          textStyle: {
            fontWeight: 'normal',
            fontSize: 16,
            color: '#0D47A1'
          }
        },
        legend: {
          show: this.legend,
          top: '10'
        },
        tooltip: {},
        grid: {
          top: '40',
          bottom: '30'
        },
        dataset: {
          source: this.dataItem
        },
        xAxis: { type: 'category' },
        yAxis: {},
        series: []
      }
    }
  },
  created () {
    const num = this.dataItem[0].length - 1
    this.option.series = new Array(num).fill({ type: 'bar' })
  },
  mounted () {
    this.initMap()
    this.chart.on('click', (param) => {
      this.$emit('click', param)
    })
  },
  methods: {
    initMap () {
      this.chart = this.$echarts.init(document.getElementById(this.domId), null, { renderer: 'svg' })
      this.chart.setOption(this.option)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
