<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: {
    monthArray: {
      type: Array,
      default: []
    },
  },
  data() {
    return {
      chartObj: null,
      ooption: {
        xdata: [],
        teacher: [],
        student: [],
      },
      option: {
        // title: {
        //     text: '登录频次',
        //     textStyle:{
        //         fontSize: 12,
        //     },
        //     padding: [0, 0, 0, 10]
        // },
        tooltip: {
          trigger: 'axis'
        },
        // legend: {
        //     data: ['老师', '学生'],
        //     right: '10'
        // },
        grid: {
          top: '30',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: true,
          // data: ['1月', '2月', '3月', '4月', '5月', '6月']
          data: []
        },
        yAxis: {
          type: 'value',
          minInterval: 1
        },
        series: [
          {
            // name: '老师',
            type: 'line',
            areaStyle: {},
            data: [],
            itemStyle: {
              normal: {
                color: function (params) {
                  return "#106bff";
                },
              },
            },
          }
        ]
      },
    };
  },
  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.user;
        // 初始化雷达图
        this.chartObj = chartObj;
        this.chartObj.setOption(this.option);
      });
    },
    setJson(array) {
      this.ooption = {
        xdata: [],
        user: [],
      }
      let _array = array
      for (var i = 0; i < _array.length; i++) {
        this.ooption.xdata.push(_array[i].Month + '月')
        this.ooption.user.push(_array[i].user)
      }

      if (!this.chartObj) {
        this.setChart(this.ooption);
      } else {
        this.option.xAxis.data = this.ooption.xdata;
        this.option.series[0].data = this.ooption.user;
        this.chartObj.setOption(this.option);
      }
    }
  },
  watch: {
    monthArray: {
      immediate: true,
      deep: true,
      handler(newValue, oldValue) {
        this.setJson(newValue)
        this.$forceUpdate();
      },
    },
  },
  mounted() {
    this.setJson(this.monthArray)

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

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