<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]],
    },
    areaData: {
      type: Array,
      default: () => [
        {
          name: "1",
          start: 0,
          end: 100,
          state: "green",
        },
        {
          name: "1",
          start: 100,
          end: 200,
          state: "red",
        },
        {
          name: "1",
          start: 200,
          end: 300,
          state: "yellow",
        },
        {
          name: "",
          start: 300,
          end: 400,
          state: "green",
        },
      ],
    },
    // 单位
    units: {
      type: Array,
      default: () => ["健康趋势", "风机健康状态数量"],
    },
    // 显示 legend
    showLegend: {
      type: Boolean,
      default: true,
    },
    // 颜色
    color: {
      type: Array,
      default: () => ["#323E6F", "#1DA0D7", "#02BB4C", "#DB5520", "#EDB32F", "#EDEB2F"],
    },
  },
  data() {
    return {
      id: "",
      chart: null,
    };
  },
  computed: {
    legend() {
      let data = this.bardata.legend;
      data.push(this.units[0]);
      return data;
    },
    areaChartData() {
      let data = [];
      for (var i = 0; i < this.areaData.length; i++) {
        let item = this.areaData[i];
        var color = item.color ? item.color : partten.getColor(item.state);
        data.push({
          name: item.name,
          value: [item.start, item.end, item.end - item.start],
          itemStyle: {
            normal: {
              color: color,
            },
          },
          exData: item,
        });
      }
      return data;
    },
    areaMax() {
      let max = 0;
      this.areaData.forEach((value) => {
        if (max < value.end) max = value.end;
      });
      return max;
    },
  },
  methods: {
    renderItem(params, api) {
      var start = api.coord([api.value(0)]);
      var end = api.coord([api.value(1)]);
      var height = api.size([0, 1])[1];

      var rectShape = echarts.graphic.clipRectByRect(
        {
          x: start[0],
          y: start[1] - height / 2,
          width: end[0] - start[0],
          height: height,
        },
        {
          x: params.coordSys.x,
          y: params.coordSys.y,
          width: params.coordSys.width,
          height: params.coordSys.height,
        }
      );

      return (
        rectShape && {
          type: "rect",
          transition: ["shape"],
          shape: rectShape,
          style: api.style(),
        }
      );
    },
    initChart() {
      let that = this;
      let chart = echarts.init(this.$el);

      let option = {
        color: this.color,
        grid: {
          left: 16,
          right: 16,
          bottom: 0,
          top: 32,
          containLabel: true,
        },
        tooltip: {
          show: true,
          trigger: "axis",
          axisPointer: {
            type: "line",
          },
          backgroundColor: "rgba(0,0,0,0.4)",
          borderColor: partten.getColor("gray"),
          textStyle: {
            color: "#fff",
            fontSize: 14,
          },
        },
        legend: {
          show: this.showLegend,
          data: this.bardata.legend,
          right: 120,
          icon: "ract",
          itemWidth: 8,
          itemHeight: 8,
          inactiveColor: partten.getColor("gray"),
          textStyle: {
            color: partten.getColor("grayl"),
            fontSize: 12,
          },
        },
        xAxis: [
          {
            type: "category",
            axisLabel: {
              color: partten.getColor("gray"),
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            data: this.bardata.area,
          },
          {
            show: false,
            min: 0,
            max: this.areaMax,
            axisLabel: {
              show: false,
              formatter: function(val) {
                return Math.max(0, val - 0) + " ms";
              },
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: this.units[0],
            axisLabel: {
              formatter: "{value} ",
              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: this.units[1],
            axisLabel: {
              formatter: "{value} ",
              color: partten.getColor("gray"),
              align: "left",
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
          },
          {
            data: [(this.areaData && this.areaData[0] && this.areaData[0].name) || ""],
            axisLabel: { show: false },
            splitLine: { show: false },
          },
        ],
        series: [],
      };

      // line data
      if (this.lineData.length) {
        this.lineData.forEach((ele, index) => {
          option.series.push({
            name: this.units[index] || this.units[0] || "",
            type: "line",
            data: ele,
            smooth: true, //平滑展示
            yAxisIndex: 0,
            lineStyle: {
              color: this.color[index],
            },
            itemStyle: {
              color: this.color[index],
            },
          });
        });
      }

      // bar data
      for (var i = 0; i < this.bardata.legend.length; i++) {
        option.series.push({
          name: this.bardata.legend[i],
          type: "bar",
          stack: "总量",
          yAxisIndex: 1,
          barWidth: "10%",
          label: {
            show: false,
            position: "insideRight",
          },
          data: this.bardata.data[i],
        });
      }

      // 区域
      if (this.areaData && this.areaData.length > 0) {
        option.series.push({
          type: "custom",
          renderItem: this.renderItem,
          yAxisIndex: 2,
          xAxisIndex: 1,
          itemStyle: {
            opacity: 0.2,
          },
          tooltip: {
            show: false,
            formatter: function(params) {
              return params.marker + params.name + ": " + params.value[2] + "s";
            },
          },
          encode: {
            x: [1, 2],
            y: 0,
          },
          data: this.areaChartData,
        });
      }

      chart.setOption(option);

      chart.on("click", function(e, p) {
        if (e.seriesType == "custom") {
          that.$emit("areaClick", { data: e.data.exData });
        }
      });
    },
  },
  emits: {
    areaClick: null,
  },
  created() {
    this.id = "pie-chart-" + util.newGUID();
  },
  mounted() {
    this.$nextTick(() => {
      this.$el.style.width = this.width;
      this.$el.style.height = this.height;
      this.initChart();
    });
  },
  updated() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
};
</script>

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