<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
// require("echarts/theme/macarons"); // echarts theme
// import resize from "./mixins/resize";

export default {
  // mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      // required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ expectedData, actualData, keys, arr } = {}) {
      this.chart.setOption({
        tooltip: {},
        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            data: [
              "2021-01",
              "2021-02",
              "2021-03",
              "2021-04",
              "2021-05",
              "2021-06",
              "2021-07",
              "2021-08",
              "2021-09",
              "2021-10",
              "2021-11",
              "2021-12",
            ],
            axisPointer: {
              type: "shadow",
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            min: 0,
            axisLabel: {
              formatter: "{value}",
            },
            splitLine: {
              show: false, //不显示网格线
            },
            splitArea: {
              show: false, //不显示网格区域
            },
          },
        ],
        grid: {
          containLabel: true,
          width: "99%",
          left: 0,
          top: 30,
          bottom: 0,
        },
        series: [
          {
            name: "",
            type: "line",
            data: [
              2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2,
            ],
            areaStyle: {
              color: "#dbeefc",
            },
            itemStyle: {
              normal: {
                color: "rgba(73, 169,238, 1)",
              },
            },
            // symbol: "none",
            // smooth: true,
            // showSymbol: false,
          },
        ],
      });
    },
  },
};
</script>
