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

const animationDuration = 4000;
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'
          }
        },
        xAxis: {
          type: 'category',
          data: [],
          axisLine: {
            show: false, //不显示坐标轴轴线
            lineStyle: {
              color: "#a0a0a0",
            },
          },
          axisLabel: {
            interval: 0, // 坐标轴刻度标签的显示间隔
            rotate: 40, // 标签倾斜的角度
            textStyle: {
              fontSize: 10, //刻度大小
              color: "#a0a0a0",
            },
          },
        },
        grid: {
          top: "6%",
          left: "0%",
          right: "0%",
          bottom: "3%",
          containLabel: true,
        },
        yAxis: {
          type: 'value',
          splitLine: {
            show: false, // 不显示网格线
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false, //不显示坐标轴轴线
            lineStyle: {
              color: "#a0a0a0",
            },
          },
          axisLabel: {
            textStyle: {
              fontSize: 10, //刻度大小
              color: "#a0a0a0",
            },
          },
        },
        series: [
          {
            data: [],
            type: 'bar',
            barWidth:15,
            label:{
              normal:{
                show:true,
                position:'top'
              }
            },
            itemStyle:{
              normal:{
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: '#118ced'},
                  { offset: 0.5, color: '#65aef6'},
                  { offset: 1, color: '#e4f4fc' }
                ])
              }
            }
          }
        ]
      }
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    imageStatistics() {
      imageStatistics().then((res) => {
        if (res.code === "100") {
          let _xAxisData = [];
          let _seriesData = [];
          let sumAll = null;
          res.body.list.forEach((ele) => {
            _xAxisData.push(ele.name)
            _seriesData.push(ele.value);
            sumAll = sumAll + ele.value;
          });
          this.$emit("sunYW", sumAll);
          this.chartOption.xAxis.data = _xAxisData;
          this.chartOption.series[0].data = _seriesData;
          this.chart.setOption(this.chartOption);
        }
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.imageStatistics();
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
};
</script>
