1
0
mirror of https://github.com/home-assistant/frontend.git synced 2025-12-19 18:28:42 +00:00

Add Y-axis label formatter to energy charts (#28298)

This commit is contained in:
Petar Petrov
2025-12-03 11:33:00 +02:00
committed by GitHub
parent 0447d87f18
commit 2c00889921

View File

@@ -56,6 +56,19 @@ export function getSuggestedPeriod(
return dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour"; return dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour";
} }
function createYAxisLabelFormatter(locale: FrontendLocaleData) {
let previousValue: number | undefined;
return (value: number): string => {
const maximumFractionDigits = Math.max(
1,
-Math.floor(Math.log10(Math.abs(value - (previousValue ?? value) || 1)))
);
previousValue = value;
return formatNumber(value, locale, { maximumFractionDigits });
};
}
export function getCommonOptions( export function getCommonOptions(
start: Date, start: Date,
end: Date, end: Date,
@@ -86,7 +99,7 @@ export function getCommonOptions(
align: "left", align: "left",
}, },
axisLabel: { axisLabel: {
formatter: (value: number) => formatNumber(Math.abs(value), locale), formatter: createYAxisLabelFormatter(locale),
}, },
splitLine: { splitLine: {
show: true, show: true,