<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";
import { fondStatistics } from "@/api/dashboard";

const animationDuration = 4200;
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
      // default: (window.innerHeight-260)/2+"px"
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      chart: null,
      chartOption: {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
        },

        grid: {
          top: "5%",
          left: "0%",
          right: "0%",
          bottom: "0%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            // data: ['文书', '科技', '会计', '声像', '实物', '专业', '图像'],
            axisTick: {
              show: true,
              alignWithLabel: true,
            },
            // axisPointer: { // 坐标轴指示器，坐标轴触发有效
            //   type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            // },
            axisLine: {
              show: true, //不显示坐标轴轴线
              lineStyle: {
                color: "#3a4975",
              },
            },
            axisLabel: {
              interval: 0, // 坐标轴刻度标签的显示间隔
              // rotate: 40 // 标签倾斜的角度
              textStyle: {
                fontSize: 8, //刻度大小
                color: "#a8d4f9",
              },
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            // show: false,

            splitLine: {
              show: false, // 不显示网格线
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: true, //不显示坐标轴轴线
              lineStyle: {
                color: "#3a4975",
              },
            },
            axisLabel: {
              textStyle: {
                fontSize: 10, //刻度大小
                color: "#a8d4f9",
              },
            },
          },
        ],
        series: [
          {
            // name: 'Direct',
            color: "#3451b0",
            type: "bar",
            barWidth: "40%",
            // data: [79, 52, 200, 45, 70, 50, 100],
            animationDuration,
          },
        ],
      },
    };
  },
  methods: {
    fondStatistics() {
      fondStatistics().then((res) => {
        console.log(res);
        if (res.code === "100") {
          let labelArr = [];
          let dataArr = [];
          let sumAll = null;

          res.body.list.forEach((ele) => {
            labelArr.push(ele.name);
            dataArr.push(ele.value);
            sumAll = sumAll + ele.value;
          });
          this.chartOption.xAxis[0].data = labelArr;
          this.chartOption.series[0].data = dataArr;
          this.chart.setOption(this.chartOption);
        }
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.fondStatistics();
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
};
</script>
