<!-- 横向柱状图 -->
<template>
  <div>
    <boxTop :title="title" />
    <div class="histogramBox">
      <div class="chartsdom" :id="id"></div>
    </div>
  </div>
</template>

<script>
import boxTop from '@/components/boxTop/index.vue'
export default {
  data() {
    return {
      option: null,
      information: {},
    }
  },
  components: {
    boxTop,
  },
  props: {
    id: {
      type: String,
      default: ``,
    },
    barWidth: {
      type: Number,
    },
    area: {
      type: Array,
    },
    dataArray: {
      type: Object,
    },
    title: {
      type: String,
      default: ``,
    },
    unit: {
      type: String,
    },
  },
  mounted() {
    this.getEchart()
  },
  watch: {
    area() {
      this.getEchart()
    },
  },
  methods: {
    getEchart() {
      let myChart = echarts.init(document.getElementById(this.id))
      // 定义颜色数组
      const colorList = `#111`
      const colorListA = [`#40F8FF`, `#FFf`]
      const data = {
        label: this.area,
        value: this.dataArray.Done,
        value2: this.dataArray?.Percentage || [],
      }
      this.option = {
        backgroundColor: `rgb(0,0,0,0.3)`,
        tooltip: {
          trigger: `axis`,
          backgroundColor: `rgba(0, 0, 0, 0.8)`,
          textStyle: {
            color: `#fff`,
          },
          axisPointer: {
            lineStyle: {
              color: `transparent`,
            },
          },
        },
        grid: {
          containLabel: true,
          bottom: 20,
          left: 30,
          top: 20,
          right: 30,
        },
        xAxis: {
          type: `value`,
          axisLabel: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          splitLine: {
            show: false,
          },
        },
        yAxis: [
          {
            type: `category`,
            data: data.label,
            inverse: true,
            axisLabel: {
              margin: 10, // 刻度标签与轴线之间的距离
              formatter: function (value) {
                let k = data.label.indexOf(value)
                //  let index = k < 9 ? "0" + (k + 1) : k + 1;
                let index = k < 9 ? k + 1 : k + 1
                return `{a|${value}}`
              },
              rich: {
                b: {
                  // borderColor: "#fff",
                  // borderWidth: 1,
                  padding: [3, 1, 0, 1],
                  color: `#fff`,
                  fontSize: `80%`,
                },
                a: {
                  fontSize: `80%`,
                  color: `#D0DEEE`,
                  padding: [4, 0, 0, 8],
                },
              },
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
          },
          {
            type: `category`,
            data: data.label,
            inverse: true,
            axisLabel: {
              margin: 15,
              formatter: function (value) {
                let k = data.label.indexOf(value)
                let index = k
                return `{a|${data.value[index]}}`
              },
              rich: {
                a: {
                  fontSize: `90%`,
                  color: `#fff`,
                  padding: [4, 0, 0, 0],
                  fontFamily: `DOUYU`,
                },
              },
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
          },
          {
            type: `category`,
            data: data.label,
            inverse: true,
            axisLabel: {
              margin: 2,
              formatter: function (value) {
                if (data.value2.length > 0) {
                  let k = data.label.indexOf(value)
                  let index = k
                  return `{a|${data.value2[index]} %}`
                }
              },
              rich: {
                a: {
                  fontSize: `90%`,
                  color: `#fff`,
                  padding: [4, 0, 0, 60],
                  fontFamily: `DOUYU`,
                },
              },
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
          },
        ],
        series: [
          {
            data: data.value,
            type: `bar`,
            barWidth: 10, // 柱子宽度
            MaxSize: 0,
            showBackground: true,
            barBorderRadius: [30, 0, 0, 30],
            backgroundStyle: {
              color: `rgba(9, 68, 131, .2)`,
            },
            label: {
              show: false,
              offset: [60, -17],
              color: `#fff`,
              fontWeight: 500,
              position: `left`,
              align: `left`,
              fontSize: `80%`,
              formatter: function (params) {
                console.log(params)
                return params.data.name
              },
            },
            itemStyle: {
              // barBorderRadius: [3, 0, 0, 3],
              color: (params) => {
                return {
                  type: `linear`,
                  x: 0,
                  y: 0,
                  x2: 1,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: colorListA[params.dataIndex <= 2 ? 0 : `1`],
                    },
                    {
                      offset: 1,
                      color: colorList,
                    },
                  ],
                }
              },
            },
          },
          {
            name: `外圆`,
            type: `scatter`,
            emphasis: {
              scale: false,
            },
            tooltip: {
              show: false,
            },
            symbol: `rect`,
            symbolSize: [3, 8], // 进度条白点
            itemStyle: {
              barBorderRadius: [30, 0, 0, 30],
              color: `#FFF`,
              shadowColor: `rgba(255, 255, 255, 0.5)`,
              shadowBlur: 5,
              borderWidth: 1,
              opacity: 1,
            },
            z: 2,
            data: data.value,
            animationDelay: 500,
          },
        ],
      }
      myChart.setOption(this.option, true)
    },
  },
  beforeDestroy() {},
}
</script>

<style lang="less" scoped>
.histogramBox {
  width: 401px;
  height: 290px;
  .chartsdom {
    width: 100%;
    height: 100%;
  }
}
</style>
