Merge PR #744: Fixes and improvements in the energy charts

This commit is contained in:
Jenkins nymea 2022-01-05 01:52:49 +01:00
commit 9d7325ca44
10 changed files with 107 additions and 73 deletions

View File

@ -76,10 +76,6 @@ void EnergyLogs::setSampleRate(SampleRate sampleRate)
m_sampleRate = sampleRate; m_sampleRate = sampleRate;
emit sampleRateChanged(); emit sampleRateChanged();
beginResetModel();
qDeleteAll(m_list);
m_list.clear();
endResetModel();
fetchLogs(); fetchLogs();
} }
} }
@ -209,7 +205,14 @@ QVariantMap EnergyLogs::fetchParams() const
void EnergyLogs::getLogsResponse(int commandId, const QVariantMap &params) void EnergyLogs::getLogsResponse(int commandId, const QVariantMap &params)
{ {
Q_UNUSED(commandId) Q_UNUSED(commandId)
// qCDebug(dcEnergyLogs()) << "Energy logs response:" << params; if (!m_list.isEmpty()) {
beginResetModel();
qDeleteAll(m_list);
m_list.clear();
endResetModel();
}
qCDebug(dcEnergyLogs()) << "Energy logs received";
// qCDebug(dcEnergyLogs()) << "Energy logs:" << params;
logEntriesReceived(params); logEntriesReceived(params);
m_fetchingData = false; m_fetchingData = false;
@ -236,6 +239,13 @@ void EnergyLogs::fetchLogs()
return; return;
} }
if (!m_list.isEmpty()) {
beginResetModel();
qDeleteAll(m_list);
m_list.clear();
endResetModel();
}
m_fetchingData = true; m_fetchingData = true;
fetchingDataChanged(); fetchingDataChanged();
@ -248,7 +258,7 @@ void EnergyLogs::fetchLogs()
if (!m_endTime.isNull()) { if (!m_endTime.isNull()) {
params.insert("to", m_endTime.toSecsSinceEpoch()); params.insert("to", m_endTime.toSecsSinceEpoch());
} }
qCDebug(dcEnergyLogs()) << "Fetching power balance logs" << params; qCDebug(dcEnergyLogs()) << "Fetching energy logs" << params;
m_engine->jsonRpcClient()->sendCommand("Energy.Get" + logsName(), params, this, "getLogsResponse"); m_engine->jsonRpcClient()->sendCommand("Energy.Get" + logsName(), params, this, "getLogsResponse");
} }

View File

