<template>
  <div>
    <div
      ref="chart"
      style="height: 200px"
      v-show="items && items.length > 0"
    ></div>
    <div
      style="margin-top: 0px; margin-left: 140px"
      v-show="items && items.length == 0"
    >
      <img
        src="../../../../assets/images/null2.png"
        alt=""
        style="width: 150px; height: 150px"
      />
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  name: "ChartComponent",
  props: {
    items: Array,
    label: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    // 初始化表格
    this.init();
  },
  watch: {
    items: {
      handler() {
        this.reload();
      },
      immediate: true,
      deep: true,
    },
    label: {
      handler() {
        this.chart.setOption({
          xAxis: {
            data: this.label,
          },
        });
      },
      deep: true,
    },
  },
  methods: {
    init() {
      this.chart = echarts.init(this.$refs.chart);
      this.chart.setOption({
        tooltip: {
          trigger: "axis",
          formatter: "{b} : {c}",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
          confine: true,
          extraCssText: "z-index: 1000;",
          backgroundColor: "#02809a",
          borderColor: "rgba(0,213,255, .6)",
          textStyle: {
            color: "#fff",
          },
        },
        xAxis: {
          type: "category",
          axisLine: {
            show: false,
          },
          axisTick: {
            //y轴刻度线
            show: false,
          },
          axisLabel: {
            color: "#fff",
            fontSize: 8,
          },
          data: this.label,
        },
        yAxis: {
          type: "value",
          //坐标轴
          axisLine: {
            show: false,
          },
          axisLabel: {
            color: "#fff",
          },
          axisTick: {
            //y轴刻度线
            show: false,
          },
          //分格线
          splitLine: {
            lineStyle: {
              color: "#4784e8",
              type: "dashed",
            },
          },
        },
        grid: {
          right: "8px",
          top: "20px",
        },
        series: [
          {
            data: this.items,
            type: "bar",
            barWidth: 10,
            barGap: 20,
            showBackground: true,
            backgroundStyle: {
              color: "rgba(180, 180, 180, 0.2)",
            },
            label: {
              show: true,
              color: "#fff",
              position: "top",
            },
            itemStyle: {
              normal: {
                color: "#0085FF",
              },
              emphasis: {
                color: "#00E0FF",
                borderWidth: 0,
              },
            },
          },
        ],
      });
      // if (this.chart && this.chart.clearLoop) {
      //   this.chart.clearLoop();
      // }
      // this.chart = loopShowTooltip(this.chart);

      this.chart;
    },
    reload() {
      // 重载数据
      if (this.chart) {
        const max = this.items
          .map((item) => parseInt(item.value || 0))
          .sort((a, b) => b - a);
        this.chart.setOption({
          yAxis: {
            min: 0,
            max: function (value) {
              return max[0] === 0 ? 8 : Math.ceil(value.max);
            },
          },
          series: [
            {
              data: this.items,
            },
          ],
        });
      }
    },
  },
};
</script>

<style scoped lang="less"></style>
