From 9dcc38bdd901232677ecaf73957b5777ee3d870e Mon Sep 17 00:00:00 2001 From: Timi Date: Wed, 12 Aug 2026 21:54:00 +0800 Subject: [PATCH] pie chart --- .../component/StoreCustomerGenderChart.vue | 96 +++++++++---------- src/pages/store/component/useStoreChart.ts | 3 +- 2 files changed, 45 insertions(+), 54 deletions(-) diff --git a/src/pages/store/component/StoreCustomerGenderChart.vue b/src/pages/store/component/StoreCustomerGenderChart.vue index 65585a8..6e889e3 100644 --- a/src/pages/store/component/StoreCustomerGenderChart.vue +++ b/src/pages/store/component/StoreCustomerGenderChart.vue @@ -9,16 +9,18 @@ import type { EChartsOption } from "echarts"; import StoreChartAPI from "@/api/StoreChartAPI"; import type { CustomerGenderStatView } from "@/types/Customer"; -import { buildStoreBaseChartOption, useStoreChart } from "@/pages/store/component/useStoreChart"; - -const props = defineProps<{ - dateTextList: string[]; -}>(); +import { useStoreChart } from "@/pages/store/component/useStoreChart"; const genderColorMap = { - MALE: "#2563eb", - FEMALE: "#db2777", - UNKNOWN: "#64748b" + MALE: "#7A9BFF", + FEMALE: "#FF7A9B", + UNKNOWN: "#CCC" +} as const; + +const genderNameMap = { + MALE: "男", + FEMALE: "女", + UNKNOWN: "未知" } as const; // ---------- 图表数据 ---------- @@ -26,58 +28,36 @@ const genderColorMap = { const statList = ref([]); const chartOption = computed(() => ({ - ...buildStoreBaseChartOption(props.dateTextList), - grid: { - top: 24, - left: 12, - right: 12, - bottom: 12, - containLabel: true - }, + color: [genderColorMap.MALE, genderColorMap.FEMALE, genderColorMap.UNKNOWN], legend: { - show: false - }, - tooltip: { - trigger: "item" - }, - xAxis: { - type: "category", - data: ["男", "女", "未知"], - axisTick: { - show: false - }, - axisLabel: { + bottom: 0, + icon: "circle", + textStyle: { color: "#52607a" } }, + tooltip: { + trigger: "item", + formatter: "{b}:{c} 人({d}%)" + }, series: [ { name: "客户数", - type: "bar", - barWidth: 24, - data: [ - { - name: "男", - value: getGenderCount("MALE"), - itemStyle: { - color: genderColorMap.MALE - } - }, - { - name: "女", - value: getGenderCount("FEMALE"), - itemStyle: { - color: genderColorMap.FEMALE - } - }, - { - name: "未知", - value: getGenderCount("UNKNOWN"), - itemStyle: { - color: genderColorMap.UNKNOWN - } - } - ] + type: "pie", + top: 4, + bottom: 24, + radius: ["0%", "58%"], + center: ["50%", "46%"], + avoidLabelOverlap: true, + label: { + color: "#52607a", + formatter: "{b} {c} 人" + }, + labelLine: { + length: 16, + length2: 8 + }, + data: getGenderPieData() } ] })); @@ -95,6 +75,16 @@ async function loadCustomerGenderStat(): Promise { function getGenderCount(genderCode: "MALE" | "FEMALE" | "UNKNOWN"): number { return statList.value.find(item => item.genderCode === genderCode)?.count || 0; } + +function getGenderPieData(): Array<{ name: string; value: number; itemStyle: { color: string } }> { + return Object.entries(genderNameMap).map(([genderCode, name]) => ({ + name, + value: getGenderCount(genderCode as keyof typeof genderNameMap), + itemStyle: { + color: genderColorMap[genderCode as keyof typeof genderColorMap] + } + })); +}