Skip to content

Commit

Permalink
fix: truncate floats in total datalabels
Browse files Browse the repository at this point in the history
  • Loading branch information
RomRider committed Feb 11, 2021
1 parent 7ecfafd commit 917a20a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 43 deletions.
43 changes: 10 additions & 33 deletions src/apex-layouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -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);
};
}

Expand Down Expand Up @@ -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,
Expand Down
11 changes: 2 additions & 9 deletions src/apexcharts-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
mergeDeepConfig,
offsetData,
prettyPrintTime,
truncateFloat,
validateInterval,
validateOffset,
} from './utils';
Expand Down Expand Up @@ -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);
}

/*
Expand Down
10 changes: 9 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

0 comments on commit 917a20a

Please sign in to comment.