<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 ["#515fe7", "#e56600"];
      },
    },
    bar_border_radius: {
      type: Array,
      default: () => {
        return [10, 10, 10, 10];
      },
    },
    show_split: {
      type: Boolean,
      default: false,
    },
    x_router: {
      type: Number,
      default: 0,
    },
  },
  methods: {
    bar() {
      let _self = this;
      let xAxis = (_self.data && _self.data.xAxis) || [];
      let series = (_self.data && _self.data.series) || [{}, {}];

      return {
        grid: {
          left: 10,
          right: 10,
          bottom: 25,
          top: 25,
          containLabel: true,
        },
        legend: {
          left: "right",
          itemWidth: 10,
          itemHeight: 10,
          orient: "horizontal",
          backgroundColor: "#f4f7fc",
          data: series.map((item) => item.name),
        },
        tooltip: {
          trigger: "axis",
        },
        xAxis: {
          type: "category",
          axisLabel: {
            margin: 20,
            color: "#0d1847",
            textStyle: {
              fontSize: 12,
            },
            interval: 0,
            rotate: _self.x_router,
            /* formatter: function(value) {
              let valueTxt = "";
              if (value.length > 6) {
                valueTxt = value.substring(0, 5) + "...";
              } else {
                valueTxt = value;
              }
              return valueTxt;
            }, */
          },
          axisLine: {
            lineStyle: {
              color: "#f2f2f2",
            },
          },
          axisTick: {
            show: false,
          },
          splitArea: {
            show: true,
            areaStyle: {
              color: ["#fff", "#f8f9fd"],
              opacity: 0.5,
            },
            interval: 0,
          },
          axisPointer: {
            type: "shadow",
          },
          data: xAxis,
        },
        yAxis: [
          {
            axisLabel: {
              color: "#a9aec0",
              textStyle: {
                fontSize: 12,
              },
            },
            minInterval: 1,
            axisLine: {
              lineStyle: {
                color: "rgba(0, 0, 0, 0)",
              },
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              lineStyle: {
                color: "#f2f2f2",
                type: "solid",
              },
            },
          },
        ],
        series: [
          {
            type: "bar",
            type: "bar",
            barWidth: _self.bar_width,
            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[0].name,
            data: series[0].data,
          },
          {
            type: "bar",
            type: "bar",
            barWidth: _self.bar_width,
            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,
              },
            },
            name: series[1].name,
            data: series[1].data,
          },
        ],
      };
    },
  },
};
</script>
<style scoped>
.bar {
  width: 100%;
  height: 100%;
}
</style>
