Newer
Older
KaiFengPC / src / components / Echarts / pieChart.vue
@zhangdeliang zhangdeliang on 20 May 1 KB 项目初始化
<template>
  <!-- 饼图 -->
  <div :id="id" style="width: 100%; height: 100%"></div>
</template>
<script setup>
import { guid } from '@/utils/ruoyi';
import useUserStore from '@/store/modules/user';
const pinias = useUserStore();
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%'],
  },
  //饼块颜色设置
  colorList: {
    type: Array,
    default: [],
  },
});

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

//自适应
function resizeTheChart() {
  if (myChart.value) {
    myChart.value.resize();
  }
}
//初始化
function intChart() {
  myChart.value = proxy.echarts.init(document.getElementById(id));
  myChart.value.clear();
  // 绘制图表
  myChart.value.setOption({
    color: pinias.$state.chartColor,
    grid: {
      bottom: 0,
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b} : {c}个 ({d}%)',
    },
    series: [
      {
        name: '',
        type: 'pie',
        radius: props.radius,
        data: props.echartData,
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
        label: {
          show: true,
          formatter: '{d|{b}}\n {c}个 ({d|{d}%})',
          position: 'outside',
          rich: {
            d: {
              fontSize: 13,
              color: '#509BE0',
              align: 'left',
              fontFamily: 'SourceHanSansCN',
            },
          },
        },
      },
    ],
  });
}
onMounted(() => {
  intChart();
  window.addEventListener('resize', resizeTheChart);
});
</script>