Fixes and improvements in the energy charts

This commit is contained in:
Michael Zanetti 2022-01-01 17:51:14 +01:00
parent 3d80a6f5bb
commit ea4a31ff72
10 changed files with 107 additions and 73 deletions

View File

@ -76,10 +76,6 @@ void EnergyLogs::setSampleRate(SampleRate sampleRate)
m_sampleRate = sampleRate;
emit sampleRateChanged();
beginResetModel();
qDeleteAll(m_list);
m_list.clear();
endResetModel();
fetchLogs();
}
}
@ -209,7 +205,14 @@ QVariantMap EnergyLogs::fetchParams() const
void EnergyLogs::getLogsResponse(int commandId, const QVariantMap &params)
{
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);
m_fetchingData = false;
@ -236,6 +239,13 @@ void EnergyLogs::fetchLogs()
return;
}
if (!m_list.isEmpty()) {
beginResetModel();
qDeleteAll(m_list);
m_list.clear();
endResetModel();
}
m_fetchingData = true;
fetchingDataChanged();
@ -248,7 +258,7 @@ void EnergyLogs::fetchLogs()
if (!m_endTime.isNull()) {
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");
}

View File

@ -45,12 +45,7 @@ StatsBase {
powerLogs.sampleRate = config.sampleRate
powerLogs.startTime = new Date(config.startTime().getTime() - config.sampleRate * 60000)
barSeries.clear();
barSeries.thingBarSetMap = ({})
valueAxis.max = 0
chartView.animationOptions = ChartView.SeriesAnimations
chartView.reset();
powerLogs.loadingInhibited = false
}
@ -66,6 +61,8 @@ StatsBase {
if (!fetchingData) {
var config = root.configs[selectionTabs.currentValue.config]
chartView.reset()
// First grouping log entries by timestamp
var groupedEntries = []
var groupedEntry = {}
@ -94,11 +91,6 @@ StatsBase {
groupedEntries.unshift(groupedEntry)
}
chartView.animationOptions = ChartView.NoAnimation
var labels = []
var entries = []
@ -171,19 +163,6 @@ StatsBase {
// print("assigning categories:", 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++) {
var entry = entries[i]
// print("Adding entry", JSON.stringify(entry))
@ -298,6 +277,22 @@ StatsBase {
legend.font: Style.extraSmallFont
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 {
id: labelsLayout
@ -387,7 +382,7 @@ StatsBase {
backgroundItem: chartView
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
x: Math.min(idx * mouseArea.width / categoryAxis.count, mouseArea.width - width)
@ -417,15 +412,42 @@ StatsBase {
}
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 {
Rectangle {
width: Style.extraSmallFont.pixelSize
height: width
color: index >= 0 ? root.colors[index % root.colors.length] : "white"
color: root.colors[model.indexInModel % root.colors.length]
}
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
}
}

View File

@ -268,7 +268,7 @@ Item {
height: parent.height
width: 1
color: Style.foregroundColor
x: mouseArea.mouseX
x: Math.min(mouseArea.width - 1, Math.max(0, mouseArea.mouseX))
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)
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 xOnLeft: mouseArea.mouseX - Style.smallMargins - width
property int xOnRight: Math.max(0, mouseArea.mouseX) + Style.smallMargins
property int xOnLeft: Math.min(mouseArea.width, mouseArea.mouseX) - Style.smallMargins - width
x: xOnRight + width < mouseArea.width ? xOnRight : xOnLeft
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)

View File

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

View File

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

View File

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

View File

@ -59,28 +59,7 @@ StatsBase {
powerBalanceLogs.startTime = new Date(config.startTime().getTime() - config.sampleRate * 60000)
powerBalanceLogs.loadingInhibited = false
barSeries.clear();
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
chartView.reset();
}
}
@ -106,6 +85,8 @@ StatsBase {
if (!fetchingData) {
chartView.animationOptions = ChartView.NoAnimation
chartView.reset();
// print("Logs fetched")
var config = root.configs[selectionTabs.currentValue.config]
@ -255,6 +236,27 @@ StatsBase {
margins.bottom: 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 {
id: labelsLayout
x: Style.smallMargins
@ -342,7 +344,7 @@ StatsBase {
backgroundItem: chartView
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
x: Math.min(idx * mouseArea.width / categoryAxis.count, mouseArea.width - width)

View File

@ -259,7 +259,7 @@ Item {
height: parent.height
width: 1
color: Style.foregroundColor
x: mouseArea.mouseX
x: Math.min(mouseArea.width - 1, Math.max(0, mouseArea.mouseX))
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 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 xOnLeft: mouseArea.mouseX - Style.smallMargins - width
property int xOnRight: Math.max(0, mouseArea.mouseX) + Style.smallMargins
property int xOnLeft: Math.min(mouseArea.mouseX, mouseArea.width) - Style.smallMargins - width
x: xOnRight + width < mouseArea.width ? xOnRight : xOnLeft
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)

View File

@ -251,7 +251,7 @@ Item {
height: parent.height
width: 1
color: Style.foregroundColor
x: mouseArea.mouseX
x: Math.min(mouseArea.width, Math.max(0, mouseArea.mouseX))
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)
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 xOnLeft: mouseArea.mouseX - Style.smallMargins - width
property int xOnRight: Math.max(0, mouseArea.mouseX) + Style.smallMargins
property int xOnLeft: Math.min(mouseArea.mouseX, mouseArea.width) - Style.smallMargins - width
x: xOnRight + width < mouseArea.width ? xOnRight : xOnLeft
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)

View File

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