Skip to content

Commit 0c789ff

Browse files
committed
fix: Floats were not truncated properly for radialBar
Fix #174
1 parent d7063a5 commit 0c789ff

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

.devcontainer/ui-lovelace.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,11 @@ views:
438438
- entity: sensor.random0_100
439439
- entity: sensor.random_0_1000
440440
unit: Mbits/s
441+
float_precision: 4
441442
min: 0
442443
max: 1000
443444
group_by:
444-
func: max
445+
func: avg
445446
duration: 30min
446447
- type: custom:apexcharts-card
447448
chart_type: pie

src/apex-layouts.ts

+13
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,19 @@ function getPlotOptions_radialBar(config: ChartCardConfig) {
355355
track: {
356356
background: 'rgba(128, 128, 128, 0.2)',
357357
},
358+
dataLabels: {
359+
value: {
360+
formatter: function (value, opts, conf = config) {
361+
const index = opts?.config?.series?.findIndex((x) => {
362+
return parseFloat(value) === x;
363+
});
364+
if (index != -1) {
365+
return truncateFloat(value, conf.series_in_graph[index].float_precision) + '%';
366+
}
367+
return value;
368+
},
369+
},
370+
},
358371
};
359372
} else {
360373
return {};

src/utils.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,19 @@ export function is12Hour(locale: string): boolean {
234234
return !(new Date(2021, 1, 1, 15, 0, 0, 0).toLocaleTimeString(locale).indexOf('15') > -1);
235235
}
236236

237-
export function truncateFloat(value: number | null | undefined, precision: number | undefined): string | number | null {
237+
export function truncateFloat(
238+
value: string | number | null | undefined,
239+
precision: number | undefined,
240+
): string | number | null {
238241
let lValue: string | number | null | undefined = value;
239242
if (lValue === undefined) return null;
240-
if (value !== null && typeof value === 'number' && !Number.isInteger(value)) {
243+
if (typeof lValue === 'string') {
244+
lValue = parseFloat(lValue);
245+
if (Number.isNaN(lValue)) {
246+
return lValue;
247+
}
248+
}
249+
if (lValue !== null && typeof lValue === 'number' && !Number.isInteger(lValue)) {
241250
lValue = (lValue as number).toFixed(precision === undefined ? DEFAULT_FLOAT_PRECISION : precision);
242251
}
243252
return lValue;

0 commit comments

Comments
 (0)