Skip to content

Commit

Permalink
Support max_devices for energy-devices-detail-graph (#19936)
Browse files Browse the repository at this point in the history
* Support max_devices for energy-devices-detail-graph

* responsive ui editor
  • Loading branch information
karwosts authored Mar 4, 2024
1 parent 05c1328 commit 19d50b9
Showing 1 changed file with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ export class HuiEnergyDevicesDetailGraphCard
}

protected willUpdate(changedProps: PropertyValues) {
if (changedProps.has("_hiddenStats") && this._data) {
if (
(changedProps.has("_hiddenStats") || changedProps.has("_config")) &&
this._data
) {
this._processStatistics();
}
}
Expand Down Expand Up @@ -217,17 +220,17 @@ export class HuiEnergyDevicesDetailGraphCard
const datasets: ChartDataset<"bar", ScatterDataPoint[]>[] = [];
const datasetExtras: ChartDatasetExtra[] = [];

datasets.push(
...this._processDataSet(
const { data: processedData, dataExtras: processedDataExtras } =
this._processDataSet(
data,
energyData.statsMetadata,
energyData.prefs.device_consumption,
sorted_devices
)
);
);

datasets.push(...processedData);

const items = datasets.length;
datasetExtras.push(...Array<ChartDatasetExtra>(items).fill({}));
datasetExtras.push(...processedDataExtras);

if (compareData) {
// Add empty dataset to align the bars
Expand All @@ -247,18 +250,19 @@ export class HuiEnergyDevicesDetailGraphCard
show_legend: false,
});

datasets.push(
...this._processDataSet(
compareData,
energyData.statsMetadata,
energyData.prefs.device_consumption,
sorted_devices,
true
)
);
datasetExtras.push(
...Array<ChartDatasetExtra>(items).fill({ show_legend: false })
const {
data: processedCompareData,
dataExtras: processedCompareDataExtras,
} = this._processDataSet(
compareData,
energyData.statsMetadata,
energyData.prefs.device_consumption,
sorted_devices,
true
);

datasets.push(...processedCompareData);
datasetExtras.push(...processedCompareDataExtras);
}

this._start = energyData.start;
Expand All @@ -281,6 +285,7 @@ export class HuiEnergyDevicesDetailGraphCard
compare = false
) {
const data: ChartDataset<"bar", ScatterDataPoint[]>[] = [];
const dataExtras: ChartDatasetExtra[] = [];

devices.forEach((source, idx) => {
const color = getColorByIndex(idx);
Expand Down Expand Up @@ -317,23 +322,30 @@ export class HuiEnergyDevicesDetailGraphCard
}
}

const order = sorted_devices.indexOf(source.stat_consumption);
const itemExceedsMax = !!(
this._config?.max_devices && order >= this._config.max_devices
);

data.push({
label: getStatisticLabel(
this.hass,
source.stat_consumption,
statisticsMetaData[source.stat_consumption]
),
hidden: this._hiddenStats.has(source.stat_consumption),
hidden:
this._hiddenStats.has(source.stat_consumption) || itemExceedsMax,
borderColor: compare ? color + "7F" : color,
backgroundColor: compare ? color + "32" : color + "7F",
data: consumptionData,
order: 1 + sorted_devices.indexOf(source.stat_consumption),
order: 1 + order,
stack: "devices",
pointStyle: compare ? false : "circle",
xAxisID: compare ? "xAxisCompare" : undefined,
});
dataExtras.push({ show_legend: !compare && !itemExceedsMax });
});
return data;
return { data, dataExtras };
}

static get styles(): CSSResultGroup {
Expand Down

0 comments on commit 19d50b9

Please sign in to comment.