<template>
  <div class="data_body">
    <div style="width: 100%; height: 100%">
      <div id="charts_canvas" class="echart" style="width: 100%; height: 100%; "></div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    yearArray: {
      type: Array,
    },
  },
  data() {
    return {
      chartObj: null,
      ooption: {
        xdata: [],
        sdata: [],
      },
      option: {
        // title: {
        //   text: '登录频次',
        //   textStyle: {
        //     fontSize: 12,
        //   },
        //   padding: [10, 0, 0, 10]
        // },
        tooltip: {
          position: 'top',
          formatter: function(params){
            return params.name + '  ' + params.data[2] + '(小时)';
          }
        },
        grid: {
          top: '50',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          // data: [
          //     '1月', '2月', '3月', '4月', '5月', '6月', '7月',
          //     '8月', '9月', '10月', '11月', '12月'
          // ],
          data: [],
          splitArea: {
            show: true
          },
          axisLabel: {
            formatter: function (value) {
              console.log(value);
              var ret = "";//拼接加\n返回的类目项  
              var maxLength = 2;//每项显示文字个数  
              var valLength = value.length;//X轴类目项的文字个数  
              var rowN = Math.ceil(valLength / maxLength); //类目项需要换行的行数  
              if (rowN > 1)//如果类目项的文字大于5,  
              {
                for (var i = 0; i < rowN; i++) {
                  var temp = "";//每次截取的字符串  
                  var start = i * maxLength;//开始截取的位置  
                  var end = start + maxLength;//结束截取的位置  
                  //这里也可以加一个是否是最后一行的判断，但是不加也没有影响，那就不加吧  
                  temp = value.substring(start, end) + "\n";
                  ret += temp; //凭借最终的字符串  
                }
                return ret;
              }
              else {
                return value;
              }
            }
          }
        },
        yAxis: {
          type: 'category',
          data: [
            '星期天', '星期一', '星期二', '星期三',
            '星期四', '星期五', '星期六',
          ],
          splitArea: {
            show: true
          },
        },
        visualMap: {
          min: 0,
          max: 10,
          calculable: true,
          orient: 'horizontal',
          right: '10',
          top: '0%'
        },
        series: [
          {
            name: '',
            type: 'heatmap',
            data: [],
            label: {
              show: true
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      },
    };
  },
  methods: {
    setChart(option) {
      // 雷达图显示的标签
      let newPromise = new Promise((resolve) => {
        resolve();
      });
      //然后异步执行echarts的初始化函数
      newPromise.then(() => {
        const chartObj = this.$echarts.init(
          //劳动课程
          this.$el.querySelector("#charts_canvas")
        );

        this.option.xAxis.data = option.xdata;
        this.option.series[0].data = option.sdata;
        this.option.visualMap.max = option.max ? option.max : 0;
        // 初始化雷达图
        this.chartObj = chartObj;
        this.chartObj.setOption(this.option);
      });
    },
    setJson(array) {
      this.ooption = {
        xdata: [],
        sdata: [],
        max: 0
      }
      let _array = array
      let max = []
      for (var i = 0; i < _array.length; i++) {
        this.ooption.xdata.push(_array[i].Month + '月')
        this.ooption.sdata.push([i, 0, _array[i].sun])
        this.ooption.sdata.push([i, 1, _array[i].mon])
        this.ooption.sdata.push([i, 2, _array[i].tue])
        this.ooption.sdata.push([i, 3, _array[i].wed])
        this.ooption.sdata.push([i, 4, _array[i].thur])
        this.ooption.sdata.push([i, 5, _array[i].fri])
        this.ooption.sdata.push([i, 6, _array[i].sat])
        // let _data = [_array[i].Month-1,] //[月份，星期，值]
        max.push(_array[i].sun)
        max.push(_array[i].mon)
        max.push(_array[i].tue)
        max.push(_array[i].wed)
        max.push(_array[i].thur)
        max.push(_array[i].fri)
        max.push(_array[i].sat)
      }
      this.ooption.max = max.sort(function (a, b) {
        return b - a;
      })[0];
      if (!this.chartObj) {
        this.setChart(this.ooption);
      } else {
        this.option.xAxis.data = this.ooption.xdata;
        this.option.series[0].data = this.ooption.sdata;
        this.option.visualMap.max = this.ooption.max ? this.ooption.max : 0;
        this.chartObj.setOption(this.option);
      }
    }
  },
  watch: {
    yearArray: {
      immediate: true,
      deep: true,
      handler(newValue, oldValue) {
        this.setJson(newValue)
        this.$forceUpdate();
      },
    },
  },
  mounted() {
    this.setJson(this.yearArray)

    window.addEventListener("resize", () => {
      if (_this.chartObj) {
        _this.chartObj.resize();
      }
    });
  },
};
</script>

<style scoped>
.data_body {
  height: 100%;
  position: relative;
  border-radius: 5px;
  margin: 0 auto;
  box-sizing: border-box;
  padding: 0;
  width: 95%;
  background: #fff;
}
</style>
