Skip to content

Commit 830ad05

Browse files
committed
✨ Add risk and liquidity distribution functions #26
1 parent e4c4818 commit 830ad05

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

client/src/components/ChartWidget/DonutChart.svelte

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@
1111
value?: number
1212
}
1313
14+
interface RiskLiquidityItem {
15+
name: string
16+
value: number
17+
color: string
18+
}
19+
1420
export let sources
1521
1622
const options: ApexOptions | any = genDonutOptions()
1723
let percentages: PercentageItem[] = []
24+
let riskDistribution: RiskLiquidityItem[] = []
25+
let liquidityDistribution: RiskLiquidityItem[] = []
1826
1927
$: {
2028
options.series = []
@@ -28,6 +36,7 @@
2836
options.labels = labels
2937
options.series = series
3038
regenWealthPercentages(options)
39+
calculateRiskLiquidityDistribution(sources, totalSumNum)
3140
}
3241
3342
const regenWealthPercentages = (sources) => {
@@ -40,11 +49,111 @@
4049
})
4150
percentages = percentages.sort((a, b) => b.value - a.value)
4251
}
52+
53+
const calculateRiskLiquidityDistribution = (sources, totalSum) => {
54+
// 计算风险分布
55+
const riskGroups = {}
56+
sources.forEach((item) => {
57+
const risk = item.risk || 'LOW'
58+
if (!riskGroups[risk]) {
59+
riskGroups[risk] = 0
60+
}
61+
riskGroups[risk] += item.amount
62+
})
63+
64+
riskDistribution = Object.entries(riskGroups).map(([risk, amount]) => ({
65+
name: $_(risk.toLowerCase()),
66+
value: Number(((Number(amount) / totalSum) * 100).toFixed(2)),
67+
color: getRiskColor(risk),
68+
}))
69+
70+
// 计算流动性分布
71+
const liquidityGroups = {}
72+
sources.forEach((item) => {
73+
const liquidity = item.liquidity || 'GOOD'
74+
if (!liquidityGroups[liquidity]) {
75+
liquidityGroups[liquidity] = 0
76+
}
77+
liquidityGroups[liquidity] += item.amount
78+
})
79+
80+
liquidityDistribution = Object.entries(liquidityGroups).map(([liquidity, amount]) => ({
81+
name: $_(liquidity.toLowerCase()),
82+
value: Number(((Number(amount) / totalSum) * 100).toFixed(2)),
83+
color: getLiquidityColor(liquidity),
84+
}))
85+
}
86+
87+
const getRiskColor = (risk: string): string => {
88+
switch (risk) {
89+
case 'LOW':
90+
return '#2edfa3' // success color
91+
case 'MIDDLE':
92+
return '#f8d826' // link color
93+
case 'HIGH':
94+
return '#ff4582' // mark color
95+
default:
96+
return '#B7B8B9' // grey color
97+
}
98+
}
99+
100+
const getLiquidityColor = (liquidity: string): string => {
101+
switch (liquidity) {
102+
case 'GOOD':
103+
return '#2edfa3' // success color
104+
case 'MIDDLE':
105+
return '#f8d826' // link color
106+
case 'POOR':
107+
return '#ff4582' // mark color
108+
default:
109+
return '#B7B8B9' // grey color
110+
}
111+
}
43112
</script>
44113

45114
<Card size="xl" class="h-fit shadow-none md:p-4">
46115
<Caption title={$_('assetAllocation')} subtitle={$_('currentAssetStatus')}></Caption>
116+
117+
<!-- 风险与流动性分布概览 -->
118+
<div class="my-4 grid grid-cols-2 gap-4 md:grid-cols-1">
119+
<!-- 风险分布 -->
120+
<div class="rounded-lg border border-gray-200 p-4">
121+
<h3 class="mb-3 text-lg font-semibold text-gray-800">{$_('risk')} {$_('distribution')}</h3>
122+
<div class="space-y-3">
123+
{#each riskDistribution as item}
124+
<div class="flex items-center justify-between">
125+
<div class="flex items-center space-x-2">
126+
<div class="h-3 w-3 rounded-full" style="background-color: {item.color}"></div>
127+
<span class="text-sm font-medium text-gray-700">{item.name}</span>
128+
</div>
129+
<span class="text-blue text-sm font-bold">{item.value}%</span>
130+
</div>
131+
{/each}
132+
</div>
133+
</div>
134+
135+
<!-- 流动性分布 -->
136+
<div class="rounded-lg border border-gray-200 p-4">
137+
<h3 class="mb-3 text-lg font-semibold text-gray-800">
138+
{$_('liquidity')}
139+
{$_('distribution')}
140+
</h3>
141+
<div class="space-y-3">
142+
{#each liquidityDistribution as item}
143+
<div class="flex items-center justify-between">
144+
<div class="flex items-center space-x-2">
145+
<div class="h-3 w-3 rounded-full" style="background-color: {item.color}"></div>
146+
<span class="text-sm font-medium text-gray-700">{item.name}</span>
147+
</div>
148+
<span class="text-blue text-sm font-bold">{item.value}%</span>
149+
</div>
150+
{/each}
151+
</div>
152+
</div>
153+
</div>
154+
47155
<Chart {options}></Chart>
156+
48157
<div class="my-6 flex flex-wrap items-center justify-start pt-4 sm:pt-6 md:justify-evenly">
49158
{#each percentages as item}
50159
<SmallPanel title={item.name} subtitle={`${item.value}%`} />

client/src/lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"poor": "Poor",
2929
"all": "All",
3030
"total": "Total",
31+
"distribution": "Distribution",
3132
"assetAllocation": "Asset Allocation",
3233
"currentAssetStatus": "Current Asset Status",
3334
"assetChanges": "Asset Changes",

client/src/lang/fr.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"poor": "Mauvais",
2929
"all": "Tous",
3030
"total": "Total",
31+
"distribution": "Distribution",
3132
"assetAllocation": "Allocation d'actifs",
3233
"currentAssetStatus": "État actuel des actifs",
3334
"assetChanges": "Changements d'actifs",

client/src/lang/ja.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"poor": "不良",
2929
"all": "すべて",
3030
"total": "合計",
31+
"distribution": "分布",
3132
"assetAllocation": "資産配分",
3233
"currentAssetStatus": "現在の資産状況",
3334
"assetChanges": "資産変動",

client/src/lang/zh-tw.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"poor": "",
2929
"all": "全部",
3030
"total": "總計",
31+
"distribution": "分佈",
3132
"assetAllocation": "資產配置",
3233
"currentAssetStatus": "當前資產狀況",
3334
"assetChanges": "資產变化",

client/src/lang/zh.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"poor": "",
2929
"all": "全部",
3030
"total": "总计",
31+
"distribution": "分布",
3132
"assetAllocation": "资产配置",
3233
"currentAssetStatus": "当前资产状况",
3334
"assetChanges": "资产变化",

0 commit comments

Comments
 (0)