diff --git a/src/apex-layouts.ts b/src/apex-layouts.ts index 225d273..d6e35df 100644 --- a/src/apex-layouts.ts +++ b/src/apex-layouts.ts @@ -10,7 +10,7 @@ import { TIMESERIES_TYPES, } from './const'; import { ChartCardConfig } from './types'; -import { computeName, computeUom, is12Hour, mergeDeep, prettyPrintTime } from './utils'; +import { computeName, computeUom, is12Hour, mergeDeep, prettyPrintTime, truncateFloat } from './utils'; import { layoutMinimal } from './layouts/minimal'; import * as ca from 'apexcharts/dist/locales/ca.json'; import * as cs from 'apexcharts/dist/locales/cs.json'; @@ -248,17 +248,8 @@ function getXTooltipFormatter(config: ChartCardConfig, lang: string): ((val: num function getYTooltipFormatter(config: ChartCardConfig, hass: HomeAssistant | undefined) { return function (value, opts, conf = config, hass2 = hass) { - if ( - value !== null && - typeof value === 'number' && - !Number.isInteger(value) && - !conf.series_in_graph[opts.seriesIndex]?.show.as_duration - ) { - value = (value as number).toFixed( - conf.series_in_graph[opts.seriesIndex].float_precision === undefined - ? DEFAULT_FLOAT_PRECISION - : conf.series_in_graph[opts.seriesIndex].float_precision, - ); + if (!conf.series_in_graph[opts.seriesIndex]?.show.as_duration) { + value = truncateFloat(value, conf.series_in_graph[opts.seriesIndex].float_precision); } const uom = computeUom( opts.seriesIndex, @@ -276,18 +267,13 @@ function getYTooltipFormatter(config: ChartCardConfig, hass: HomeAssistant | und function getDataLabelsFormatter(config: ChartCardConfig) { return function (value, opts, conf = config) { if (conf.series_in_graph[opts.seriesIndex].show.datalabels === 'total') { - return opts.w.globals.stackedSeriesTotals[opts.dataPointIndex]; - } - if (value === null) return; - let lValue = value; - if (lValue !== null && typeof lValue === 'number' && !Number.isInteger(lValue)) { - lValue = (lValue as number).toFixed( - conf.series_in_graph[opts.seriesIndex].float_precision === undefined - ? DEFAULT_FLOAT_PRECISION - : conf.series_in_graph[opts.seriesIndex].float_precision, + return truncateFloat( + opts.w.globals.stackedSeriesTotals[opts.dataPointIndex], + conf.series_in_graph[opts.seriesIndex].float_precision, ); } - return lValue; + if (value === null) return; + return truncateFloat(value, conf.series_in_graph[opts.seriesIndex].float_precision); }; } @@ -317,17 +303,8 @@ function getLegendFormatter(config: ChartCardConfig, hass: HomeAssistant | undef let value = TIMESERIES_TYPES.includes(config.chart_type) ? opts.w.globals.series[opts.seriesIndex].slice(-1)[0] : opts.w.globals.series[opts.seriesIndex]; - if ( - value !== null && - typeof value === 'number' && - !Number.isInteger(value) && - !conf.series_in_graph[opts.seriesIndex]?.show.as_duration - ) { - value = (value as number).toFixed( - conf.series_in_graph[opts.seriesIndex].float_precision === undefined - ? DEFAULT_FLOAT_PRECISION - : conf.series_in_graph[opts.seriesIndex].float_precision, - ); + if (!conf.series_in_graph[opts.seriesIndex]?.show.as_duration) { + value = truncateFloat(value, conf.series_in_graph[opts.seriesIndex].float_precision); } const uom = computeUom( opts.seriesIndex, diff --git a/src/apexcharts-card.ts b/src/apexcharts-card.ts index c6fcafb..bb569c8 100644 --- a/src/apexcharts-card.ts +++ b/src/apexcharts-card.ts @@ -20,6 +20,7 @@ import { mergeDeepConfig, offsetData, prettyPrintTime, + truncateFloat, validateInterval, validateOffset, } from './utils'; @@ -722,15 +723,7 @@ class ChartsCard extends LitElement { } private _computeLastState(value: number | null, index: number): string | number | null { - if (value !== null && typeof value === 'number' && !Number.isInteger(value)) { - const precision = - this._config?.series[index].float_precision === undefined - ? DEFAULT_FLOAT_PRECISION - : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this._config.series[index].float_precision!; - return (value as number).toFixed(precision); - } - return value; + return truncateFloat(value, this._config?.series[index].float_precision); } /* diff --git a/src/utils.ts b/src/utils.ts index 2adedc4..954ab9e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,7 +4,7 @@ import { EntityCachePoints } from './types'; import { TinyColor } from '@ctrl/tinycolor'; import parse from 'parse-duration'; import { ChartCardExternalConfig, ChartCardPrettyTime, ChartCardSeriesExternalConfig } from './types-config'; -import { DEFAULT_MAX, DEFAULT_MIN, moment, NO_VALUE } from './const'; +import { DEFAULT_FLOAT_PRECISION, DEFAULT_MAX, DEFAULT_MIN, moment, NO_VALUE } from './const'; import { LovelaceConfig } from 'custom-card-helpers'; export function compress(data: unknown): string { @@ -235,3 +235,11 @@ export function mergeDeepConfig(target: any, source: any): any { 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, precision: number | undefined): string | number | null { + let lValue: string | number | null = value; + if (value !== null && typeof value === 'number' && !Number.isInteger(value)) { + lValue = (lValue as number).toFixed(precision === undefined ? DEFAULT_FLOAT_PRECISION : precision); + } + return lValue; +}