Refine expense pie chart with "other" category and label lines

This commit is contained in:
2026-05-06 02:19:34 +08:00
parent f357fba2a5
commit 189ebf6035
3 changed files with 138 additions and 26 deletions
+118 -18
View File
@@ -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>
File diff suppressed because one or more lines are too long
+9 -7
View File
@@ -1,11 +1,13 @@
`关闭powershell的脚本限制
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
遭遇zed内npm instal失败问题,报错为sqlite3-better组建需要调用原生node-gyp编译。报错大意为better-sqlite3需要编译原生模块但网络超时调了下梯子和莫名其妙好了的。
`zed内npm instal失败问题,报错为sqlite3-better组建需要调用原生node-gyp编译。报错大意为better-sqlite3需要编译原生模块但网络超时调了下梯子和莫名其妙好了的。
1.先把数据导入的功能做清楚
2.把交易合并功能做清楚
2.把调用大模型做分类的功能做清楚
为“AI分类全部”专门写一个服务,这个方法不需要传参,过程与原有方法类似,不过它的逻辑是取出当前全部订单,写成并行的。
取消在导入账单时调用大模型分类的逻辑
3.最后调报表
todo
1.系统设置加一个选项,将投资赠与去除统计
2.叠一个用户管理体系
3.总收入和总支出这个和投资赠与的关系处理一下,看怎么搞最合适
4.分类管理变更之后历史记录怎么处理要构思一下
5.导入账单的类型要扩充,农行只有pdf的问题要想办法处理一下,这个网银怎么登想想办法。最好是pdf直接转
6.更有价值的报表功能要继续开发