diff --git a/client/src/pages/Dashboard.js b/client/src/pages/Dashboard.js index 3e3fc90..86b8196 100644 --- a/client/src/pages/Dashboard.js +++ b/client/src/pages/Dashboard.js @@ -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 ( + <> + + cx ? 4 : -4)} + y={y} + textAnchor={textAnchor} + dominantBaseline="central" + fontSize={11} + fill="#475569" + > + {category} + {pct}% + + + ); +} 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 (
@@ -69,11 +158,6 @@ export default function Dashboard() { ); } - const ov = overview?.overview || {}; - const pieData = (overview?.categoryStats || []).filter( - (c) => !EXCLUDED_CATEGORIES.includes(c.category), - ); - return (

🏷️ 支出分类占比

+ {pieData.length > 0 && ( + + 合计 {formatAmount(-pieTotal)} + + )}
{pieData.length > 0 ? ( - + - `${category} ${(percent * 100).toFixed(0)}%` - } + label={renderPieLabel} labelLine={false} + isAnimationActive={true} > - {pieData.slice(0, 8).map((_, idx) => ( + {pieData.map((_, idx) => ( ))} - formatAmount(value)} /> + formatAmount(-value)} + labelFormatter={(name) => `分类: ${name}`} + /> + ) : overview?.categoryStats?.length > 0 ? ( +
+
📊
+

+ 投资/转账类不参与支出占比统计 +

+
) : ( -
+
📭
-

暂无分类数据

+

暂无分类数据,请先对账并 AI 分类

)}
diff --git a/data/scripts/reco.sqbpro b/data/scripts/reco.sqbpro index 4b6a895..54f588e 100644 --- a/data/scripts/reco.sqbpro +++ b/data/scripts/reco.sqbpro @@ -1 +1,11 @@ -
SELECT * FROM deals
+
SELECT + COUNT(*) as total_count, + COALESCE(SUM(CASE WHEN d.direction = 'income' THEN d.amount ELSE 0 END), 0) as total_income, + COALESCE(SUM(CASE WHEN d.direction = 'expense' THEN ABS(d.amount) ELSE 0 END), 0) as total_expense, + COUNT(CASE WHEN d.reviewed = 0 AND ABS(d.amount) >= 100 THEN 1 END) as pending_review, + COUNT(CASE WHEN d.reviewed = 1 THEN 1 END) as reviewed_count, + COALESCE(SUM(d.match_count), 0) as matched_count + FROM deals d + + + select * from deals
diff --git a/readme.md b/readme.md index 451c82a..0ba117d 100644 --- a/readme.md +++ b/readme.md @@ -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.更有价值的报表功能要继续开发