Newer
Older
Nanping_sponge_JXKH / src / components / Echarts / pieChart.vue
@liyingjing liyingjing on 25 Oct 2 KB 海绵绩效考个
<template>
  <div :id="id" style="width: 100%; height: 100%"></div>
</template>
<script setup>
import { guid } from "@/utils/ruoyi";
const { proxy } = getCurrentInstance();
const id = guid();
const myChart = shallowRef("");
const props = defineProps({
  //刷新标志
  refresh: {
    type: [String, Number],
    default: 1,
  },
  //数据
  echartData: {
    type: Array,
    default: [],
  },
  //饼图半径值
  radius: {
    type: [Array, String],
    default: ["40%", "70%"],
  },
  //图列的排列方式
  legend: {
    type: Object,
    default: {
      top: "5%",
      orient: "vertical", //horizontal
      left: "right", //left ,center
    },
  },
  //饼块颜色设置
  colorList: {
    type: Array,
    default: [],
  },
});

watch(
  () => props.refresh,
  (value) => {
    //先销毁实例
    myChart.value && myChart.value.dispose();
    intChart();
  }
);

//自适应
function resizeTheChart() {
  if (myChart.value) {
    myChart.value.resize();
  }
}
//初始化
function intChart() {
  const dom = document.getElementById(id)
  if(!dom) return
  if(dom.clientWidth === 0 || dom.clientHeight === 0) return
  myChart.value = proxy.echarts.init(dom);
  // 绘制图表
  myChart.value.setOption({
    tooltip: {
      trigger: "item",
    },
    legend: props.legend,
    grid: {
      bottom: 0,
    },
    series: [
      {
        name: "",
        type: "pie",
        radius: props.radius,
        data: props.echartData,
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: "rgba(0, 0, 0, 0.5)",
          },
        },
        itemStyle: {
          //   normal: {
          //     color: function (colors) {
          //       var colorList = props.colorList;
          //       return colorList[colors.dataIndex];
          //     },
          //   },
        },
        labelLine: {
          length: 20,
          length2: 15,
          maxSurfaceAngle: 80
        },
        label: {
          show: true,
          formatter: "{d|{b}}\n{d|{d}%}",
          position: "outside",
          lineHeight: 18,
          rich: {
            d: {
              fontSize: 16,
              color: "#000000",
              align: "left",
              fontFamily: "SourceHanSansCN",
            },
            d: {
              fontSize: 14,
              color: "#000000",
              align: "left",
              fontFamily: "SourceHanSansCN",
            },
          },
        },
      },
    ],
  });
}
onMounted(() => {
  intChart();
  window.addEventListener("resize", resizeTheChart);
});
</script>