<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: "13.889vh",
    },
    // 传入数据
    list: {
      type: Array,
      default: () => [
        {
          text: "1",
          value: 1,
        },
        {
          text: "2",
          value: 2,
        },
        {
          text: "3",
          value: 1,
        },
        {
          text: "4",
          value: 3,
        },
        {
          text: "5",
          value: 3,
        },
        {
          text: "6",
          value: 3,
        },
        {
          text: "7",
          value: 3,
        },
        {
          text: "8",
          value: 3,
        },
        {
          text: "9",
          value: 3,
        },
        {
          text: "10",
          value: 3,
        },
        {
          text: "11",
          value: 3,
        },
        {
          text: "12",
          value: 3,
        },
        {
          text: "13",
          value: 3,
        },
        {
          text: "14",
          value: 3,
        },
        {
          text: "15",
          value: 3,
        },
        {
          text: "16",
          value: 3,
        },
        {
          text: "17",
          value: 3,
        },
        {
          text: "18",
          value: 3,
        },
        {
          text: "19",
          value: 3,
        },
        {
          text: "20",
          value: 3,
        },
        {
          text: "21",
          value: 3,
        },
        {
          text: "22",
          value: 3,
        },
        {
          text: "23",
          value: 3,
        },
        {
          text: "24",
          value: 3,
        },
        {
          text: "25",
          value: 3,
        },
        {
          text: "26",
          value: 3,
        },
        {
          text: "27",
          value: 3,
        },
        {
          text: "28",
          value: 3,
        },
        {
          text: "29",
          value: 3,
        },
        {
          text: "30",
          value: 3,
        },
        {
          text: "31",
          value: 3,
        },
      ],
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      color: ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b"],
    };
  },
  computed: {
    colorValue() {
      return partten.getColor(this.color);
    },
    datas() {
      return this.list.map((t) => {
        return t.value;
      });
    },
    xdata() {
      return this.list.map((t) => {
        return t.text;
      });
    },
  },
  methods: {
    initChart() {
      let option = {
        color: this.color,
        tooltip: {
          trigger: "item",
          backgroundColor: "rgba(0,0,0,0.3)",
          textStyle: {
            color: "#fff",
            fontSize: util.vh(16),
          },
          formatter: function(param) {
            console.log(param);
            let marker = '<span style="display:inline-block;margin-right:0.370vh;border-radius:0.926vh;width:0.926vh;height:0.926vh;background-color:' + partten.getColor("green") + ';"></span>';
            return param.name + "<br >" + marker + param.seriesName + "：" + param.value;
          },
        },
        grid: {
          top: 28,
          left: 16,
          bottom: 16,
          right: 16,
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: this.xdata,
            axisLabel: {
              fontSize: util.vh(14),
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "(万KWh)",
            //分格线
            splitLine: {
              lineStyle: {
                color: "#999",
                type: "dashed",
              },
            },
            axisLabel: {
              fontSize: util.vh(14),
            },
          },
        ],
        series: {
          name: "损失电量",
          type: "bar",
          data: this.datas,
          itemStyle: {
            color: partten.getColor("gray"),
          },
          emphasis: {
            itemStyle: {
              color: partten.getColor("green"),
            },
          },
        },
      };

      this.chart.setOption(option);
    },
  },
  created() {
    this.id = "pie-chart-" + util.newGUID();
  },
  mounted() {
    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>