@ -45,12 +45,7 @@ StatsBase {
powerLogs.sampleRate = config.sampleRate powerLogs.sampleRate = config.sampleRate
powerLogs.startTime = new Date(config.startTime().getTime() - config.sampleRate * 60000) powerLogs.startTime = new Date(config.startTime().getTime() - config.sampleRate * 60000)
barSeries.clear(); chartView.reset();
barSeries.thingBarSetMap = ({})
valueAxis.max = 0
chartView.animationOptions = ChartView.SeriesAnimations
powerLogs.loadingInhibited = false powerLogs.loadingInhibited = false
} }
@ -66,6 +61,8 @@ StatsBase {
if (!fetchingData) { if (!fetchingData) {
var config = root.configs[selectionTabs.currentValue.config] var config = root.configs[selectionTabs.currentValue.config]
chartView.reset()
// First grouping log entries by timestamp // First grouping log entries by timestamp
var groupedEntries = [] var groupedEntries = []
var groupedEntry = {} var groupedEntry = {}
@ -94,11 +91,6 @@ StatsBase {
groupedEntries.unshift(groupedEntry) groupedEntries.unshift(groupedEntry)
} }
chartView.animationOptions = ChartView.NoAnimation
var labels = [] var labels = []
var entries = [] var entries = []
@ -171,19 +163,6 @@ StatsBase {
// print("assigning categories:", labels) // print("assigning categories:", labels)
categoryAxis.timestamps = labels categoryAxis.timestamps = labels
var map = {}
for (var j = 0; j < consumers.count; j++) {
var consumer = consumers.get(j)
var barSet = barSeries.append(consumer.name, [])
barSet.color = root.colors[j % root.colors.length]
barSet.borderColor = barSet.color
barSet.borderWith = 0
map[consumer.id] = barSet
}
barSeries.thingBarSetMap = map
chartView.animationOptions = ChartView.SeriesAnimations
for (var i = 0; i < entries.length; i++) { for (var i = 0; i < entries.length; i++) {
var entry = entries[i] var entry = entries[i]
// print("Adding entry", JSON.stringify(entry)) // print("Adding entry", JSON.stringify(entry))
@ -298,6 +277,22 @@ StatsBase {
legend.font: Style.extraSmallFont legend.font: Style.extraSmallFont
legend.labelColor: Style.foregroundColor legend.labelColor: Style.foregroundColor
function reset() {
chartView.animationOptions = ChartView.NoAnimation
barSeries.clear();
valueAxis.max = 0
var map = {}
for (var j = 0; j < consumers.count; j++) {
var consumer = consumers.get(j)
var barSet = barSeries.append(consumer.name, [])
barSet.color = root.colors[j % root.colors.length]
barSet.borderColor = barSet.color
barSet.borderWith = 0
map[consumer.id] = barSet
}
barSeries.thingBarSetMap = map
chartView.animationOptions = ChartView.SeriesAnimations
}
Item { Item {
id: labelsLayout id: labelsLayout
@ -387,7 +382,7 @@ StatsBase {
backgroundItem: chartView backgroundItem: chartView
backgroundRect: Qt.rect(chartView.plotArea.x + toolTip.x, chartView.plotArea.y + toolTip.y, toolTip.width, toolTip.height) backgroundRect: Qt.rect(chartView.plotArea.x + toolTip.x, chartView.plotArea.y + toolTip.y, toolTip.width, toolTip.height)
property int idx: Math.floor(mouseArea.mouseX * categoryAxis.count / mouseArea.width) property int idx: Math.max(0, Math.min(categoryAxis.count -1, Math.floor(mouseArea.mouseX * categoryAxis.count / mouseArea.width)))
visible: mouseArea.containsMouse || mouseArea.preventStealing visible: mouseArea.containsMouse || mouseArea.preventStealing
x: Math.min(idx * mouseArea.width / categoryAxis.count, mouseArea.width - width) x: Math.min(idx * mouseArea.width / categoryAxis.count, mouseArea.width - width)
@ -417,15 +412,42 @@ StatsBase {
} }
Repeater { Repeater {
model: consumers model: ListModel {
id: toolTipModel
property var entries: {
var unsorted = []
for (var i = 0; i < consumers.count; i++) {
var consumer = consumers.get(i)
var entry = {
name: consumer.name,
value: barSeries.thingBarSetMap[consumer.id].at(toolTip.idx).toFixed(2),
indexInModel: i
}
unsorted.push(entry)
}
return unsorted
}
onEntriesChanged: {
clear();
var unsorted = entries;
for (var i = 0; i < unsorted.length; i++) {
var j = 0;
while (j < count && get(j).value > unsorted[i].value) {
j++;
}
insert(j, unsorted[i])
}
}
}
delegate: RowLayout { delegate: RowLayout {
Rectangle { Rectangle {
width: Style.extraSmallFont.pixelSize width: Style.extraSmallFont.pixelSize
height: width height: width
color: index >= 0 ? root.colors[index % root.colors.length] : "white" color: root.colors[model.indexInModel % root.colors.length]
} }
Label { Label {
text: barSeries.thingBarSetMap.hasOwnProperty(model.id) ? "%1: %2 kWh".arg(model.name).arg(barSeries.thingBarSetMap[model.id].at(toolTip.idx).toFixed(2)) : "" text: "%1: %2 kWh".arg(model.name).arg(model.value)
font: Style.extraSmallFont font: Style.extraSmallFont
} }
} }

View File

@ -268,7 +268,7 @@ Item {
height: parent.height height: parent.height
width: 1 width: 1
color: Style.foregroundColor color: Style.foregroundColor
x: mouseArea.mouseX x: Math.min(mouseArea.width - 1, Math.max(0, mouseArea.mouseX))
visible: mouseArea.containsMouse || mouseArea.preventStealing visible: mouseArea.containsMouse || mouseArea.preventStealing
} }
@ -280,10 +280,10 @@ Item {
backgroundRect: Qt.rect(mouseArea.x + toolTip.x, mouseArea.y + toolTip.y, toolTip.width, toolTip.height) backgroundRect: Qt.rect(mouseArea.x + toolTip.x, mouseArea.y + toolTip.y, toolTip.width, toolTip.height)
property int idx: consumptionUpperSeries.count - Math.floor(mouseArea.mouseX * consumptionUpperSeries.count / mouseArea.width) property int idx: consumptionUpperSeries.count - Math.floor(mouseArea.mouseX * consumptionUpperSeries.count / mouseArea.width)
property int seriesIndex: consumptionUpperSeries.count - idx property int seriesIndex: Math.min(consumptionUpperSeries.count - 1, Math.max(0, consumptionUpperSeries.count - idx))
property int xOnRight: mouseArea.mouseX + Style.smallMargins property int xOnRight: Math.max(0, mouseArea.mouseX) + Style.smallMargins
property int xOnLeft: mouseArea.mouseX - Style.smallMargins - width property int xOnLeft: Math.min(mouseArea.width, mouseArea.mouseX) - Style.smallMargins - width
x: xOnRight + width < mouseArea.width ? xOnRight : xOnLeft x: xOnRight + width < mouseArea.width ? xOnRight : xOnLeft
property double maxValue: consumptionUpperSeries.at(seriesIndex).y property double maxValue: consumptionUpperSeries.at(seriesIndex).y
y: Math.min(Math.max(mouseArea.height - (maxValue * mouseArea.height / valueAxis.max) - height - Style.margins, 0), mouseArea.height - height) y: Math.min(Math.max(mouseArea.height - (maxValue * mouseArea.height / valueAxis.max) - height - Style.margins, 0), mouseArea.height - height)

View File

@ -9,7 +9,7 @@ import Nymea 1.0
ChartView { ChartView {
id: root id: root
backgroundColor: "transparent" backgroundColor: "transparent"
animationOptions: ChartView.SeriesAnimations animationOptions: Qt.application.active ? ChartView.SeriesAnimations : ChartView.NoAnimation
title: qsTr("Consumers balance") title: qsTr("Consumers balance")
titleColor: Style.foregroundColor titleColor: Style.foregroundColor
legend.visible: false legend.visible: false

View File

@ -9,7 +9,7 @@ import Nymea 1.0
ChartView { ChartView {
id: consumptionPieChart id: consumptionPieChart
backgroundColor: "transparent" backgroundColor: "transparent"
animationOptions: ChartView.SeriesAnimations animationOptions: Qt.application.active ? ChartView.SeriesAnimations : ChartView.NoAnimation
title: qsTr("My energy mix") title: qsTr("My energy mix")
titleColor: Style.foregroundColor titleColor: Style.foregroundColor
legend.visible: false legend.visible: false

View File

@ -9,7 +9,7 @@ import Nymea 1.0
ChartView { ChartView {
id: productionPieChart id: productionPieChart
backgroundColor: "transparent" backgroundColor: "transparent"
animationOptions: ChartView.SeriesAnimations animationOptions: Qt.application.active ? ChartView.SeriesAnimations : ChartView.NoAnimation
title: qsTr("My energy production") title: qsTr("My energy production")
titleColor: Style.foregroundColor titleColor: Style.foregroundColor
legend.visible: false legend.visible: false

View File

@ -59,28 +59,7 @@ StatsBase {
powerBalanceLogs.startTime = new Date(config.startTime().getTime() - config.sampleRate * 60000) powerBalanceLogs.startTime = new Date(config.startTime().getTime() - config.sampleRate * 60000)
powerBalanceLogs.loadingInhibited = false powerBalanceLogs.loadingInhibited = false
barSeries.clear(); chartView.reset();
d.consumptionSet = barSeries.append(qsTr("Consumed"), [])
d.consumptionSet.color = Style.blue
d.consumptionSet.borderColor = d.consumptionSet.color
d.consumptionSet.borderWidth = 0
d.productionSet = barSeries.append(qsTr("Produced"), [])
d.productionSet.color = Style.green
d.productionSet.borderColor = d.productionSet.color
d.productionSet.borderWidth = 0
d.acquisitionSet = barSeries.append(qsTr("From grid"), [])
d.acquisitionSet.color = Style.red
d.acquisitionSet.borderColor = d.acquisitionSet.color
d.acquisitionSet.borderWidth = 0
d.returnSet = barSeries.append(qsTr("To grid"), [])
d.returnSet.color = Style.orange
d.returnSet.borderColor = d.returnSet.color
d.returnSet.borderWidth = 0
valueAxis.max = 0
} }
} }
@ -106,6 +85,8 @@ StatsBase {
if (!fetchingData) { if (!fetchingData) {
chartView.animationOptions = ChartView.NoAnimation chartView.animationOptions = ChartView.NoAnimation
chartView.reset();
// print("Logs fetched") // print("Logs fetched")
var config = root.configs[selectionTabs.currentValue.config] var config = root.configs[selectionTabs.currentValue.config]
@ -255,6 +236,27 @@ StatsBase {
margins.bottom: 0 margins.bottom: 0
margins.top: 0 margins.top: 0
function reset() {
barSeries.clear();
valueAxis.max = 0
d.consumptionSet = barSeries.append(qsTr("Consumed"), [])
d.consumptionSet.color = Style.blue
d.consumptionSet.borderColor = d.consumptionSet.color
d.consumptionSet.borderWidth = 0
d.productionSet = barSeries.append(qsTr("Produced"), [])
d.productionSet.color = Style.green
d.productionSet.borderColor = d.productionSet.color
d.productionSet.borderWidth = 0
d.acquisitionSet = barSeries.append(qsTr("From grid"), [])
d.acquisitionSet.color = Style.red
d.acquisitionSet.borderColor = d.acquisitionSet.color
d.acquisitionSet.borderWidth = 0
d.returnSet = barSeries.append(qsTr("To grid"), [])
d.returnSet.color = Style.orange
d.returnSet.borderColor = d.returnSet.color
d.returnSet.borderWidth = 0
}
Item { Item {
id: labelsLayout id: labelsLayout
x: Style.smallMargins x: Style.smallMargins
@ -342,7 +344,7 @@ StatsBase {
backgroundItem: chartView backgroundItem: chartView
backgroundRect: Qt.rect(chartView.plotArea.x + toolTip.x, chartView.plotArea.y + toolTip.y, toolTip.width, toolTip.height) backgroundRect: Qt.rect(chartView.plotArea.x + toolTip.x, chartView.plotArea.y + toolTip.y, toolTip.width, toolTip.height)
property int idx: Math.floor(mouseArea.mouseX * categoryAxis.count / mouseArea.width) property int idx: Math.min(Math.max(0,Math.floor(mouseArea.mouseX * categoryAxis.count / mouseArea.width)), categoryAxis.count - 1)
visible: mouseArea.containsMouse || mouseArea.preventStealing visible: mouseArea.containsMouse || mouseArea.preventStealing
x: Math.min(idx * mouseArea.width / categoryAxis.count, mouseArea.width - width) x: Math.min(idx * mouseArea.width / categoryAxis.count, mouseArea.width - width)

View File

@ -259,7 +259,7 @@ Item {
height: parent.height height: parent.height
width: 1 width: 1
color: Style.foregroundColor color: Style.foregroundColor
x: mouseArea.mouseX x: Math.min(mouseArea.width - 1, Math.max(0, mouseArea.mouseX))
visible: mouseArea.containsMouse || mouseArea.preventStealing visible: mouseArea.containsMouse || mouseArea.preventStealing
} }
@ -273,10 +273,10 @@ Item {
property int idx: consumptionUpperSeries.count - (Math.floor(mouseArea.mouseX * consumptionUpperSeries.count / mouseArea.width)) property int idx: consumptionUpperSeries.count - (Math.floor(mouseArea.mouseX * consumptionUpperSeries.count / mouseArea.width))
property int seriesIndex: consumptionUpperSeries.count - idx property int seriesIndex: Math.min(consumptionUpperSeries.count - 1, Math.max(0, consumptionUpperSeries.count - idx))
property int xOnRight: mouseArea.mouseX + Style.smallMargins property int xOnRight: Math.max(0, mouseArea.mouseX) + Style.smallMargins
property int xOnLeft: mouseArea.mouseX - Style.smallMargins - width property int xOnLeft: Math.min(mouseArea.mouseX, mouseArea.width) - Style.smallMargins - width
x: xOnRight + width < mouseArea.width ? xOnRight : xOnLeft x: xOnRight + width < mouseArea.width ? xOnRight : xOnLeft
property double maxValue: consumptionUpperSeries.at(seriesIndex).y property double maxValue: consumptionUpperSeries.at(seriesIndex).y
y: Math.min(Math.max(mouseArea.height - (maxValue * mouseArea.height / valueAxis.max) - height - Style.margins, 0), mouseArea.height - height) y: Math.min(Math.max(mouseArea.height - (maxValue * mouseArea.height / valueAxis.max) - height - Style.margins, 0), mouseArea.height - height)

View File

@ -251,7 +251,7 @@ Item {
height: parent.height height: parent.height
width: 1 width: 1
color: Style.foregroundColor color: Style.foregroundColor
x: mouseArea.mouseX x: Math.min(mouseArea.width, Math.max(0, mouseArea.mouseX))
visible: mouseArea.containsMouse || mouseArea.preventStealing visible: mouseArea.containsMouse || mouseArea.preventStealing
} }
@ -263,10 +263,10 @@ Item {
backgroundRect: Qt.rect(mouseArea.x + toolTip.x, mouseArea.y + toolTip.y, toolTip.width, toolTip.height) backgroundRect: Qt.rect(mouseArea.x + toolTip.x, mouseArea.y + toolTip.y, toolTip.width, toolTip.height)
property int idx: productionUpperSeries.count - Math.floor(mouseArea.mouseX * productionUpperSeries.count / mouseArea.width) property int idx: productionUpperSeries.count - Math.floor(mouseArea.mouseX * productionUpperSeries.count / mouseArea.width)
property int seriesIndex: productionUpperSeries.count - idx property int seriesIndex: Math.min(productionUpperSeries.count - 1, Math.max(0, productionUpperSeries.count - idx))
property int xOnRight: mouseArea.mouseX + Style.smallMargins property int xOnRight: Math.max(0, mouseArea.mouseX) + Style.smallMargins
property int xOnLeft: mouseArea.mouseX - Style.smallMargins - width property int xOnLeft: Math.min(mouseArea.mouseX, mouseArea.width) - Style.smallMargins - width
x: xOnRight + width < mouseArea.width ? xOnRight : xOnLeft x: xOnRight + width < mouseArea.width ? xOnRight : xOnLeft
property double maxValue: productionUpperSeries.at(seriesIndex).y property double maxValue: productionUpperSeries.at(seriesIndex).y
y: Math.min(Math.max(mouseArea.height - (maxValue * mouseArea.height / valueAxis.max) - height - Style.margins, 0), mouseArea.height - height) y: Math.min(Math.max(mouseArea.height - (maxValue * mouseArea.height / valueAxis.max) - height - Style.margins, 0), mouseArea.height - height)

View File

@ -101,7 +101,7 @@ Item {
return d return d
} }
function weekLabel(date) { function weekLabel(date) {
var yearStart = new Date(); var yearStart = new Date(date);
yearStart.setHours(0,0,0,0); yearStart.setHours(0,0,0,0);
yearStart.setDate(1); yearStart.setDate(1);
yearStart.setMonth(0); yearStart.setMonth(0);