Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMcInnes committed Jul 12, 2023
1 parent e9ca0ac commit 571a77d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
12 changes: 10 additions & 2 deletions components/ValueRange.qml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ QtObject {
property real value: NaN
readonly property real valueAsRatio: _valueAsRatio
property real maximumValue: NaN // if NaN, _max is dynamically adjusted to the maximum encountered value
readonly property real maximumSeen: _max
readonly property real maximumSeen: isNaN(maximumValue) ? _max : _actualMaximum
readonly property real minimumSeen: _min

property real _valueAsRatio: 0
property real _min: NaN
property real _max: isNaN(maximumValue) ? NaN : maximumValue
property real _actualMaximum: NaN

onValueChanged: {
// If value=NaN, or if only one value has been received, the min/max cannot yet be
Expand All @@ -29,7 +30,14 @@ QtObject {
return
}
_min = Math.min(_min, value)
_max = isNaN(maximumValue) ? Math.max(_max, value) : maximumValue

if (isNaN(maximumValue)) {
_max = Math.max(_max, value)
} else {
_max = maximumValue
_actualMaximum = Math.max(_actualMaximum, value)
}

if (!isNaN(_max) && value >= _max) {
_valueAsRatio = 1
return
Expand Down
44 changes: 22 additions & 22 deletions pages/BriefMonitorPanel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -216,42 +216,40 @@ exported power v 0.4 | /

// If we have ever seen power exported to the grid, the graph shows imported and exported power, as in Graph B.
// Otherwise, we only show imported power, as in Graph A.
readonly property bool graphShowsExportPower: inputsPower.minimumSeen && inputsPower.minimumSeen < 0
property bool graphShowsExportPower: false

// If we show export power, the minimum scale of the y axis goes from -1000W to +1000W
// If we don't show export power, the minimum scale of the y axis goes from 0W to +1000W
readonly property real minimumRangeWatts: graphShowsExportPower
? Theme.animation.loadGraph.minimumRange.watts * 2
: Theme.animation.loadGraph.minimumRange.watts

// This represents the difference in power between y=0 and y=1
readonly property real graphPowerRange: {
const peakPowerImportedOrExported = Math.max(Math.abs(inputsPower.maximumSeen), Math.abs(inputsPower.minimumSeen))
return graphShowsExportPower
? Math.max(2 * peakPowerImportedOrExported, minimumRangeWatts)
: Math.max(inputsPower.maximumSeen, minimumRangeWatts)
}
property real _oldGraphPowerRange: NaN

// in Graph A, when the graph is at y=0.0, power is zero.
// in Graph B, when the graph is at y=0.5, power is zero.
readonly property real normalizedZeroPowerPoint: graphShowsExportPower ? 0.5 : 0.0
function addNewValue() {
graphShowsExportPower = inputsPower.minimumSeen < 0

readonly property real normalizedPower: (inputsPower.value / graphPowerRange) + normalizedZeroPowerPoint
// If we show export power, the minimum scale of the y axis goes from -1000W to +1000W
// If we don't show export power, the minimum scale of the y axis goes from 0W to +1000W
const minimumRangeWatts = graphShowsExportPower
? Theme.animation.loadGraph.minimumRange.watts * 2
: Theme.animation.loadGraph.minimumRange.watts
const peakPowerImportedOrExported = Math.max(Math.abs(inputsPower.maximumSeen), Math.abs(inputsPower.minimumSeen))
const graphPowerRange = graphShowsExportPower // This represents the difference in power between y=0 and y=1 on the graph
? Math.max(2 * peakPowerImportedOrExported, minimumRangeWatts)
: Math.max(inputsPower.maximumSeen, minimumRangeWatts)

property real _oldGraphPowerRange: NaN
// in Graph A, when the graph is at y=0.0, power is zero.
// in Graph B, when the graph is at y=0.5, power is zero.
const normalizedZeroPowerPoint = graphShowsExportPower ? 0.5 : 0.0
const normalizedPower = (inputsPower.value / graphPowerRange) + normalizedZeroPowerPoint

function addNewValue() {
if (_oldGraphPowerRange != graphPowerRange) {
if (!isNaN(_oldGraphPowerRange)) {
const scalingFactor = graphPowerRange / _oldGraphPowerRange
scaleHistoricalData(scalingFactor)
scaleHistoricalData(scalingFactor, normalizedZeroPowerPoint)
}
_oldGraphPowerRange = graphPowerRange
}
addValue(normalizedPower)
normalizedPowerSlider.value = normalizedPower
}

function scaleHistoricalData(scalingFactor) {
function scaleHistoricalData(scalingFactor, normalizedZeroPowerPoint) {
// If our historical power data was like this: [-1000, 1000, -1000, 1000, ...], and inputsPower.minimumSeen === -1000,
// and inputsPower.maximumSeen === 1000, our graph 'y' values would be like this: [-1, 0, -1, 1, ...]
// If we then got a new power import reading of 5000W, we need to scale down all of the historical data by
Expand Down Expand Up @@ -312,6 +310,8 @@ exported power v 0.4 | /
}
}
Slider {
id: normalizedPowerSlider

enabled: false // not interactive
width: parent.width
height: Theme.geometry.briefPage.sidePanel.generator.slider.height
Expand Down
2 changes: 1 addition & 1 deletion src/veutil

0 comments on commit 571a77d

Please sign in to comment.