From c92d8ffe9c983fc2186e99f44ddfdcb2caf5a810 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Thu, 30 Mar 2023 00:16:53 +0200 Subject: [PATCH] Optimize loading performance of the energy history charts --- .../ui/mainviews/energy/ConsumersHistory.qml | 45 ++++++++++++------- .../mainviews/energy/PowerBalanceHistory.qml | 27 +++++++++-- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/nymea-app/ui/mainviews/energy/ConsumersHistory.qml b/nymea-app/ui/mainviews/energy/ConsumersHistory.qml index c233cc98..70b84965 100644 --- a/nymea-app/ui/mainviews/energy/ConsumersHistory.qml +++ b/nymea-app/ui/mainviews/energy/ConsumersHistory.qml @@ -376,6 +376,8 @@ Item { property AreaSeries series: null property QHash valueCache: QHash {} + property LineSeries lowerSeries: null + property LineSeries upperSeries: null function calculateBaseValue(timestamp) { if (index > 0) { @@ -407,19 +409,25 @@ Item { // print("inserting entry for", thing.name, entry.timestamp) var baseValue = calculateBaseValue(entry.timestamp); - series.lowerSeries.insert(idx, entry.timestamp.getTime(), baseValue) - series.upperSeries.insert(idx, entry.timestamp.getTime(), baseValue + entry.currentPower) + lowerSeries.insert(idx, entry.timestamp.getTime(), baseValue) + upperSeries.insert(idx, entry.timestamp.getTime(), baseValue + entry.currentPower) } function addEntries(index, count) { // print("adding entries for", thing.name) - // Remove the leading 0-value entry - series.lowerSeries.removePoints(0, 1); - series.upperSeries.removePoints(0, 1); + series.lowerSeries = null; + series.upperSeries = null; var oldestTimestamp = null var newestTimestamp = null + + // Remove the leading 0-value entry + lowerSeries.removePoints(0, 1); + upperSeries.removePoints(0, 1); + + + for (var i = 0; i < count; i++) { var entry = logs.get(index + i) // print("got thing entry", thing.name, entry.timestamp, entry.currentPower, index + i) @@ -444,8 +452,11 @@ Item { zeroSeries.ensureValue(newestTimestamp) // Add the leading 0-value entry back - series.lowerSeries.insert(0, series.upperSeries.at(0).x, 0) - series.upperSeries.insert(0, series.upperSeries.at(0).x, 0) + lowerSeries.insert(0, upperSeries.at(0).x, 0) + upperSeries.insert(0, upperSeries.at(0).x, 0) + + series.upperSeries = upperSeries; + series.lowerSeries = lowerSeries; } readonly property ThingPowerLogs logs: ThingPowerLogs { @@ -462,15 +473,15 @@ Item { onEntriesRemoved: { // Remove the leading 0-value entry - consumerDelegate.series.lowerSeries.removePoints(0, 1); - consumerDelegate.series.upperSeries.removePoints(0, 1); + consumerDelegate.lowerSeries.removePoints(0, 1); + consumerDelegate.upperSeries.removePoints(0, 1); - consumerDelegate.series.lowerSeries.removePoints(index, count) - consumerDelegate.series.upperSeries.removePoints(index, count) + consumerDelegate.lowerSeries.removePoints(index, count) + consumerDelegate.upperSeries.removePoints(index, count) // Add the leading 0-value entry back - consumerDelegate.series.lowerSeries.insert(0, consumerDelegate.series.upperSeries.at(0).x, 0) - consumerDelegate.series.upperSeries.insert(0, consumerDelegate.series.upperSeries.at(0).x, 0) + consumerDelegate.lowerSeries.insert(0, consumerDelegate.series.upperSeries.at(0).x, 0) + consumerDelegate.upperSeries.insert(0, consumerDelegate.series.upperSeries.at(0).x, 0) zeroSeries.shrink() } @@ -493,8 +504,8 @@ Item { Component.onCompleted: { series = chartView.createSeries(ChartView.SeriesTypeArea, thing.name, dateTimeAxis, valueAxis) - series.lowerSeries = lineSeriesComponent.createObject(series) - series.upperSeries = lineSeriesComponent.createObject(series) + lowerSeries = lineSeriesComponent.createObject(series) + upperSeries = lineSeriesComponent.createObject(series) series.color = NymeaUtils.generateColor(Style.generationBaseColor, index) series.opacity = Qt.binding(function() { return d.selectedSeries == null || d.selectedSeries == series ? 1 : 0.3 @@ -503,8 +514,8 @@ Item { series.borderColor = series.color // Add a first point at 0 value - series.lowerSeries.insert(0, new Date().getTime(), 0) - series.upperSeries.insert(0, new Date().getTime(), 0) + lowerSeries.insert(0, new Date().getTime(), 0) + upperSeries.insert(0, new Date().getTime(), 0) } Component.onDestruction: { diff --git a/nymea-app/ui/mainviews/energy/PowerBalanceHistory.qml b/nymea-app/ui/mainviews/energy/PowerBalanceHistory.qml index 9ec8dbdd..a816f73d 100644 --- a/nymea-app/ui/mainviews/energy/PowerBalanceHistory.qml +++ b/nymea-app/ui/mainviews/energy/PowerBalanceHistory.qml @@ -82,14 +82,25 @@ Item { onEntriesAddedIdx: { // print("entries added", index, count) + selfProductionConsumptionSeries.upperSeries = null + selfProductionConsumptionSeries.lowerSeries = null + toStorageSeries.upperSeries = null + toStorageSeries.lowerSeries = null + fromStorageSeries.upperSeries = null + fromStorageSeries.lowerSeries = null + returnSeries.upperSeries = null + returnSeries.lowerSeries = null + acquisitionSeries.upperSeries = null + acquisitionSeries.lowerSeries = null + for (var i = 0; i < count; i++) { var entry = powerBalanceLogs.get(index + i) // print("got entry", entry.timestamp) zeroSeries.ensureValue(entry.timestamp) - // For debugging, to see if the other maths line up with the plain production graph productionSeries.insertEntry(index + i, entry) - consumptionSeries.insertEntry(index + i, entry) + // For debugging, to see if the other maths line up with the plain production graph +// consumptionSeries.insertEntry(index + i, entry) selfProductionConsumptionSeries.insertEntry(index + i, entry) toStorageSeries.insertEntry(index + i, entry) fromStorageSeries.insertEntry(index + i, entry) @@ -99,6 +110,16 @@ Item { d.now = entry.timestamp } } + selfProductionConsumptionSeries.upperSeries = selfProductionConsumptionUpperSeries + selfProductionConsumptionSeries.lowerSeries = zeroSeries + toStorageSeries.upperSeries = toStorageUpperSeries + toStorageSeries.lowerSeries = selfProductionConsumptionUpperSeries + fromStorageSeries.upperSeries = fromStorageUpperSeries + fromStorageSeries.lowerSeries = selfProductionConsumptionUpperSeries + returnSeries.upperSeries = returnUpperSeries + returnSeries.lowerSeries = toStorageUpperSeries + acquisitionSeries.upperSeries = acquisitionUpperSeries + acquisitionSeries.lowerSeries = fromStorageUpperSeries } onEntriesRemoved: { @@ -108,7 +129,7 @@ Item { toStorageUpperSeries.removePoints(index, count) selfProductionConsumptionUpperSeries.removePoints(index, count) productionSeries.removePoints(index, count) - consumptionSeries.removePoints(index, count) +// consumptionSeries.removePoints(index, count) zeroSeries.shrink() } }