Files
pqs-9100_client/frontend/src/components/echarts/pie/default.vue

175 lines
4.8 KiB
Vue
Raw Normal View History

2024-08-23 13:19:20 +08:00
<!-- 默认饼图 -->
<template>
<div class="pie" ref="chartsRef"></div>
2024-08-23 13:19:20 +08:00
</template>
<script lang="ts" setup>
import * as echarts from "echarts";
2024-12-17 13:57:45 +08:00
import { ref } from "vue";
2024-08-23 13:19:20 +08:00
const chartsRef = ref();
const props = defineProps({
//饼图数据
chartsData: {
type: Array,
default: () => [],
},
//自定义数据
customData: {
type: Object,
default: () => ({}),
},
//legend配置
legendData: {
type: Object,
default: () => ({}),
},
});
const customData: any = ref({}),
2024-08-27 14:01:26 +08:00
legendData: any = ref({}),
chart: any = ref();
2024-08-23 13:19:20 +08:00
const init = () => {
customData.value = {
title: "", //标题
textAlign: "left", //标题位置可选属性left 可选属性值 left,right,center
ratio: true, //是否显示数值占比,默认不显示
isRing: false, //是否环形图
isRadius: false, //是否圆角
2024-11-25 21:11:10 +08:00
isSpace: false, //是否显示间隔
isLabelLine: true, //是否显示引导线
2024-08-23 13:19:20 +08:00
...props.customData,
};
legendData.value = {
icon: "roundRect", // 图例项的icon,类型包括 circle(圆形),rect(正方形),//roundRect(圆角正方形),triangle(三角形),diamond(菱形),//pin(大头针行),arrow(箭头形),none(无图例项的icon)
orient: "vertical", //图例排列方向
left: "right", //可选属性left,right,top,bottom,可选属性值 left,right,top,bottom,px,百分比,数值,
2024-12-05 18:14:43 +08:00
itemGap: 10, // 设置图例项之间的间隔为20
2024-08-23 13:19:20 +08:00
...props.legendData,
};
2024-09-02 16:10:33 +08:00
chart.value = chartsRef.value && echarts.init(chartsRef.value);
2024-08-23 13:19:20 +08:00
var option = {
title: {
text: customData.value.title,
left: customData.value.textAlign,
},
2024-12-04 21:36:12 +08:00
legend:legendData.value,
// legend: {
// icon: legendData.value.icon, // 图例项的icon,类型包括 circle(圆形),rect(正方形),//roundRect(圆角正方形),triangle(三角形),diamond(菱形),//pin(大头针行),arrow(箭头形),none(无图例项的icon)
// orient: "vertical", //图例排列方向
// left: legendData.value.left, //可选属性left,right,top,bottom,可选属性值 left,right,top,bottom,px,百分比,数值,
// itemGap: 1, // 设置图例项之间的间隔为20
// formatter: function (name) {
// const item = props.chartsData.filter(item=>item.name==name)
// console.log(item)
// if(item)
// return item[0].value;
// },
// },
2024-08-23 13:19:20 +08:00
tooltip: {
show: true,
trigger: "item",
formatter: customData.value.ratio ? `{b} : {c} ({d}%)` : "{b} :{c} ",
borderWidth: 1,
},
series: [
{
type: "pie",
2024-09-02 16:29:04 +08:00
radius: customData.value.isRing ? ["55", "75"] : "80%",
2024-08-23 13:19:20 +08:00
data: props.chartsData,
2024-12-17 13:57:45 +08:00
formatter: function (name: any) {
2024-12-04 21:36:12 +08:00
const item = props.chartsData.filter(item=>item.name==name)
console.log(item)
if(item)
return item[0].value;
},
center: ["55%", "50%"], // 设置饼图的中心位置
2024-08-23 13:19:20 +08:00
// padAngle: 2,
minAngle: 15, //最小角度
startAngle: 270, //起始角度
2024-08-23 13:19:20 +08:00
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: "rgba(0, 0, 0, 0.5)",
},
},
2024-09-02 16:10:33 +08:00
label: {
normal: {
show: true,
2024-12-04 21:36:12 +08:00
position: "outside",
2024-09-02 16:10:33 +08:00
textStyle: {
2024-12-04 21:36:12 +08:00
//color: "#fff",
2024-09-02 16:10:33 +08:00
fontSize: 12,
},
2024-12-04 21:36:12 +08:00
formatter: function (data) {
2024-12-05 18:14:43 +08:00
return data.value +'台'
2024-12-04 21:36:12 +08:00
}
2024-09-02 16:10:33 +08:00
},
},
2024-08-23 13:19:20 +08:00
itemStyle: {
borderRadius: customData.value.isRadius ? 10 : 0,
borderColor: customData.value.isSpace ? "#fff" : "",
borderWidth: customData.value.isSpace ? 2 : 0,
},
labelLine: {
show: customData.value.isLabelLine,
},
},
],
};
2024-09-02 16:10:33 +08:00
option && chart.value && chart.value.setOption(option);
setTimeout(() => {
2024-08-27 14:01:26 +08:00
chart.value.resize();
}, 0);
2024-08-23 13:19:20 +08:00
};
2024-11-29 13:45:48 +08:00
const reSize = (widthValue: number,heightValue: number,silentValue: boolean) => {
if (chart.value) {
chart.value.resize({
width: widthValue,
height: heightValue,
silent: silentValue,
});
}
};
const resizeCharts = () => {
2024-12-06 09:11:44 +08:00
//console.log(chart.value,111111);
2024-11-29 13:45:48 +08:00
2024-08-27 14:01:26 +08:00
if (chart.value) {
chart.value.resize();
}
};
window.addEventListener("resize", resizeCharts);
onUnmounted(() => {
2024-08-27 14:01:26 +08:00
if (chart.value) {
chart.value.resize();
}
window.removeEventListener("resize", resizeCharts);
2024-08-27 14:01:26 +08:00
if (chart.value != null && chart.value.dispose) {
chart.value.dispose(); // 销毁图表
}
});
2024-08-23 13:19:20 +08:00
watch(
() => props.chartsData,
(val, oldVal) => {
if (val) {
init();
}
},
{
deep: true,
}
);
onMounted(() => {
init();
});
2024-11-29 13:45:48 +08:00
defineExpose({ init,reSize });
2024-08-23 13:19:20 +08:00
</script>
<style lang="scss" scoped>
.pie {
2024-08-23 13:19:20 +08:00
width: 100%;
height: 100%;
padding: 10px;
box-sizing: border-box;
2024-08-23 13:19:20 +08:00
}
</style>