<template>
  <div
    ref="main"
    :class="className"
    :style="{ height: height, width: width }"
  ></div>
</template>
<script>
import echarts from "echarts";
//require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  //自适应窗口大小改变图表大小
  mixins: [resize],

  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    yData: {
      type: Array,
      default: () => ["未处理数", "审核未通过", "超时完成", "已完成"],
    },
    yData2: {
      type: Array,
      default: () => [60, 180, 30, 260],
    },
    seriesData: {
      type: Array,
      default: () => [12, 36, 2, 50],
    },
    myColor: {
      type: Array,
      default: () => [12, 36, 2, 50],
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.main);

      var myColor = ["rgb(248, 72, 3)", "#FD7700", "#0BC3FF", "#01E416"];

      this.chart.setOption({
        grid: {
          top: "0%",
          left: "25%",
          bottom: "0%",
          // containLabel: true
        },
        // 不显示x轴的相关信息
        xAxis: {
          show: false,
        },
        yAxis: [
          {
            type: "category",
            // inverse: true,
            data: this.yData,
            // 不显示y轴的线
            axisLine: {
              show: false,
            },
            // 不显示刻度
            axisTick: {
              show: false,
            },
            // 把刻度标签里面的文字颜色设置为白色
            axisLabel: {
              color: "#fff",
            },
          },
          {
            data: this.yData2,
            inverse: false,
            // 不显示y轴的线
            axisLine: {
              show: false,
            },
            // 不显示刻度
            axisTick: {
              show: false,
            },
            // 把刻度标签里面的文字颜色设置为白色
            axisLabel: {
              color: "#fff",
            },
          },
        ],
        series: [
          {
            name: "条",
            type: "bar",
            data: this.seriesData,
            yAxisIndex: 0,
            // 修改第一组柱子的圆角
            itemStyle: {
              // 此时的color 可以修改柱子的颜色
              color: function (params) {
                // params 传进来的是柱子对象
                // console.log(params);
                // dataIndex 是当前柱子的索引号
                return myColor[params.dataIndex];
              },
            },
            // 柱子之间的距离
            barCategoryGap: 50,
            //柱子的宽度
            barWidth: 10,
            // 显示柱子内的文字
            label: {
              show: true,
              position: "right",
              // {c} 会自动的解析为 数据  data里面的数据
              formatter: "{c}%",
            },
          },
          {
            name: "框",
            type: "bar",
            barCategoryGap: 50,
            barWidth: 10,
            yAxisIndex: 1,
            data: [100, 100, 100, 100],
            itemStyle: {
              color: "none",
              borderColor: "#00c1de",
              borderWidth: 1,
            },
          },
        ],
      });
    },
  },
};
</script>