<template>
  <div ref="barChart" style="width: 100%; height: 100%"></div>
</template>
<script>
import * as echarts from "echarts";

export default {
  props: { feptrendIco: Object },
  data() {
    return {
      itemStyle: [
        {
          normal: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: "#00A7FD",
              },
              {
                offset: 1,
                color: "#00A7FD",
              },
            ]),
          },
        },
        {
          normal: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: "#FD8F00",
              },
              {
                offset: 1,
                color: "#FD8F00",
              },
            ]),
          },
        },
      ],
    };
  },

  computed: {
    eptrendIco() {
      return this.feptrendIco;
    },
  },
  watch: {
    eptrendIco() {
      this.initChart();
    },
  },

  mounted() {
    this.$nextTick(() => {
      // console.log(this.feptrendIco)
      this.initChart();
    });
  },

  beforeUnmount() {
    window.removeEventListener("resize", this.chart);
  },

  methods: {
    //次数分布折线图
    initChart() {
      // 比较两个日期数组，取较长的那个
      var chooseData = [];
      var dataAll = [];
      var len = null;
      var aList = this.feptrendIco[0].listDate.length;
      var bList = this.feptrendIco[1].listDate.length;

      aList > bList ? (len = 0) : (len = 1);
      chooseData = this.feptrendIco[len].listDate.map((val) => {
        if (val.indexOf(":") == 2) {
          return val.split(":")[0] + ":" + val.split(":")[1];
        } else {
          return val;
        }
      });

      this.feptrendIco.map((val, ind) => {
        return dataAll.push({
          name: val.name,
          type: "bar",
          barWidth: "20%",
          barGap: "0%",
          itemStyle: this.itemStyle[ind],
          data: val.list,
        });
      });

      var chart = echarts.init(this.$refs.barChart);
      var option;

      option = {
        //    backgroundColor:'#323a5e',
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
        },
        grid: {
          left: "0%",
          right: "0%",
          bottom: "0%",
          top: "22%",
          containLabel: true,
        },
        legend: {
          itemGap: 30,
          left: "30%",
          top: 0,
          textStyle: {
            color: "#fff",
          },
          itemWidth: 12,
          itemHeight: 10,
        },
        xAxis: {
          type: "category",
          // data: ["0:00",
          //   "2:00",
          //   "4:00",
          //   "6:00",
          //   "8:00",
          //   "10:00",
          //   "12:00",
          //   "13:00",
          //   "16:00",
          //   "18:00",
          //   "20:00",
          //   "22:00",
          // ],
          data: chooseData,
          axisLine: {
            lineStyle: {
              color: "rgba(0,0,0,0.1)",
            },
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            // interval: 0,
            // rotate: 40,
            textStyle: {
              color: "#fff",
              fontFamily: "Microsoft YaHei",
            },
          },
        },

        yAxis: {
          type: "value",
          //   max: "800",
          axisLine: {
            show: false,
            lineStyle: {
              color: "white",
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "rgba(255,255,255,0.1)",
            },
          },
          axisLabel: {},
        },
        // dataZoom: [
        //   {
        //     show: true,
        //     height: 12,
        //     xAxisIndex: [0],
        //     bottom: "8%",
        //     start: 10,
        //     end: 90,
        //     handleIcon:
        //       "path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z",
        //     handleSize: "110%",
        //     handleStyle: {
        //       color: "#d3dee5",
        //     },
        //     textStyle: {
        //       color: "#fff",
        //     },
        //     borderColor: "#90979c",
        //   },
        //   {
        //     type: "inside",
        //     show: true,
        //     height: 15,
        //     start: 1,
        //     end: 35,
        //   },
        // ],
        series: dataAll,
      };

      chart.setOption(option);
      window.addEventListener("resize", () => {
        chart.resize();
      });
      this.chart = chart;
    },
  },
};
</script>