Skip to content

Commit 848fdfd

Browse files
committed
chore: pie-chart total format
1 parent 63bddca commit 848fdfd

1 file changed

Lines changed: 47 additions & 1 deletion

File tree

src/components/tool-invocation/pie-chart.tsx

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,52 @@ const chartColors = [
4646
"var(--chart-5)",
4747
];
4848

49+
// Function to format large numbers with k, M, B, T units
50+
function formatLargeNumber(num: number | null | undefined): string {
51+
// Handle null, undefined, or invalid numbers
52+
if (num == null || isNaN(num) || !isFinite(num)) {
53+
return "0";
54+
}
55+
56+
// Handle negative numbers
57+
if (num < 0) {
58+
return `-${formatLargeNumber(-num)}`;
59+
}
60+
61+
// Handle zero
62+
if (num === 0) {
63+
return "0";
64+
}
65+
66+
if (num < 1000) {
67+
return num.toString();
68+
}
69+
70+
const units = [
71+
"",
72+
"k",
73+
"M",
74+
"B",
75+
"T",
76+
"Qa",
77+
"Qi",
78+
"Sx",
79+
"Sp",
80+
"Oc",
81+
"No",
82+
"Dc",
83+
];
84+
let unitIndex = 0;
85+
let value = num;
86+
87+
while (value >= 1000 && unitIndex < units.length - 1) {
88+
value /= 1000;
89+
unitIndex++;
90+
}
91+
92+
return `${value.toFixed(1)}${units[unitIndex]}`;
93+
}
94+
4995
export function PieChart(props: PieChartProps) {
5096
const { title, data, unit, description, prefix, jsonView = true } = props;
5197
// Calculate total value
@@ -134,7 +180,7 @@ export function PieChart(props: PieChartProps) {
134180
y={viewBox.cy}
135181
className="fill-foreground text-3xl font-bold"
136182
>
137-
{total.toLocaleString()}
183+
{formatLargeNumber(total)}
138184
</tspan>
139185
{unit && (
140186
<tspan

0 commit comments

Comments
 (0)