<template>
  <div class="items">
    <div ref="chart" class="item" style="width: 100%; height: 180px" />
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  props: {
    dataList: {
      type: Array,
      require: true,
    },
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    dataList: {
      handler(val) {
        // console.log("newval", val);
        this.$nextTick(() => {
          // 获取dom容器
          const chartDom = this.$refs.chart
          // 初始化图表实例
          this.chart = echarts.init(chartDom)
          this.initChart(val)
        })
        // console.log("data", this.data, this.maxNum);
      },
      immediate: true,
      deep: true,
    },
  },
  mounted() {
    // 获取dom容器
    const chartDom = this.$refs.chart
    // 初始化图表实例
    this.chart = echarts.init(chartDom)
    this.initChart(this.processData)
    // console.log("da======ta", this.processData, this.maxNum);
  },
  methods: {
    initChart(data) {
      const option = {
        // title: {
        //   text: "第一个 ECharts 实例",
        // },
        // 鼠标移入进行操作
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
          formatter: function (params) {
            return `${params[0].name}: <br/>${params[0].value}`
          },
        },
        // legend: {
        //   data: ["销量"],
        // },
        xAxis: {
          // x轴分割线
          //   splitLine: {
          //     show: false,
          //   },
          // x轴下面一小段分割线去除
          axisTick: {
            show: false,
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: '#47576A', // 这里用参数代替了
            },
            interval: 0,
            rotate: 1, // 文字倾斜角度
            formatter: function (value) {
              if (value.length > 5) {
                value = value.substring(0, 4) + '..'
              }
              return value
            },
          },
        },
        grid: {
          top: '5',
          left: '1%',
          right: '0%',
          bottom: '3%',
          containLabel: true,
        },
        yAxis: {
          // 隐藏y轴数据
          //   show: false,
          splitLine: {
            show: false,
          },
          axisLine: {
            // y轴
            show: false,
          },
          // y轴字体颜色，大小
          axisLabel: {
            color: '#303133',
            fontSize: 12,
            margin: 8,
            formatter: function (params) {
              var val = ''
              if (params.length > 4) {
                val = params.substr(0, 4) + '...'
                return val
              } else {
                return params
              }
            },
          },
          axisTick: {
            show: false,
          },
          inverse: true,
          type: 'category',
          data: data.map((item) => {
            return item.name
          }),
        },
        series: [
          {
            // name: "销量",
            type: 'bar',
            // 柱子宽度
            barWidth: 8,
            data: data.map((item) => {
              return item.data
            }),
            itemStyle: {
              barBorderRadius: [5, 5, 5, 5], // 圆角（左上、右上、右下、左下）

              color: '#419fff',
            },
          },
        ],
      }
      this.chart.setOption(option)
    },
  },
}
</script>

<style lang="less" scoped>
.items {
  width: 100%;
  //   width: 350px;
  height: 180px;
  //   overflow: auto;
  // overflow-x: auto;
  //   background: url("../../../../assets/statisitcs/line.png") no-repeat;
  //   background-size: 100%;
  //   background-position-x: 97px;
}
.item {
  margin: 10px 0 15px;

  display: flex;
  align-items: center;
  font-size: 13px;
}

.anim {
  transition: all 0.8s;
}

.progress {
  margin: 0 10px;
  height: 12px;
  background: #4086ff;
  border-radius: 5px;
}
.title {
  width: 23%;
  color: #4e5969;
  text-align: right;
}
::-webkit-scrollbar-track-piece {
  background: #5598e4;
}

::-webkit-scrollbar {
  width: 6px;
}
</style>
