<template>
  <div :id="'chart-one-' + id" style="width: 100%; height: 100%"></div>
</template>

<script>
import { mapState } from "vuex";

export default {
  name: "ChartStarkBarOne",
  props: {
    id: {
      type: String,
      default: () => '0'
    },
    starkList: {
      type: Array,
      default: () => []
    }
  },
  watch: {
    starkList: {
      handler(newVal) {
        if(newVal) {
          this.myChart.clear();
          this.init()
        }
      },
      deep: true
    },
    color(newVal) {
      if(newVal) {
        this.myChart.clear();
        this.init()
      }
    }
  },
  computed: {
    ...mapState(['color'])
  },
  data() {
    return {
      myChart: null
    }
  },
  methods: {
    init() {
      var time = [];
      var data1 = [];
      this.starkList.forEach(item => {
        time.push(item.createTime);
        data1.push(item.value)
      })
      let start = time.length - 8;
      let end = time.length - 1;
      var option = {
        tooltip: {
          trigger: 'axis',
        },
        dataZoom: [{
          type: 'inside',
          startValue: start,
          endValue: end,
          show: false
        }],
        color: [this.color],
        xAxis: [
          {
            type: 'category',
            data: time,
            axisPointer: {
              type: 'shadow'
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: '#6e7079',
              }
            },
            axisTick:{
              show:false
            },
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '用料统计情况',
            axisLabel: {
              formatter: '{value} 吨'
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: '#6e7079',
              }
            },
            axisTick:{
              show:false
            },
          }
        ],
        series: [
          {
            name: '1号楼',
            data: data1,
            type: 'bar',
            barWidth : 50,
            itemStyle : {
              color: this.color,
              borderColor: this.color,
              normal: {
                label : {
                  show: true,
                  position: 'top',
                  textStyle: {
                    fontSize: 14
                  }
                }
              }
            },
          },
        ]
      };
      this.myChart.setOption(option)
    }
  },
  mounted() {
    this.myChart = this.$echarts.init(document.getElementById('chart-one-' + this.id));
    this.init()
  }
}
</script>

<style scoped>

</style>
