Skip to content

Commit 6462247

Browse files
ranglangCaedman Ziwen Lan
andauthored
feat: add safeParseNumberOrTextWithSeparator utility function (#29)
* feat: add safeParseNumberOrTextWithSeparator utility function * chore: update version --------- Co-authored-by: Caedman Ziwen Lan <lanziwen@apitable.com>
1 parent 1ef5840 commit 6462247

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.6.0",
2+
"version": "1.9.0",
33
"description": "my apitable widget chart",
44
"engines": {
55
"node": ">=8.x"

src/model/echarts_pie.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ChartType, StackType } from './interface';
44
import { Strings, t } from '../i18n';
55
import { sortBy } from '../sortBy';
66
import { getNumberBaseFieldPrecision, maxRenderNum, processChartData, processRecords } from '../helper';
7-
import { safeParseNumberOrText } from '../utils';
7+
import { safeParseNumberOrText, safeParseNumberOrTextWithSeparator } from '../utils';
88

99
export class EchartsPie extends EchartsBase {
1010
type = ChartType.EchartsPie;
@@ -108,7 +108,7 @@ export class EchartsPie extends EchartsBase {
108108
// const totalContent = Math.round(params.value / (params.percent / 100));
109109
// console.log(totalContent, params.value / (params.percent / 100));
110110
// return `{a|${t(Strings.total)}}\n{b|${totalContent}}`;
111-
return `{a|${t(Strings.total)}}\n{b|${safeParseNumberOrText(dataSum, fieldPrecision)}}`;
111+
return `{a|${t(Strings.total)}}\n{b|${safeParseNumberOrTextWithSeparator(dataSum, fieldPrecision)}}`;
112112
},
113113
// formatter: () => {
114114
// const precision = guessNumberFieldPrecision(data.map(item => item.value).filter(Boolean));

src/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,22 @@ export const safeParseNumberOrText = (num : number | string | undefined, precisi
9090
return '';
9191
}
9292
return a.toFixed(precision);
93+
94+
95+
96+
export const safeParseNumberOrTextWithSeparator = (num : number | string | undefined, precision: number) => {
97+
if(!num) {
98+
return '';
99+
}
100+
101+
const a = Number(num);
102+
if(isNaN(a)) {
103+
return '';
104+
}
105+
106+
// Format with thousand separator and fixed precision
107+
return a.toLocaleString('en-US', {
108+
minimumFractionDigits: precision,
109+
maximumFractionDigits: precision
110+
});
93111
};

0 commit comments

Comments
 (0)