Skip to content

Commit

Permalink
fix: Display non inverted numbers in legend/tooltip/datalabels when `…
Browse files Browse the repository at this point in the history
…invert: true`

Fix RomRider#96
  • Loading branch information
RomRider committed Feb 23, 2021
1 parent 64f6a27 commit 331fe04
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/ui-lovelace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ views:
func: avg
fill: 'last'
show:
legend_value: false
legend_value: true
datalabels: true
- entity: sensor.random0_100
extend_to_end: false
Expand Down
19 changes: 15 additions & 4 deletions src/apex-layouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,12 @@ function getXTooltipFormatter(

function getYTooltipFormatter(config: ChartCardConfig, hass: HomeAssistant | undefined) {
return function (value, opts, conf = config, hass2 = hass) {
let lValue = value;
if (conf.series_in_graph[opts.seriesIndex]?.invert && lValue) {
lValue = -lValue;
}
if (!conf.series_in_graph[opts.seriesIndex]?.show.as_duration) {
value = truncateFloat(value, conf.series_in_graph[opts.seriesIndex].float_precision);
lValue = truncateFloat(lValue, conf.series_in_graph[opts.seriesIndex].float_precision);
}
const uom = computeUom(
opts.seriesIndex,
Expand All @@ -282,8 +286,8 @@ function getYTooltipFormatter(config: ChartCardConfig, hass: HomeAssistant | und
);
return conf.series_in_graph[opts.seriesIndex]?.show.as_duration
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
[`<strong>${prettyPrintTime(value, conf.series_in_graph[opts.seriesIndex].show.as_duration!)}</strong>`]
: [`<strong>${value} ${uom}</strong>`];
[`<strong>${prettyPrintTime(lValue, conf.series_in_graph[opts.seriesIndex].show.as_duration!)}</strong>`]
: [`<strong>${lValue} ${uom}</strong>`];
};
}

Expand All @@ -296,7 +300,11 @@ function getDataLabelsFormatter(config: ChartCardConfig) {
);
}
if (value === null) return;
return truncateFloat(value, conf.series_in_graph[opts.seriesIndex].float_precision);
let lValue = value;
if (conf.series_in_graph[opts.seriesIndex]?.invert && lValue) {
lValue = -lValue;
}
return truncateFloat(lValue, conf.series_in_graph[opts.seriesIndex].float_precision);
};
}

Expand Down Expand Up @@ -326,6 +334,9 @@ 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 (conf.series_in_graph[opts.seriesIndex]?.invert && value) {
value = -value;
}
if (!conf.series_in_graph[opts.seriesIndex]?.show.as_duration) {
value = truncateFloat(value, conf.series_in_graph[opts.seriesIndex].float_precision);
}
Expand Down

0 comments on commit 331fe04

Please sign in to comment.