<template>
  <div ref="chart" style="width: 100%; height: 360px"></div>
</template>

<script setup>
import { onMounted, reactive, ref } from "vue";
// 局部引入echarts核心模块
import * as echarts from "echarts";

const chart = ref(); // 创建DOM引用

const data = [120, 200, 50, 80, 70];
const data2 = [12, 20, 5, 8, 7];
const lineData = [150, 230, 24, 218, 135];
const lineData2 = [15, 23, 4, 18, 15];
const max = data
  .concat(lineData)
  .reduce((pre, cur) => (pre > cur ? pre : cur), 0); //找到这个新数组中的最大值
// 背景颜色
const color = [
  {
    type: "linear",
    x: 0,
    x2: 0,
    y: 0,
    y2: 1,
  },
];
const option = {
  tooltip: {
    trigger: "axis",
    formatter: "{b0}<br/> {a0}:{c0}<br/>{a1}:{c1}",

  },

  legend: {
    data: [
      "困难残疾人生活补贴人数",
      "护理补贴人数",
      "",
      "困难残疾人生活补贴金额",
      "护理补贴金额",
    ],
    top: "3%",
    textStyle: {
      fontSize: 12,
      color: "#ffffff", //上文字标题颜色
    },
  },
  grid: {
    top: "25%",
    left: "6%",
    right: "9%",
    bottom: "5%",
    containLabel: true,
    color: "#ffffff",
  },
  calculable: true,
  color,
  xAxis: [
    {
      type: "category",

      axisLabel: {
        //坐标轴刻度标签的相关设置
        textStyle: {
          color: "#ffffff",
        },
      },
      axisTick: {
        show: false, // 设置轴刻度不显示
      },
      data: ["2019", "2020", "2021", "2022", "2023"],
    },
    {
      type: "category",
      show: false,
      data: ["2019", "2020", "2021", "2022", "2023"],
    },
  ],
  yAxis: [
    {
      type: "value",
      scale: true,
      name: "补贴人数",
      max: max,
      splitLine: {
        //分割线配置
        show: false,
        lineStyle: {
          color: "#ffffff",
          width: 1,
        },
      },
      axisLabel: {
        //y轴文字的配置
        textStyle: {
          color: "#ffffff",
        },
      },
      axisLine: {
        //y轴线的颜色以及宽度
        show: false,
        lineStyle: {
          color: "#ffffff",
          width: 1,
          type: "solid",
        },
      },
    },
    {
      type: "value",
      scale: true,
      min: 0,
      max: max,
      name: "救助金额/元",
      splitLine: {
        show: false,
        lineStyle: {
          color: "rgba(226, 226, 226, 0.3)",
          width: 1,
        },
      },
      axisLabel: {
        //y轴文字的配置
        textStyle: {
          color: "#ffffff",
        },
      },
      axisLine: {
        //y轴线的颜色以及宽度
        show: false,
        lineStyle: {
          color: "#ffffff",
          width: 1,
          type: "solid",
        },
      },
    },
  ],
  series: [
    {
      name: "困难残疾人生活补贴金额",
      data: lineData,
      type: "line", //线状图
      itemStyle: {
        borderColor: "#00FCFF",
        borderWidth: 1,
        color: "#00FCFF",
      },
    },
    {
      name: "护理补贴金额",
      data: lineData2,
      type: "line", //线状图
      itemStyle: {
        borderColor: "#00FCFF",
        borderWidth: 1,
        color: "#2468FF",
      },
    },
    {
      name: "困难残疾人生活补贴人数",
      data: data,
      barWidth: 10,
      type: "bar",
      itemStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          {
            offset: 0,
            color: "rgba(142, 187, 255, 1)",
          },
          {
            offset: 1,
            color: "rgba(142, 187, 255, 0.20)",
          },
        ]),
      },
    },
 
    {
      name: "护理补贴人数",
      data: data2,
      barWidth: 10,
      type: "bar",
      itemStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          {
            offset: 0,
            color: "rgba(23, 237, 255, 1)",
          },
          {
            offset: 1,
            color: "rgba(23, 237, 255, 0.20)",
          },
        ]),
      },
    },
    {
      xAxisIndex: 1,
      itemStyle: {
        color: "rgba(180, 180, 180, 0.2)", //外阴影背景
      },
      data: data.map(() => max),
      barWidth: 60, //外阴影背景宽
      emphasis: {
        itemStyle: {
          color: {
            type: "linear",
            x: 0,
            x2: 0,
            y: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: "rgba(64, 247, 176, 0.25)",
              },
              {
                offset: 1,
                color: "rgba(17, 34, 64, 0.25)",
              },
            ],
          },
        },
      },
      type: "bar",
    },
  ],
};

// 使用生命钩子
onMounted(() => {
  // 基于准备好的dom，初始化echarts实例
  // var myChart = echarts.init(document.getElementById('main'));
  // Vue3中： 需要引入
  var myChart = echarts.init(chart.value);

  // init(); // vue3.2没有this
  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(option);

  // 单图表响应式: 跟随浏览器大小改变
  //   window.addEventListener("resize", () => {
  //     myChart.resize();
  //   });
});
</script>

<style scoped></style>
