<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";
import dayjs from "dayjs";
export default {
  name: "multiple-bar-chart",
  componentName: "multiple-bar-chart",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "13.889vh",
    },
    pillarName: {
      type: String,
      default: "",
    },
    top: {
      type: Number,
      default: 20,
    },
    interval: {
      type: Number,
      default: 0,
    },
    padding: {
      type: Array,
      default: () => [10, 10],
    },
    // 传入数据
    list: {
      type: Array,
      default: () => [],
    },
    // 单位
    units: {
      type: String,
      default: "",
    },
    // 显示 legend
    showLegend: {
      type: Boolean,
      default: true,
    },
    // X轴是否为时间
    xdate: {
      type: Boolean,
      default: true,
    },
    // 颜色#
    color: {
      type: Array,
      default: () => [
        "#005bd9",
        "#019f2e",
        "#db6200",
        "#a10f0f",
        "#850894",
        "#9fa0a2",
      ],
    },
    showAnimation: {
      type: Boolean,
      default: true,
    },
    colorIndex: {
      type: Boolean,
      default: false,
    },
    // 柱子最大宽度
    barMaxWidth: {
      type: Number || String,
      default: 0,
    },
    // 柱子间距
    barGap: {
      type: Number || String,
      default: 0,
    },
    showkey: {
      type: String,
      default: "",
    },
    showName: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      firstAnimation: true,
    };
  },
  computed: {
    legend() {
      return this.list.map((t) => {
        return t.name;
      });
    },
    xdata() {
      if (this.list[0].value.length) {
        return this.list[0].value.map((item) => item.dateTime);
      } else {
        return [];
      }
    },

    series() {
      let result = [];
      if (this.showkey == "electric") {
        this.list.forEach((value, index) => {
          let seriesItem = {
            name: value.name,
            type: "bar",
            barWidth: "12",
            animation: this.firstAnimation && this.showAnimation,
            itemStyle: {
              borderColor: this.color[index],
              borderWidth: 1,
            },
            data: value.value,
          };
          result.push(seriesItem);
        });
      } else {
        result = [
          {
            name: this.list[0].name,
            type: "bar",
            barWidth: "12",
            itemStyle: {
              borderColor: this.color[0],
              borderWidth: 1,
            },
            data: this.list[0].value,
          },
        ];
      }
      return result;
    },
  },
  methods: {
    initChart() {
      let chart = echarts.init(this.$el);
      let option = {
        color: this.color,
        zoom: 12,
        tooltip: {
          trigger: "axis",
          backgroundColor: "rgba(0,70,199,0.35)",
          borderWidth: 1,
          padding: [10, 10, 3, 10],
          borderColor: "#074EAD",
          textStyle: {
            color: "#fff",
            fontSize: 16,
            fontFamily: "楷体",
            fontWeight: "normal",
          },
          axisPointer: {
            type: "shadow",
            shadowStyle: {
              color: "rgba(105,105,105, .05)",
              width: "1",
            },
          },
        },
        legend: {
          show: this.showLegend,
          data: this.legend,
          padding: this.padding,
          right: 56,
          icon: "ract",
          itemWidth: 8,
          itemHeight: 8,
          inactiveColor: partten.getColor("gray"),
          textStyle: {
            fontSize: 12,
            color: partten.getColor("grayl"),
          },
        },
        grid: {
          left:30,
          right: 40,
          top: 30,
          bottom: 30,
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: this.xdata,
            axisLabel: {
              interval: this.interval,
              fontSize: 12,
              textStyle: {
                color: "rgb(116,124,128)",
              },
            },
          },
        ],
        yAxis: {
          type: "value",
          name: this.units,
          //分格线
          splitLine: {
            show: false,
          },
        },
        series: this.series,
      };
      chart.setOption(option);
    },
  },
  created() {
    this.$nextTick(() => {
      this.id = "pie-chart-" + util.newGUID();
    });
  },
  mounted() {
    this.$nextTick(() => {
      //   this.$el.style.width = this.width;
      //   this.$el.style.height = this.height;
      this.initChart();
      this.firstAnimation = false;
    });
  },

  watch: {
    "$store.state.themeName"() {
      this.initChart();
    },
    list: {
      handler() {
        this.initChart();
      },
    },
  },
};
</script>

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