<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: {
    loginArray: {
      type: Array
    },
  },
  data() {
    return {
      chartObj: null,
      ooption: {
        xdata: [],
        type: [],
      },
      option: {
        tooltip: {
          trigger: "item",
          formatter: "{b}   {c}(次)",
        },
        grid: {
          top: '15%',
          left: '5%',
          right: '5%',
          bottom: '15%',
          containLabel: true
        },
        xAxis: {
          type: "category",
          data: ["老师", "学生"],
        },
        yAxis: {
          type: "value",
          data: [],
        },
        series: [
          {
            data: [],
            barWidth: 30,
            type: "bar",
          },
        ],
      },
    };
  },
  methods: {
    setChart(array) {
      // 雷达图显示的标签
      let newPromise = new Promise((resolve) => {
        resolve();
      });
      //然后异步执行echarts的初始化函数
      newPromise.then(() => {
        const chartObj = this.$echarts.init(
          this.$el.querySelector("#charts_canvas")
        );
        this.option.series[0].data = array;
        // 初始化雷达图
        this.chartObj = chartObj;
        this.chartObj.setOption(this.option);
      });
    },
    setJson(array) {
      this.ooption = {
        xdata: [],
        sdata: [],
        max: 0
      }

      if (!this.chartObj) {
        this.setChart(array);
      } else {
        this.option.series[0].data = array;
        this.chartObj.setOption(this.option);
      }
    }
  },
  watch: {
    loginArray: {
      immediate: true,
      deep: true,
      handler(newValue, oldValue) {
        this.setJson(newValue)
        this.$forceUpdate();
      },
    },
  },
  mounted() {
    this.setJson(this.loginArray)
    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>