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

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

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

const props = defineProps({
  list1: {
    type: Array,
    default: () => {
      return [];
    },
  },
  list2: {
    type: Array,
    default: () => {
      return [];
    },
  },
  year: {
    type: Array,
    default: () => {
      return [];
    },
  },
});

const data = reactive({
  list1: [],
  list2: [],
  year: [],
  option: {},
  bg:[],
})

const getOption = () =>{
  data.option = {
  tooltip: {
    trigger: "axis",
    padding: [20, 10, 20, 10],
    formatter: "{b0}<br />{a1}:{c1} <br />{a2}:{c2} ",
  },
  legend: {
    data: ["特困发放人次", "低保发放人次"],
    top: "8%",
    right: "11%",
    textStyle: {
      fontSize: 16,
      color: "#ffffff",
    },
  },
  grid: {
    left: "1%",
    right: "10%",
    bottom: "3%",
    containLabel: true,
  },

  xAxis: {
    type: "category",
    // boundaryGap: false,
    data: data.year,
    axisLabel: {
      //坐标轴刻度标签的相关设置
      textStyle: {
        color: "#ffffff",
        fontSize: 16,
      },
    },
  },
  yAxis: {
    type: "value",
    splitLine: {
      show: true,
      lineStyle: {
        color: "rgba(255, 255, 255, 0.2)",
        fontSize: 16,
        width: 1,
      },
    },
    axisLabel: {
      //坐标轴刻度标签的相关设置
      textStyle: {
        color: "#ffffff",
        fontSize: 16,
      },
    },
  },
  series: [
    {
      name: "背景",
      type: "bar",
      data: data.bg,
      showBackground: true,
      backgroundStyle: {
        color: "rgba(180, 180, 180, 0.2)",
      },
    },
    {
      name: "特困发放人次",
      type: "line",
      symbol: "emptyCircle",

      symbolSize: 10,
      itemStyle: {
        borderColor: "#00FCFF",
        borderWidth: 1,
        color: "#00FCFF",
      },
      data: data.list1,
    },
    {
      name: "低保发放人次",
      type: "line",
      symbol: "emptyCircle",

      symbolSize: 10,
      itemStyle: {
        borderColor: "#2468FF",
        borderWidth: 1,
        color: "#2468FF",
      },
      data: data.list2,
    },
  ],
};
}
const setChart = () => {
  // Vue3中： 需要引入
  var myChart = echarts.init(chart.value);

  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(data.option);
}

// 使用生命钩子
onBeforeMount(() => {
  setTimeout(() => {
    data.list1 = props.list1
    data.list2 = props.list2
    data.year = props.year
       data.year.forEach(()=>{
      data.bg.push(0)
    })
    getOption()
    setChart()
  }, 600)
});
</script>

<style scoped>
</style>