<template>
  <div id="main_bar_chart" @click="goChart"/>
</template>

<script>
import * as echarts from "echarts";

export default {
  name: "MainBarChart",
  data() {
    return {
      barChart: {
        textStyle: {
          color: "#FFFFFF",
          fontSize: "12"
        },
        tooltip: {},
        grid: {
          top: "10%",
          bottom: "10%"
        },
        xAxis: {
          data: ['未结算未开票', '已结算未开票', '已开票未收款', '应收款', '已开票已收款'],
          axisLabel: {
            color: "#FFFFFF",
            fontSize: "12",
            margin: "15"
          },
          axisTick: {
            show: false
          }
        },
        yAxis: [{
          type: "value",
          splitLine: {
            lineStyle: {
              color: "#1E2C58"
            }
          },
          axisLabel: {
            show: false,
            color: "#FFFFFFAA",
            fontSize: "16"
          }
        }],
        series: [
          {
            name: '应收账款',
            type: 'bar',
            data: [5002, 57873, 1987, 71168, 41098],
            barWidth: "40%",
            showBackground: true,
            backgroundStyle: {
              color: 'rgba(255, 255, 255, 0.1)'
            },
            itemStyle: {
              barBorderRadius: 10,
            },
            color: {
              type: 'linear',
              x: 0, y: 0, x2: 0, y2: 1,
              colorStops: [
                {offset: 0, color: '#00CCD2'},
                {offset: 1, color: '#00A2FF'}
              ]
            },
            label: {
              show: true,
              position: 'top',
              color: "#FFFFFF",
            },
          }
        ]
      },
      myChart: null
    }
  },
  methods: {
    resizeChart() {
      if (this.myChart) {
        this.myChart.resize();
      }
    },
    goChart() {
      this.$router.push({path: "/chart"})
    }
  },
  mounted() {
    // 设置500毫秒延迟执行，解决组件没有渲染完成就开始绘制图表的问题
    setTimeout(() => {
      let mainBarChart = document.getElementById('main_bar_chart')
      this.myChart = echarts.init(mainBarChart)
      this.myChart.setOption(this.barChart);
      // 监听窗口大小变化，重绘图表
      window.addEventListener('resize', this.resizeChart);
    }, 500)
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeChart);
  }

}

</script>
