<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 { yuanwendaxiao, propertyStatistics } 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'
          }
        },
        legend: {},
        grid: {
          top: "5%",
          left: "0%",
          right: "0%",
          bottom: "0%",
          containLabel: true,
        },
        xAxis: {
          type: 'value',
          boundaryGap: [0, 0.01],
          axisLine: {
            show: false, //不显示坐标轴轴线
            lineStyle: {
              color: "#a0a0a0",
            },
          },
          axisLabel: {
            interval: 0, // 坐标轴刻度标签的显示间隔

            // rotate: 40 // 标签倾斜的角度
            textStyle: {
              fontSize: 10, //刻度大小
              color: "#a0a0a0",
            },
          },
        },
        yAxis: {
          type: 'category',
          data: [], //['专利']
          splitLine: {
            show: false, // 不显示网格线
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false, //不显示坐标轴轴线
            lineStyle: {
              color: "#a0a0a0",
            },
          },
          axisLabel: {
            textStyle: {
              fontSize: 10, //刻度大小
              color: "#a0a0a0",
            },
          },
        },
        series: [
          {
            name: '',
            type: 'bar',
            barWidth:15,
            label:{
              normal:{
                show:true,
                position:'right'
              }
            },
            itemStyle:{
              normal:{
                color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [
                  { offset: 0, color: '#118ced'},
                  { offset: 0.5, color: '#65aef6'},
                  { offset: 1, color: '#e4f4fc' }
                ])
              }
            },
            data: [], //[123,2312,]
          }
        ]
      }
    };
  },
  methods: {
    yuanwendaxiao() {
      propertyStatistics().then((res) => {
        let _yAxisData = [];
        let _seriesData = [];
        if (res.code === "100") {
          res.body.list.forEach((item) => {
            _yAxisData = Object.keys(item);
            _seriesData = Object.values(item);
          });
          this.chartOption.yAxis.data = _yAxisData;
          this.chartOption.series[0].data=_seriesData;
          this.chart.setOption(this.chartOption);
        }
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.yuanwendaxiao();
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
};
</script>
