<template >
  <div ref="chart" style="height: 200px"></div>
</template>

<script>
import * as echarts from "echarts";
import toolUtils from "@/utils/echartsTooltip";
export default {
  name: "ChartComponent",
  props: {
    items: Array,
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    // 初始化表格
    this.init();
  },
  watch: {
    items: {
      handler() {
        this.reload();
      },
      deep: true,
    },
  },
  methods: {
    init() {
      this.chart = echarts.init(this.$refs.chart);
      const option = {
        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: [
            "一月",
            "二月",
            "三月",
            "四月",
            "五月",
            "六月",
            "七月",
            "八月",
            "九月",
            "十月",
            "十一月",
            "十二月",
          ],
        },
        yAxis: {
          type: "value",
          //坐标轴
          axisLine: {
            show: false,
          },
          minInterval: 1,
          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",
              },
            },
          },
        ],
      };
      this.chart.setOption(option);
      this.chart;
      // 自动轮播
      toolUtils.autoHover(this.chart, option, 12, 5000);
    },
    reload() {
      // 重载数据
      if (this.chart) {
        const max = this.items.map((item) => parseInt(item.value || 0));
        console.log(this.items);
        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>
