Skip to content

Commit e74dd7d

Browse files
akashsonunekfenner
authored andcommitted
feat(charts/gauge): support custom value formatter
DEPRECATED: Input `SiChartGaugeComponent.labelFormatter` should no longer be used to format the value. Use `SiChartGaugeComponent.valueFormatter` instead.
1 parent 886b8b5 commit e74dd7d

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

projects/charts-ng/src/components/si-chart-gauge/si-chart-gauge.component.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ export class SiChartGaugeComponent extends SiChartComponent implements OnChanges
4141
/** @defaultValue false */
4242
readonly unitsOnSplit = input(false);
4343
/**
44-
* Custom formatter for axis labels and value.
44+
* Custom formatter for axis labels.
4545
* Takes precedence when specified, i.e. number of decimals, unit etc needs
4646
* to be set by the user using this formatter.
4747
*/
4848
readonly labelFormatter = input<(val: number) => string>();
49+
/**
50+
* Custom formatter for value.
51+
* Takes precedence when specified, i.e. number of decimals needs
52+
* to be set by the user using this formatter.
53+
*/
54+
readonly valueFormatter = input<(val: number) => string>();
4955
/**
5056
* Sets the number of decimals.
5157
* @deprecated Use `minNumberOfDecimals` and `maxNumberOfDecimals` instead.
@@ -332,7 +338,7 @@ export class SiChartGaugeComponent extends SiChartComponent implements OnChanges
332338
detail: {
333339
offsetCenter: [0, -0.5 * resp.valueFontSize],
334340
valueAnimation: true,
335-
formatter: this.valueFormatter(),
341+
formatter: this.valueFormatterInternal(),
336342
rich: {
337343
value: {
338344
fontSize: resp.valueFontSize,
@@ -357,20 +363,24 @@ export class SiChartGaugeComponent extends SiChartComponent implements OnChanges
357363
this.applyTitles();
358364
}
359365

360-
private valueFormatter(): (val: number) => string {
361-
return (
362-
this.labelFormatter() ??
363-
((value: number) => {
364-
const formattedValue = this.numberFormat().format(value);
365-
const unit = this.unit();
366-
const formattedUnit = unit
367-
? unit.length > 5
368-
? `\n{unit|${unit}}`
369-
: ` {unit|${unit}}`
370-
: '';
371-
return `{value|${formattedValue}}${formattedUnit}`;
372-
})
373-
);
366+
private valueFormatterInternal(): (val: number) => string {
367+
const unit = this.unit();
368+
const valueFormatter = this.valueFormatter();
369+
const labelFormatter = this.labelFormatter();
370+
371+
const formattedUnit = unit ? (unit.length > 5 ? `\n{unit|${unit}}` : ` {unit|${unit}}`) : '';
372+
373+
return (value: number): string => {
374+
if (valueFormatter) {
375+
return `{value|${valueFormatter(value)}}${formattedUnit}`;
376+
}
377+
// DEPRECATED: Use the new input `valueFormatter` to format the value.
378+
// labelFormatter should be removed from here in future versions.
379+
if (labelFormatter) {
380+
return labelFormatter(value);
381+
}
382+
return `{value|${this.numberFormat().format(value)}}${formattedUnit}`;
383+
};
374384
}
375385

376386
override ngOnChanges(changes: SimpleChanges): void {

0 commit comments

Comments
 (0)