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

<script>
import { graphic } from "echarts/lib/export";
export default {
  props: {
    data: {
      type: Object,
      default: () => {
        return {
          legendData: [],
          seriesData: [],
        };
      },
    },
    bar_width: {
      type: Number,
      default: 12,
    },
    colors: {
      type: Array,
      default: () => {
        return ["#274fee", "#274fee"];
      },
    },
    bar_border_radius: {
      type: Array,
      default: () => {
        return [10, 10, 10, 10];
      },
    },
    show_split: {
      type: Boolean,
      default: false,
    },
    x_router: {
      type: Number,
      default: 0,
    },
    xAxis_text_length: {
      type: Number,
      default: 0,
    },
  },
  data: () => ({}),
  mounted() {},
  methods: {
    bar() {
      let self = this;

      return {
        tooltip: {
          trigger: "axis",
          formatter: function(params) {
            var htmlStr = "";
            for (var i = 0; i < params.length; i++) {
              htmlStr += params[i].name + " : ";
              htmlStr += params[i].value;
            }
            return htmlStr;
          },
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 25,
          top: 25,
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: self.data.legendData,
            axisLabel: {
              margin: 20,
              color: "#0d1847",
              textStyle: {
                fontSize: 12,
              },
              interval: 0,
              rotate: self.x_router,
              formatter: function(value) {
                let maxLen = self.xAxis_text_length;

                if (maxLen > 1) {
                  let valueTxt = "";
                  if (value.length > maxLen) {
                    valueTxt = value.substring(0, maxLen - 1) + "...";
                  } else {
                    valueTxt = value;
                  }
                  return valueTxt;
                } else {
                  return value;
                }
              },
            },
            axisLine: {
              lineStyle: {
                color: "#f2f2f2",
              },
            },
            axisTick: {
              show: false,
            },
            splitArea: {
              show: true,
              areaStyle: {
                color: ["#fff", "#f8f9fd"],
                opacity: 0.5,
              },
              interval: 0,
            },
            axisPointer: {
              type: "shadow",
            },
          },
        ],
        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",
            data: self.data.seriesData,
            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[1], // 100% 处的颜色
                      },
                    ],
                    false
                  );
                },
                barBorderRadius: self.bar_border_radius,
              },
            },
          },
        ],
      };
    },
  },
};
</script>
<style scoped>
.bar {
  width: 100%;
  height: 100%;
}
</style>
