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

<script>
import { mapState } from 'vuex';
export default {
  name: "ChartWater",
  data() {
    return {
      myChart: null
    }
  },
  props: {
    id: {
      type: String,
      default: () => '0'
    },
    dataValue: {
      type: Array,
      default: () => []
    },
    room: {
      type: String,
      default: () => '总'
    },
  },
  computed: {
    ...mapState(['color'])
  },
  watch: {
    color(newVal) {
      if(newVal) {
        this.myChart.clear();
        this.init()
      }
    },
    dataValue: {
      handler(newVal) {
        if(newVal) {
          this.myChart.clear();
          this.init()
        }
      },
      deep: true
    },
  },
  methods: {
    init() {
      let dataAxis = [];
      let data = [];
      if(this.dataValue.length > 0) {
        this.dataValue.forEach(item => {
          dataAxis.push(item.createTime)
          data.push(item.value)
        })
      }
      let start = dataAxis.length - 12;
      let end = dataAxis.length - 1;
      let options = {
        title: {
          text: this.room + '用水量统计',
          left: 'center'
        },
        tooltip: {
          trigger: 'axis',
        },
        color: [this.color],
        dataZoom: [{
          type: 'inside',
          startValue: start,
          endValue: end,
          show: false

        }],
        xAxis: [
          {
            type: 'category',
            data: dataAxis,
            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: '水量',
            type: 'line',
            // stack: 'Total',
            smooth: true,
            areaStyle: {},
            emphasis: {
              focus: 'series'
            },
            itemStyle : {
              color: this.color,
              borderColor: this.color,
              normal: {
                label : {
                  show: true,
                  textStyle: {
                    fontSize: 14
                  }
                }
              }
            },
            data: data
          }
        ]
      }
      this.myChart.setOption(options)
    }
  },
  mounted() {
    this.myChart = this.$echarts.init(document.getElementById('chartWater-' + this.id));
    this.init()
  }
}
</script>

<style scoped>

</style>
