Refine expense pie chart with "other" category and label lines
This commit is contained in:
+118
-18
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useEffect } from "react";
|
||||
import React, { useState, useEffect, useMemo } from "react";
|
||||
import { RefreshCcw } from "lucide-react";
|
||||
import {
|
||||
BarChart,
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
PieChart,
|
||||
Pie,
|
||||
Cell,
|
||||
Legend,
|
||||
} from "recharts";
|
||||
import {
|
||||
getOverview,
|
||||
@@ -34,8 +35,90 @@ const COLORS = [
|
||||
"#e11d48",
|
||||
];
|
||||
|
||||
// 不参与支出饼图的分类
|
||||
const EXCLUDED_CATEGORIES = ["投资理财", "转账红包"];
|
||||
// 不参与支出饼图的分类(投资/转账/赠与类)
|
||||
const EXCLUDED_PATTERNS = ["投资", "转账", "赠与", "红包"];
|
||||
|
||||
function isExcluded(category) {
|
||||
if (!category) return true;
|
||||
const s = category.trim().toLowerCase();
|
||||
return EXCLUDED_PATTERNS.some(p => s.includes(p.toLowerCase()));
|
||||
}
|
||||
|
||||
/** 将小切片合并为"其他",最多保留 topN-1 个分类 + 1 个"其他" */
|
||||
function buildPieData(categoryStats, topN = 8) {
|
||||
const filtered = (categoryStats || []).filter(
|
||||
(c) => !isExcluded(c.category),
|
||||
);
|
||||
if (filtered.length === 0) return { slices: [], totalAmount: 0 };
|
||||
|
||||
const totalAmount = filtered.reduce((s, c) => s + c.total_amount, 0);
|
||||
if (filtered.length <= topN) return { slices: filtered, totalAmount };
|
||||
|
||||
const top = filtered.slice(0, topN - 1);
|
||||
const rest = filtered.slice(topN - 1);
|
||||
const otherAmount = rest.reduce((s, c) => s + c.total_amount, 0);
|
||||
|
||||
return {
|
||||
slices: [
|
||||
...top,
|
||||
{
|
||||
category: "其他",
|
||||
count: rest.reduce((s, c) => s + c.count, 0),
|
||||
total_amount: otherAmount,
|
||||
},
|
||||
],
|
||||
totalAmount,
|
||||
};
|
||||
}
|
||||
|
||||
const RADIAN = Math.PI / 180;
|
||||
|
||||
/** 自定义 label:拉出引导线 + 百分比 */
|
||||
function renderPieLabel({
|
||||
cx,
|
||||
cy,
|
||||
midAngle,
|
||||
innerRadius,
|
||||
outerRadius,
|
||||
percent,
|
||||
category,
|
||||
}) {
|
||||
const radius = outerRadius + 30;
|
||||
const x = cx + radius * Math.cos(-midAngle * RADIAN);
|
||||
const y = cy + radius * Math.sin(-midAngle * RADIAN);
|
||||
|
||||
// 引导线起点
|
||||
const mx = cx + (outerRadius + 8) * Math.cos(-midAngle * RADIAN);
|
||||
const my = cy + (outerRadius + 8) * Math.sin(-midAngle * RADIAN);
|
||||
// 饼内点
|
||||
const ex = cx + (outerRadius + 2) * Math.cos(-midAngle * RADIAN);
|
||||
const ey = cy + (outerRadius + 2) * Math.sin(-midAngle * RADIAN);
|
||||
|
||||
const pct = (percent * 100).toFixed(0);
|
||||
const textAnchor = x > cx ? "start" : "end";
|
||||
|
||||
return (
|
||||
<>
|
||||
<polyline
|
||||
points={`${ex},${ey} ${mx},${my} ${x},${y}`}
|
||||
stroke="#94a3b8"
|
||||
strokeWidth={1}
|
||||
fill="none"
|
||||
/>
|
||||
<text
|
||||
x={x + (x > cx ? 4 : -4)}
|
||||
y={y}
|
||||
textAnchor={textAnchor}
|
||||
dominantBaseline="central"
|
||||
fontSize={11}
|
||||
fill="#475569"
|
||||
>
|
||||
<tspan fontWeight={600}>{category}</tspan>
|
||||
<tspan fill="#94a3b8"> {pct}%</tspan>
|
||||
</text>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Dashboard() {
|
||||
const [overview, setOverview] = useState(null);
|
||||
@@ -61,6 +144,12 @@ export default function Dashboard() {
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
const ov = overview?.overview || {};
|
||||
const { slices: pieData, totalAmount: pieTotal } = useMemo(
|
||||
() => buildPieData(overview?.categoryStats),
|
||||
[overview],
|
||||
);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="loading">
|
||||
@@ -69,11 +158,6 @@ export default function Dashboard() {
|
||||
);
|
||||
}
|
||||
|
||||
const ov = overview?.overview || {};
|
||||
const pieData = (overview?.categoryStats || []).filter(
|
||||
(c) => !EXCLUDED_CATEGORIES.includes(c.category),
|
||||
);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div
|
||||
@@ -186,33 +270,49 @@ export default function Dashboard() {
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3>🏷️ 支出分类占比</h3>
|
||||
{pieData.length > 0 && (
|
||||
<span style={{ fontSize: 13, color: "var(--text-secondary)" }}>
|
||||
合计 {formatAmount(-pieTotal)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
{pieData.length > 0 ? (
|
||||
<ResponsiveContainer width="100%" height={280}>
|
||||
<ResponsiveContainer width="100%" height={300}>
|
||||
<PieChart>
|
||||
<Pie
|
||||
data={pieData.slice(0, 8)}
|
||||
data={pieData}
|
||||
cx="50%"
|
||||
cy="50%"
|
||||
outerRadius={100}
|
||||
innerRadius={50}
|
||||
outerRadius={95}
|
||||
paddingAngle={2}
|
||||
dataKey="total_amount"
|
||||
nameKey="category"
|
||||
label={({ category, percent }) =>
|
||||
`${category} ${(percent * 100).toFixed(0)}%`
|
||||
}
|
||||
label={renderPieLabel}
|
||||
labelLine={false}
|
||||
isAnimationActive={true}
|
||||
>
|
||||
{pieData.slice(0, 8).map((_, idx) => (
|
||||
{pieData.map((_, idx) => (
|
||||
<Cell key={idx} fill={COLORS[idx % COLORS.length]} />
|
||||
))}
|
||||
</Pie>
|
||||
<Tooltip formatter={(value) => formatAmount(value)} />
|
||||
<Tooltip
|
||||
formatter={(value) => formatAmount(-value)}
|
||||
labelFormatter={(name) => `分类: ${name}`}
|
||||
/>
|
||||
</PieChart>
|
||||
</ResponsiveContainer>
|
||||
) : overview?.categoryStats?.length > 0 ? (
|
||||
<div className="empty-state" style={{ padding: "40px 0" }}>
|
||||
<div className="icon">📊</div>
|
||||
<p style={{ fontSize: 13, color: "var(--text-secondary)" }}>
|
||||
投资/转账类不参与支出占比统计
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="empty-state">
|
||||
<div className="empty-state" style={{ padding: "40px 0" }}>
|
||||
<div className="icon">📭</div>
|
||||
<p>暂无分类数据</p>
|
||||
<p>暂无分类数据,请先对账并 AI 分类</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user