Skip to content

Commit

Permalink
fix: Floats were not truncated properly for radialBar
Browse files Browse the repository at this point in the history
Fix #174
  • Loading branch information
RomRider committed Jul 12, 2021
1 parent d7063a5 commit 0c789ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/ui-lovelace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,11 @@ views:
- entity: sensor.random0_100
- entity: sensor.random_0_1000
unit: Mbits/s
float_precision: 4
min: 0
max: 1000
group_by:
func: max
func: avg
duration: 30min
- type: custom:apexcharts-card
chart_type: pie
Expand Down
13 changes: 13 additions & 0 deletions src/apex-layouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,19 @@ function getPlotOptions_radialBar(config: ChartCardConfig) {
track: {
background: 'rgba(128, 128, 128, 0.2)',
},
dataLabels: {
value: {
formatter: function (value, opts, conf = config) {
const index = opts?.config?.series?.findIndex((x) => {
return parseFloat(value) === x;
});
if (index != -1) {
return truncateFloat(value, conf.series_in_graph[index].float_precision) + '%';
}
return value;
},
},
},
};
} else {
return {};
Expand Down
13 changes: 11 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,19 @@ export function is12Hour(locale: string): boolean {
return !(new Date(2021, 1, 1, 15, 0, 0, 0).toLocaleTimeString(locale).indexOf('15') > -1);
}

export function truncateFloat(value: number | null | undefined, precision: number | undefined): string | number | null {
export function truncateFloat(
value: string | number | null | undefined,
precision: number | undefined,
): string | number | null {
let lValue: string | number | null | undefined = value;
if (lValue === undefined) return null;
if (value !== null && typeof value === 'number' && !Number.isInteger(value)) {
if (typeof lValue === 'string') {
lValue = parseFloat(lValue);
if (Number.isNaN(lValue)) {
return lValue;
}
}
if (lValue !== null && typeof lValue === 'number' && !Number.isInteger(lValue)) {
lValue = (lValue as number).toFixed(precision === undefined ? DEFAULT_FLOAT_PRECISION : precision);
}
return lValue;
Expand Down

0 comments on commit 0c789ff

Please sign in to comment.