<template>
  <div>
    <div>
      <div ref="chart" class="item" style="height: 300px" />
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import toolUtils from "@/utils/echartsTooltip";
export default {
  data() {
    return {
      chart: null,
      timer: "",
      dataList: [],
    };
  },
  props: {
    list: {
      type: Array,
      default: [],
    },
  },
  watch: {
    list: {
      handler(val) {
        this.$nextTick(() => {
          const chartDom = this.$refs.chart;
          // 初始化图表实例
          this.chart = echarts.init(chartDom);
          this.initChart(val);
        });
      },
      immediate: true,
      deep: true,
    },
  },

  methods: {
    initChart(data) {
      const x = [];
      data.forEach((p) => {
        if (x.indexOf(p.gd) < 0) {
          x.push(p.gd);
        }
      });
      const sideData = [];
      x.forEach((p) => {
        const val = data.find((k) => k.gd === p);
        sideData.push(val.sl);
      });
      // const datalsit = [220, 182, 191, 234, 290, 330, 310];
      // const sideData = datalsit.map((item) => item + 4.5);
      const option = {
        // backgroundColor: "#041730",
        tooltip: {
          trigger: "axis",
          formatter: "{b} : {c}",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
          extraCssText: "z-index: 1000;"
        },
        title: {
          top: "5%",
          right: "2%",
          text: "单位：栋",
          textStyle: {
            align: "center",
            color: "#fff",
            fontSize: 12,
          },
        },

        grid: {
          right: "10px",
        },
        xAxis: {
          data: [
            "30m及以下",
            "30~50m(含)",
            "50~80m(含)",
            "80~100m(含)",
            "100m以上",
          ],
          //坐标轴
          axisLine: {
            lineStyle: {
              color: "#3eb2e8",
            },
          },
          axisTick: {
            //y轴刻度线
            show: false,
          },
          //坐标值标注
          axisLabel: {
            show: true,
            textStyle: {
              color: "#fff",
              fontSize: 9,
            },
          },
        },
        yAxis: {
          //坐标轴
          axisLine: {
            show: false,
          },
          axisTick: {
            //y轴刻度线
            show: false,
          },
          //坐标值标注
          axisLabel: {
            show: true,
            textStyle: {
              color: "#fff",
            },
          },
          //分格线
          splitLine: {
            lineStyle: {
              color: "#4784e8",
              type: "dashed",
            },
          },
        },
        series: [
          {
            type: "bar",
            barWidth: 20,
            label: {
              show: true,
              color: "#fff",
              position: "top",
            },
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  1,
                  0,
                  0,
                  [
                    {
                      offset: 0,
                      color: "rgba(0,209,255, 1)", // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: "rgba(0, 255,224, .87)", // 100% 处的颜色
                    },
                  ],
                  false
                ),
              },
            },
            barGap: 0,
            data: sideData,
          },
        ],
      };

      this.chart.setOption(option);
      //自适应图表
      window.onresize = this.chart.resize;

      // 自动轮播
      console.log(data);
      toolUtils.autoHover(this.chart, option, data.length, 5000);
    },
    openBasicModal() {
      this.showModal("basicInfoModal");
    },
  },
  // mounted() {
  //   const chartDom = this.$refs.chart;
  //   //   // 初始化图表实例
  //   this.chart = echarts.init(chartDom);
  //   this.initChart(this.dataList);
  // },
};
</script>

<style scoped></style>
