diff --git a/src/App.tsx b/src/App.tsx index 53c41b1..7de7a1b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -512,17 +512,28 @@ export default function App() { customerData.forEach(item => { map[item.region] = (map[item.region] || 0) + item.total; }); data = Object.entries(map).map(([name, value]) => ({ name, value })).sort((a, b) => b.value - a.value); } else { - const map: Record = {}; - customerData.forEach(item => { map[item.city] = (map[item.city] || 0) + item.total; }); - const tot = Object.values(map).reduce((a, b) => a + b, 0); + const cityMap: Record = {}; + const cityToRegion: Record = {}; + customerData.forEach(item => { + cityMap[item.city] = (cityMap[item.city] || 0) + item.total; + if (!cityToRegion[item.city]) cityToRegion[item.city] = item.region; + }); + const tot = Object.values(cityMap).reduce((a, b) => a + b, 0); const threshold = tot * 0.05; let other = 0; - Object.entries(map).forEach(([name, value]) => { + Object.entries(cityMap).forEach(([name, value]) => { if (value >= threshold) data.push({ name, value }); else other += value; }); if (other > 0) data.push({ name: '其他', value: other }); - data.sort((a, b) => b.value - a.value); + // Sort by region group, then by value within group + const regionOrder = ['华东', '华南', '华中', '华北', '西北', '西南', '其他']; + data.sort((a, b) => { + const ra = regionOrder.indexOf(cityToRegion[a.name] || '其他'); + const rb = regionOrder.indexOf(cityToRegion[b.name] || '其他'); + if (ra !== rb) return ra - rb; + return b.value - a.value; + }); } return data; }, [customerData, customerChartView]);