<template>
  <div class="chart" :id="id"></div>
</template>

<script>
import util from "@/helper/util.js";
import partten from "@/helper/partten.js";
import * as echarts from "echarts";

export default {
  name: "multiple-bar-chart",
  componentName: "multiple-bar-chart",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "800px",
    },
    // 传入数据
    data: {
      type: Object,
      default: {
        area: ["新荣区", "平城区", "云冈区", "云州区", "阳高县", "天镇县", "广灵县", "浑源县", "左云县"],
        legend: ["因病", "因残", "因学", "因灾", "缺土地", "缺水"],
        data: [
          [1320, 1302, 901, 634, 1390, 1330, 1320, 1000, 500],
          [320, 302, 301, 334, 390, 330, 320, 100, 50],
          [320, 302, 301, 334, 390, 330, 320, 100, 50],
          [320, 302, 301, 334, 390, 330, 320, 100, 50],
          [320, 302, 301, 334, 390, 330, 320, 100, 50],
          [320, 302, 301, 334, 390, 330, 320, 100, 50],
        ],
      },
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      color: ["#323E6F", "#1DA0D7", "#02BB4C", "#DB5520", "#EDB32F", "#EDEB2F"],
    };
  },
  computed: {
    datas() {
      return this.list.map((t) => {
        return t.value;
      });
    },
    xdata() {
      return this.list[0].value.map((t) => {
        return t.text;
      });
    },
    ydata() {
      let result = [];

      return result;
    },
    series() {
      let result = [];
      return result;
    },
  },
  methods: {
    initChart() {
      let option = {
        color: this.color,
        grid: {
          left: 40,
          right: 16,
          bottom: 16,
          top: 16,
          containLabel: true,
        },
        tooltip: {
          trigger: "item",
          backgroundColor: "rgba(0,0,0,0.4)",
          textStyle: {
            color: "#fff",
            fontSize: util.vh(16),
          },
          formatter: function(param) {
            return param.name + "<br >" + param.marker + param.seriesName + "：" + param.value;
          },
        },
        yAxis: {
          type: "category",
          axisLabel: {
            color: partten.getColor("gray"),
          },
          axisLine: {
            lineStyle: {
              color: "#96A4F4",
            },
            width: 5,
          },
          axisTick: {
            show: false,
          },
          data: this.data.area,
        },
        xAxis: {
          type: "value",
          axisLabel: {
            color: partten.getColor("gray"),
          },
          axisLine: {
            lineStyle: {
              color: "#96A4F4",
              type: "dashed",
            },
            width: 5,
          },
          axisTick: {
            show: false,
          },
          splitLine: {
            lineStyle: {
              color: partten.getColor("gray") + "20",
            },
          },
        },
        series: [],
      };
      for (var i = 0; i < this.data.legend.length; i++) {
        option.series.push({
          name: this.data.legend[i],
          type: "bar",
          stack: "总量",
          //   barWidth: "60%",
          label: {
            show: false,
            position: "insideRight",
          },
          data: this.data.data[i],
        });
      }

      this.chart.setOption(option);
    },
  },
  created() {
    this.id = "pie-chart-" + util.newGUID();
  },
  mounted() {
    this.$nextTick(() => {
      this.$el.style.width = this.width;
      this.$el.style.height = this.height;
      this.chart = echarts.init(this.$el);
      this.initChart();
    });
  },
  updated() {
    this.initChart();
  },
};
</script>

<style lang="less">
.chart {
  width: 100%;
  height: 100%;
  display: inline-block;
}
</style>
