<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",
    },
    // 传入数据
    bardata: {
      type: Object,
      default: () => {
        return {
          area: ["风场1", "风场2", "风场3", "风场4", "风场5", "风场6", "风场7", "风场8", "风场9"],
          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],
            [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],
            [1320, 1302, 901, 634, 1390, 1330, 1320, 1000, 500],
            [320, 302, 301, 334, 390, 330, 320, 100, 50],
          ],
        };
      },
    },
    lineData: {
      type: Array,
      default: () => [200, 350, 400, 500, 600, 700, 800, 900, 1200],
    },
    // 单位
    units: {
      type: Array,
      default: () => ["(万KWh)", "(风速)"],
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      color: ["#323E6F", "#1DA0D7", "#02BB4C", "#DB5520", "#EDB32F", "#EDEB2F"],
    };
  },
  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: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            data: this.bardata.area,
          },
        ],
        xAxis: [
          {
            type: "value",
            axisLabel: {
              color: partten.getColor("gray"),
            },
            axisLine: {
              type: "dashed",
              lineStyle: {
                color: partten.getColor("gray"),
              },
              width: 5,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              lineStyle: {
                type: "dashed",
                dashOffset: 10,
                color: partten.getColor("gray") + 80,
              },
            },
          },
          {
            type: "value",
            name: "",
            axisLabel: {
              show: false,
              // formatter: "{value}",
              // color: partten.getColor("gray"),
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
          },
        ],
        series: [],
      };
      for (var i = 0; i < this.bardata.legend.length; i++) {
        option.series.push({
          name: this.bardata.legend[i],
          type: "bar",
          stack: "总量",
          //   barWidth: "60%",
          label: {
            show: false,
            position: "insideRight",
          },
          data: this.bardata.data[i],
        });
      }

      if (this.lineData.length > 0)
        option.series.push({
          name: "损失电量",
          type: "line",
          data: this.lineData,
          smooth: false, //平滑展示
          xAxisIndex: 1,
          lineStyle: {
            color: partten.getColor("green"),
          },
          itemStyle: {
            color: partten.getColor("green"),
          },
        });

      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>
