<template>
  <div class="ech">
    <div id="linezss" style="width: 100%; height: 260px"></div>
  </div>
</template>

<script setup>
import * as echarts from "echarts";
import { ref, onMounted, defineProps, watch } from "vue";
const props = defineProps({
  data: { type: Array }
});
watch(
  () => props.data,
  () => {
    chartFn();
  }
);
function chartFn() {
  var chartDom = document.getElementById("linezss");
  var myChart = echarts.init(chartDom);
  var option;   
  option = {
    tooltip: {
      trigger: "axis",
      axisPointer: {
        lineStyle: {
          color: "#ddd"
        }
      },
      backgroundColor: "rgba(255,255,255,1)",
      padding: [5, 10],
      textStyle: {
        color: "#7588E4"
      },
      extraCssText: "box-shadow: 0 0 5px rgba(0,0,0,0.3)"
    },
    // legend: {
    //     right: 20,
    //     orient: 'vertical',
    //     data: ['今日','昨日']
    // },
    xAxis: {
      type: "category",
      data: props.data.map((item) => item.x),
      boundaryGap: false,
      splitLine: {
        lineStyle: {
          color: ["#034079"]
        }
      },
      axisTick: {
        show: false
      },
      axisLine: {
        lineStyle: {
          color: "#ddd"
        }
      },
      axisLabel: {
        margin: 10,
        textStyle: {
          fontSize: 14
        }
      }
    },
    yAxis: {
      type: "value",
      splitLine: {
        lineStyle: {
          color: ["#034079"]
        }
      },
      axisTick: {
        show: false
      },
      axisLine: {
        lineStyle: {
          color: "#ddd"
        }
      },
      axisLabel: {
        margin: 10,
        textStyle: {
          fontSize: 14
        }
      },
      minInterval: 1,
      min: 0,
      max: function (value) {
        return value.max + 5;
      }
    },
    series: [
      {
        name: "今日",
        type: "line",
        label: {
          show: true,
          position: "top"
        },
        smooth: true,
        // showSymbol: false,
        symbol: "circle",
        symbolSize: 6,
        data: props.data.map((item) => item.y),
        // areaStyle: {
        //     normal: {
        //         color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
        //             offset: 0,
        //             color: 'rgba(199, 237, 250,0.5)'
        //         }, {
        //             offset: 1,
        //             color: 'rgba(199, 237, 250,0.2)'
        //         }], false)
        //     }
        // },
        itemStyle: {
          normal: {
            color: "rgb(4, 145, 216)"
          }
        },
        lineStyle: {
          normal: {
            width: 3
          }
        }
      }
    ]
  };
  option && myChart.setOption(option);
  document.getElementById("linezss").setAttribute("_echarts_instance_", "");
}
onMounted(() => {
  chartFn();
  window.addEventListener("resize", () => {
    chartFn();
  });
  window.onresize = function () {
    chartFn();
  };
});
</script>

<style lang="scss" scoped>
.aaa {
  color: rgb(238, 6, 6);
}
.ech {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
}
</style>
