<template>
  <chart :options="bar()" class="bar" />
</template>

<script>
import { graphic } from "echarts/lib/export";
export default {
  props: {
    data: {
      type: Object,
      default: () => {
        return null;
      },
    },
    bar_width: {
      type: Number,
      default: 12,
    },
    colors: {
      type: Array,
      default: () => {
        return ["#1034eb", "#e56600"];
      },
    },
    bar_border_radius: {
      type: Array,
      default: () => {
        return [10, 0, 0, 10];
      },
    },
    bar_border_radius_two: {
      type: Array,
      default: () => {
        return [0, 10, 10, 0];
      },
    },
    show_split: {
      type: Boolean,
      default: false,
    },
    x_router: {
      type: Number,
      default: 0,
    },
  },
  data: () => ({}),
  mounted() {},
  methods: {
    bar() {
      let self = this;
      let yAxis = (self.data && self.data.yAxis) || [];
      let series = (self.data && self.data.series) || [{}, {}];

      return {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        legend: {
          data: series.map((item) => item.name),
          left: "right",
          itemWidth: 10,
          itemHeight: 10,
          orient: "horizontal",
        },
        color: self.colors,
        grid: {
          top: 35,
          left: 10,
          right: 30,
          bottom: 0,
          containLabel: true,
        },
        xAxis: [
          {
            axisLabel: {
              formatter: function(value) {
                return Math.abs(value);
              },
            },
            type: "value",
            axisTick: {
              show: false,
            },
          },
        ],
        yAxis: [
          {
            type: "category",
            axisTick: { show: false },
            axisLabel: {
              color: "#333",
              textStyle: {
                fontSize: 12,
              },
            },
            minInterval: 1,
            data: yAxis,
            axisLine: {
              show: false,
            },
          },
        ],
        series: [
          {
            type: "bar",
            stack: "总量",
            label: {
              normal: {
                show: false,
                position: "right",
              },
            },
            barWidth: 10,
            itemStyle: {
              normal: {
                color: function(params) {
                  return new graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    [
                      {
                        offset: 0,
                        color: self.colors[1], // 0% 处的颜色
                      },
                      {
                        offset: 1,
                        color: self.colors[1], // 100% 处的颜色
                      },
                    ],
                    false
                  );
                },
                barBorderRadius: self.bar_border_radius_two,
              },
            },
            name: series[0].name,
            data: series[0].data,
          },
          {
            type: "bar",
            stack: "总量",
            label: {
              normal: {
                show: false,
                position: "left",
                formatter: function(v) {
                  return Math.abs(v.data);
                },
              },
            },
            barWidth: 10,
            itemStyle: {
              normal: {
                color: function(params) {
                  return new graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    [
                      {
                        offset: 0,
                        color: self.colors[0], // 0% 处的颜色
                      },
                      {
                        offset: 1,
                        color: self.colors[0], // 100% 处的颜色
                      },
                    ],
                    false
                  );
                },
                barBorderRadius: self.bar_border_radius,
              },
            },
            name: series[1].name,
            data: series[1].data.map((v) => v * -1),
          },
        ],
      };
    },
  },
};
</script>
<style scoped>
.bar {
  width: 100%;
  height: 100%;
}
</style>
