<template>
  <div>
    <div class="title" v-if="title">{{title}}</div>
    <div class="barChart" :id='"barChart"+index' :key="index"/>
  </div>
</template>

<script>
  import theme from '../../../styles/element-ui.scss';
  import elementResizeDetectorMaker from 'element-resize-detector'
  import * as echarts from 'echarts';
  export default {
    name: "BarChart",
    props: {
      index: {
        type: Number|String,
        default: 0
      },
      chartData: {
        type: Object,
        default: () => ({
          xdata: [],
          ydata: []
        })
      },
      title: {
        type: String
      },
      unit: {
        type: String,
        default: ''
      },
      leftUnit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        chart: null
      }
    },
    watch: {
      chartData: {
        deep: true,
        handler(val) {
          this.setOptions()
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
      const _this = this;
      const erd = elementResizeDetectorMaker()
      erd.listenTo(document.getElementById('barChart'+this.index), element => {
        _this.$nextTick(() => {
          _this.chart.resize()
        })
      })
    },
    methods: {
      initChart() {
        this.chart = echarts.init(document.getElementById('barChart'+this.index), 'macarons')
        this.setOptions()
      },
      setOptions() {
        var ydata = this.chartData.ydata
        if (ydata && ydata.length > 1) {
          const p = [ydata[0], {
            value: ydata[1],
            itemStyle: {
              color: theme.colorPrimary
            }
          }]
          ydata = p
        }
        this.chart.setOption({
          xAxis: {
            data: this.chartData.xdata,
            axisTick: {
              show: false
            },
            axisLabel: {
              color: '#333',
              fontSize: 12
            },
            axisLine: {
              lineStyle: {
                color: '#ffffff'
              }
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: '#FDEDFA'
              }
            }
          },
          yAxis: {
            show: false,
            axisLabel: {
              show: false,
              color: '#5E5E5E'
            },
            axisLine: {
              lineStyle: {
                color: '#fff'
              }
            },
            splitLine: {
              show: false
            }
          },
          grid: {
            show: true,
            bottom: 5,
            top: 35,
            borderColor: '#fff',
            containLabel: true
          },
          series: [{
            type: 'bar',
            itemStyle: {
              color: theme.colorAccent,
              barBorderRadius: [4, 4, 0, 0]
            },
            label: {
              show: true,
              color: '#000',
              position: 'top',
              fontWeight: 'bold',
              formatter: params => {
                var v = params.data.value !== undefined ? params.data.value : params.data
                if (this.leftUnit)
                  return this.unit + v
                else
                  return v + this.unit
              }
            },
            barWidth: 40,
            data: ydata,
            coordinateSystem: 'cartesian2d'
          }]
        })
      }
    }
  }
</script>

<style scoped="scoped" lang="scss">
  @import '../../../styles/element-ui.scss';
  .title {
    color: #333;
    text-align: center;
    font-size: 14px;
    position: relative;
    padding-bottom: 5px;
  }
  .title::after {
    left: 50%;
    bottom: -5px;
    width: 50px;
    height: 5px;
    content: '';
    border-radius: 10px;
    margin-left: -25px;
    background-color: $--color-primary;
    position: absolute;
  }
  .barChart {
    width: 100%;
    height: 18vh;
  }
</style>
