<template>
  <div ref="chart" class="user-type-stack-bar-chart"></div>
</template>

<script>
export default {
  name: "UserTypeStackBarChart",
  props: {
    categories: {
      type: Array,
      default() {
        return [
          "白芨屯",
          "七树庄",
          "王浒圳",
          "新罕电",
          "小张各庄",
          "欢喜庄",
          "李朝庄",
        ];
      },
    },
    residential: {
      type: Array,
      default() {
        return [22, 14, 24, 22, 26, 18, 28];
      },
    },
    industrial: {
      type: Array,
      default() {
        return [10, 8, 12, 10, 6, 16, 4];
      },
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.initChart();
    window.addEventListener("resize", this.handleResize);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.handleResize);
    if (this.chart) {
      this.chart.dispose();
      this.chart = null;
    }
  },
  watch: {
    categories() {
      this.updateChart();
    },
    residential() {
      this.updateChart();
    },
    industrial() {
      this.updateChart();
    },
  },
  methods: {
    initChart() {
      if (!this.$refs.chart) {
        return;
      }
      this.chart = this.$echarts.init(this.$refs.chart);
      this.updateChart();
    },
    handleResize() {
      if (this.chart) {
        this.chart.resize();
      }
    },
    updateChart() {
      if (!this.chart) {
        return;
      }
      this.chart.setOption({
        grid: {
          left: 6,
          right: 6,
          top: 28,
          bottom: 0,
          containLabel: true,
        },
        legend: {
          top: 0,
          right: 10,
          itemWidth: 16,
          itemHeight: 8,
          textStyle: {
            color: "#ffffff",
            fontSize: 16,
          },
        },
        xAxis: {
          type: "category",
          data: this.categories,
          axisLine: { lineStyle: { color: "rgba(19,253,245,0.19)" } },
          axisTick: {
            show: true,
            inside: true,
            length: 1.5,
            width:2,
            lineStyle: { color: "#2CFFFF" },
          },
          axisLabel: {
            color: "#ffffff",
            fontSize: 11,
          },
        },
        yAxis: {
          type: "value",
          axisLine: { show: false },
          axisTick: { show: false },
          axisLabel: { show: false },
          splitLine: {
            lineStyle: { color: "rgba(19,253,245,0.19)" },
          },
        },
        series: [
          {
            name: "居民用户",
            type: "bar",
            stack: "total",
            data: this.residential,
            barWidth: 14,
            itemStyle: {
              borderRadius: [2, 2, 0, 0],
              color: "#20c8ff",
            },
          },
          {
            name: "工商业用户",
            type: "bar",
            stack: "total",
            data: this.industrial,
            barWidth: 14,
            itemStyle: {
              borderRadius: [2, 2, 0, 0],
              color: "#ff6b3d",
            },
          },
        ],
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.user-type-stack-bar-chart {
  width: 100%;
  height: 200px;
}
</style>
