Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update statistics chart to respect entity display precision, fix precision bug in history chart #18334

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/components/chart/state-history-chart-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,10 @@ export class StateHistoryChartLine extends LitElement {
`${context.dataset.label}: ${formatNumber(
context.parsed.y,
this.hass.locale,
this.data[context.datasetIndex]?.entity_id
? getNumberFormatOptions(
this.hass.states[
this.data[context.datasetIndex].entity_id
],
this.hass.entities[
this.data[context.datasetIndex].entity_id
]
)
: undefined
getNumberFormatOptions(
undefined,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we pass the state?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this, but looking at what getNumberFormatOptions does with the state, I'm not sure it made sense to use it.

The only thing it seems to check is if the current state is an integer. Should the current state of an entity affect how all its historical states are rendered?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not, unless we assume the state was always an integer, which I dont think we can do.
More the step that might be useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you could combine the current step attribute, and the historical state value, merge them into a HassEntity, and pass that to the function.

this.hass.entities[this._entityIds[context.datasetIndex]]
)
)} ${this.unit}`,
},
},
Expand Down
12 changes: 11 additions & 1 deletion src/components/chart/statistics-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { isComponentLoaded } from "../../common/config/is_component_loaded";
import {
formatNumber,
numberFormatToLocale,
getNumberFormatOptions,
} from "../../common/number/format_number";
import {
getDisplayUnit,
Expand Down Expand Up @@ -74,6 +75,8 @@ export class StatisticsChart extends LitElement {

@state() private _chartData: ChartData = { datasets: [] };

@state() private _statisticIds: string[] = [];

@state() private _chartOptions?: ChartOptions;

@query("ha-chart-base") private _chart?: HaChartBase;
Expand Down Expand Up @@ -189,7 +192,11 @@ export class StatisticsChart extends LitElement {
label: (context) =>
`${context.dataset.label}: ${formatNumber(
context.parsed.y,
this.hass.locale
this.hass.locale,
getNumberFormatOptions(
undefined,
this.hass.entities[this._statisticIds[context.datasetIndex]]
)
)} ${
// @ts-ignore
context.dataset.unit || ""
Expand Down Expand Up @@ -248,6 +255,7 @@ export class StatisticsChart extends LitElement {
let colorIndex = 0;
const statisticsData = Object.entries(this.statisticsData);
const totalDataSets: ChartDataset<"line">[] = [];
const statisticIds: string[] = [];
let endTime: Date;

if (statisticsData.length === 0) {
Expand Down Expand Up @@ -386,6 +394,7 @@ export class StatisticsChart extends LitElement {
unit: meta?.unit_of_measurement,
band,
});
statisticIds.push(statistic_id);
}
});

Expand Down Expand Up @@ -427,6 +436,7 @@ export class StatisticsChart extends LitElement {
this._chartData = {
datasets: totalDataSets,
};
this._statisticIds = statisticIds;
}

static get styles(): CSSResultGroup {
Expand Down