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

<script setup>
import { onBeforeMount, reactive, defineProps, ref, nextTick } 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: {},
});
const getOption = () => {
  data.option = {
    tooltip: {
      trigger: "axis",
      formatter: "{b0}<br />{a1}:{c1} <br />{a3}:{c3} ",
    },
    legend: {
      data: ["职工养老保险发放人次", "城乡养老保险发放人次"],
      top: "8%",
      right: "11%",
      textStyle: {
        fontSize: 16,
        color: "#ffffff",
      },
    },
    grid: {
      left: "1%",
      right: "10%",
      bottom: "0%",
      containLabel: true,
    },
    calculable: true,
    xAxis: [
      {
        type: "category",
        axisLabel: {
          //坐标轴刻度标签的相关设置
          textStyle: {
            color: "#ffffff",
            fontSize: 16,
          },
        },
        data: data.year,
      },
      {
        axisTick: false,
        type: "category",
        data: data.year,
        axisLabel: {
          show: false,
        },
      },
    ],

    yAxis: [
      {
        type: "value",
        splitLine: {
          show: true,
          lineStyle: {
            color: "rgba(226, 226, 226, 0.3)",
            fontSize: 16,
            width: 1,
          },
        },
        axisLabel: {
          //坐标轴刻度标签的相关设置
          textStyle: {
            color: "#ffffff",
          },
        },
      },
      {
        type: "value",
        splitLine: {
          show: false,
          lineStyle: {
            type: "solid",
            color: "rgb(221, 242, 255,0.1)",
          },
        },
        axisLine: {
          show: false,
          lineStyle: {
            type: "dotted",
          },
        },
        axisLabel: {
          show: false,
          fontSize: 16,
          fontFamily: "MicrosoftYaHei",
          color: "#ffffff",
          lineHeight: 19,
        },
      },
    ],
    series: [
      {
        z: 1,
        name: "上部1",
        type: "pictorialBar",
        symbolPosition: "end",
        data: data.list1,
        symbol: "diamond",
        symbolOffset: ["-60%", "-50%"],
        symbolSize: [19, 10],
        itemStyle: {
          borderColor: "#2fffa4",
          color: "rgba(142, 187, 255, 1)",
        },
      },
      {
        z: 1,
        name: "职工养老保险发放人次",
        type: "bar",
        barGap: 0.3 /*多个并排柱子设置柱子之间的间距*/,
        data: data.list1,
        barWidth: "18%",
        itemStyle: {
          color: {
            type: "linear",
            x: 0,
            x2: 1,
            y: 0,
            y2: 0,
            colorStops: [
              { offset: 0, color: "rgba(142, 187, 255, .7)" },
              { offset: 0.5, color: "rgba(142, 187, 255, .7)" },
              { offset: 0.5, color: "rgba(142, 187, 255, .3)" },
              { offset: 1, color: "rgba(142, 187, 255, .5)" },
            ],
          },
        },
        // 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)",
        //     },
        //   ]),
        // },
        label: {
          show: true,
          position: "top",
          color: "#ffffff",
          formatter: function (data) {
            return data.value;
          },
        },
      },
      {
        z: 2,
        name: "上部1",
        type: "pictorialBar",
        symbolPosition: "end",
        data: data.list2,
        symbol: "diamond",
        symbolOffset: ["62%", "-50%"],
        symbolSize: [19, 10],
        itemStyle: {
          borderColor: "#32ffee",
          color: "rgba(23, 237, 255, 1)",
        },
      },
      {
        name: "城乡养老保险发放人次",
        type: "bar",
        data: data.list2,
        // barGap: "-50%",
        barGap: 0.3 /*多个并排柱子设置柱子之间的间距*/,
        barWidth: "18%",
        itemStyle: {
          color: {
            type: "linear",
            x: 0,
            x2: 1,
            y: 0,
            y2: 0,
            colorStops: [
              { offset: 0, color: "rgba(23, 237, 255, .7)" },
              { offset: 0.5, color: "rgba(23, 237, 255, .7)" },
              { offset: 0.5, color: "rgba(23, 237, 255, .3)" },
              { offset: 1, color: "rgba(23, 237, 255, .5)" },
            ],
          },
        },
        // 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)",
        //     },
        //   ]),
        // },
        label: {
          show: true,
          position: "top",
          color: "#ffffff",
          formatter: function (data) {
            return data.value;
          },
        },
      },
      {
        type: "bar",
        xAxisIndex: 1,
        yAxisIndex: 1,
        itemStyle: {
          color: "rgba(221, 242, 255, 0.1)",
        },
        data: data.year.map(() => 100),
        barWidth: 50,
      },
    ],
  };
};
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;
    getOption();
    setChart();
  }, 600);
});
</script>

<style scoped></style>