<template>
  <div class="average">
    <!-- 标题区域 -->
    <div class="average_title">
      <div class="title_text">近7天平均房价</div>
      <img src="@/assets/images/33.png" />
    </div>

    <!-- 折线图区域 -->
    <div class="average_line" ref="lineChart"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import * as Echarts from "echarts";
import { getNearDay } from "@/utils/getNearDay.ts";

// 折线图DOM元素
const lineChart = ref(null);

onMounted(() => {
  // 初始化折线图
  initLineChart();
});

const initLineChart = () => {
  let myLineChart = Echarts.init(lineChart.value);
  // 折线图配置
  const options = {
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow",
      },
    },
    grid: {
      left: "1%",
      right: "4%",
      top: "22%",
      bottom: "5%",
      containLabel: true,
    },
    xAxis: {
      type: "category",
      boundaryGap: true,
      axisLabel: {
        color: "#fff",
        fontSize: 16,
        margin: 20,
      },
      data: getNearDay(7),
    },
    yAxis: {
      type: "value",
      name: "元",
      nameTextStyle: {
        color: "#fff",
        fontSize: 16,
      },
      axisLabel: {
        color: "#fff",
        fontSize: 16,
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: ["rgba(255, 255, 255, 0.15)"],
        },
      },
    },
    series: [
      {
        data: [160, 199, 126, 116, 216, 516, 126, 316],
        type: "line",
        smooth: true,
        color: "#1F92F6",
        tooltip: {
          valueFormatter: function (value: any) {
            return value + "元";
          },
        },
        areaStyle: {
          color: {
            type: "linear",
            x: 0,
            y: 1,
            x2: 0,
            y2: 0,
            colorStops: [
              {
                offset: 0,
                color: "rgba(31, 146, 246, 0)",
              },
              {
                offset: 1,
                color: "rgba(54, 161, 255, 0.6)",
              },
            ],
          },
        },
      },
    ],
  };
  myLineChart.setOption(options);
};
</script>

<style lang="scss" scoped>
.average {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;

  .average_title {
    position: relative;
    margin-top: 31px;
    width: 548px;
    height: 41px;
    line-height: 41px;

    .title_text {
      position: absolute;
      left: 15px;
      z-index: 1;
      font-size: 23.5px;
      font-weight: bold;
      color: #e0efff;
    }

    img {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 548px;
      height: 16px;
    }
  }

  .average_line {
    width: 548px;
    height: 350px;
  }
}
</style>
