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

<script>
import { graphic } from "echarts/lib/export";
export default {
  props: {
    data: {
      type: Object,
      default: () => {
        return {
          title:[],
          dataOne:[],
          dataTwo:[]
        };
      }
    },
    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;
      return {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow" // 默认为直线，可选为：'line' | 'shadow'
          },
          formatter: function(params, ticket, callback) {
            // console.log(params);
            var res = params[0].name;
            for (var i = 0, l = params.length; i < l; i++) {
              res +=
                "<br/>" +
                params[i].seriesName +
                " : " +
                Math.abs(params[i].value);
            }
            setTimeout(function() {
              // 仅为了模拟异步回调
              callback(ticket, res);
            }, 500);
            return "loading...";
          }
        },
        legend: {
          data: ["设置警告值", "实际访问值"],
          left: "right",
          itemWidth: 10,
          itemHeight: 10,
          orient: "horizontal",
          backgroundColor: "#f4f7fc"
        },
        color:self.colors,
        grid: {
          left: "1%",
          right: "4%",
          bottom: "3%",
          containLabel: true
        },
        xAxis: [
          {
            axisLabel: {
              formatter: function(value) {
                return Math.abs(value); //显示的数值都取绝对值
              }
            },
            type: "value",
            axisTick: {
              show: false
            },
            // splitLine: {
            //   show: false
            // }
            // splitArea: {
            //   show: true,
            //   areaStyle: {
            //     color: ["#fff", "#f8f9fd"],
            //     opacity: 0.5
            //   },
            //   interval: 0
            // }
          }
        ],
        yAxis: [
          {
            type: "category",
            axisTick: { show: false },
            axisLabel: {
              color: "#333",
              textStyle: {
                fontSize: 12
              }
            },
            minInterval: 1,
            data: self.data.title,
            // splitLine: {
            //   show: false
            // },
            axisLine:{
              show:false
            }
          }
        ],
        series: [
          {
            name: "设置警告值",
            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
              }
            },
            data: self.data.dataOne
          },
          {
            name: "实际访问值",
            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
              }
            },
            data: self.data.dataTwo
          }
        ]
      };
    }
  }
};
</script>
<style scoped>
.bar {
  width: 100%;
  height: 100%;
}
</style>