Skip to content

Commit 7c35ab6

Browse files
committed
Improve percentage calculation and display formatting
Updated SQL logic to ensure minimum non-zero percentages are at least 0.01%. Enhanced CategoryDetails.vue to display percentages <1% with one decimal place, improving accuracy and clarity in UI.
1 parent 89da5f4 commit 7c35ab6

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

db/rpc.sql

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ BEGIN
7272
COALESCE(SUM(e.amount), 0) as amount,
7373
COUNT(*)::BIGINT as count,
7474
CASE
75-
WHEN total_amount > 0 THEN ROUND((COALESCE(SUM(e.amount), 0) / total_amount * 100), 2)
75+
WHEN total_amount > 0 THEN
76+
GREATEST(
77+
ROUND((COALESCE(SUM(e.amount), 0) / total_amount * 100), 2),
78+
CASE WHEN COALESCE(SUM(e.amount), 0) > 0 THEN 0.01 ELSE 0 END
79+
)
7680
ELSE 0
7781
END as percentage
7882
FROM expenses e
@@ -315,7 +319,11 @@ BEGIN
315319
ct.total_amount as amount,
316320
ct.total_count as count,
317321
CASE
318-
WHEN total_amount > 0 THEN ROUND((ct.total_amount / total_amount * 100), 2)
322+
WHEN total_amount > 0 THEN
323+
GREATEST(
324+
ROUND((ct.total_amount / total_amount * 100), 2),
325+
CASE WHEN ct.total_amount > 0 THEN 0.01 ELSE 0 END
326+
)
319327
ELSE 0
320328
END as percentage,
321329
ct.has_children

src/components/charts/CategoryDetails.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class="mr-3"
1919
>
2020
<span class="text-white text-caption font-weight-bold">
21-
{{ Math.round(categoryItem.percentage) }}%
21+
{{ formatPercentage(categoryItem.percentage) }}
2222
</span>
2323
</v-avatar>
2424
</template>
@@ -56,4 +56,13 @@ const formatAmount = (amount: number) => {
5656
currency: 'CNY'
5757
}).format(amount)
5858
}
59+
60+
const formatPercentage = (percentage: number) => {
61+
// For percentages >= 1%, show as integer
62+
if (percentage >= 1) {
63+
return `${Math.round(percentage)}%`
64+
}
65+
// For percentages < 1%, show 1 decimal place
66+
return `${percentage.toFixed(1)}%`
67+
}
5968
</script>

0 commit comments

Comments
 (0)