Skip to content

Commit

Permalink
fix(graph): improve label displayment
Browse files Browse the repository at this point in the history
This also improve way to have second line value
  • Loading branch information
orblazer committed Dec 19, 2021
1 parent 2454aba commit f4b05d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
41 changes: 30 additions & 11 deletions package/contents/ui/components/SensorGraph.qml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ Item {
property string placement: plasmoid.configuration.placement // Values: top-right, top-left, bottom-right, bottom-left
property string displayment: plasmoid.configuration.displayment // Values: always, hover, hover-hints

readonly property real valueVisible: displayment !== 'hover-hints' || !mouseArea.containsMouse
property bool valueVisible: displayment !== 'hover' || !mouseArea.containsMouse

// Graph properties
readonly property string firstSensorUnits: sensorData.getUnits(sensors[0])
readonly property string secondSensorUnits: sensors.length > 1 ? sensorData.getUnits(sensors[1]) : ""
property var hideSensorIndexs: []

RMComponents.PlotterCanvas {
id: plotter
Expand Down Expand Up @@ -96,12 +97,17 @@ Item {
var list = []
for (var i = 0; i < sensors.length; i++) {
if (!sensors[i]) {
return
continue
}
if (!sensorData.isConnectedSource(sensors[i])) {
sensorData.connectSource(sensors[i])
}

// Hide wanted graph
if(hideSensorIndexs.indexOf(i) !== -1) {
continue
}

var item = plotDataComponent.createObject(plotter, {
color: plotter.colors[i % plotter.colors.length],
})
Expand All @@ -117,20 +123,25 @@ Item {
function onDataTick() {
plotter.initialize()

var values = new Array(plotter.sensors.length)
for (var i = 0; i < plotter.sensors.length; i++) {
values[i] = sensorData.getData(plotter.sensors[i])
}
plotter.addSample(values)
plotter.values = values
var values = new Array(plotter.sensors.length)
var graphValues = new Array(plotter.sensors.length - hideSensorIndexs.length)
for (var i = 0; i < plotter.sensors.length; i++) {
values[i] = sensorData.getData(plotter.sensors[i])

if (hideSensorIndexs.indexOf(i) === -1) {
graphValues[i] = values[i]
}
}
plotter.values = values
plotter.addSample(graphValues)
plotter.requestPaint()

// Update labels
if (valueVisible) {
firstLineLabel.text = formatLabel(values[0], sensorGraph.firstSensorUnits)
if (values.length > 1) {
secondLineLabel.text = formatLabel(values[1], sensorGraph.secondSensorUnits)
secondLineLabel.visible = (parseInt(values[1]) !== 0 || secondLabelWhenZero) ? true : false
secondLineLabel.visible = canSeeSecondLine()
}
}
}
Expand Down Expand Up @@ -237,24 +248,32 @@ Item {
}
}

function canSeeSecondLine() {
return parseInt(plotter.values[1]) !== 0 || secondLabelWhenZero
}

onDisplaymentChanged: {
switch (displayment) {
case 'always':
case 'hover-hints':
firstLineLabel.color = secondLineLabel.color = textColor

firstLineLabel.text = formatLabel(plotter.values[0], sensorGraph.firstSensorUnits)
firstLineLabel.visible = true
if (plotter.values.length > 1) {
secondLineLabel.text = formatLabel(plotter.values[1], sensorGraph.secondSensorUnits)
secondLineLabel.visible = true
secondLineLabel.visible = canSeeSecondLine()
} else if (!secondLineLabel.keepVisible) {
secondLineLabel.text = ''
secondLineLabel.visible = false
}

valueVisible = true
break

case 'hover':
firstLineLabel.visible = secondLineLabel.visible = false
valueVisible = mouseArea.containsMouse
break
}
}
Expand Down Expand Up @@ -294,7 +313,7 @@ Item {
firstLineLabel.text = formatLabel(plotter.values[0], sensorGraph.firstSensorUnits)
if (plotter.values.length > 1) {
secondLineLabel.text = formatLabel(plotter.values[1], sensorGraph.secondSensorUnits)
secondLineLabel.visible = plotter.values[1] !== 0 || secondLabelWhenZero
secondLineLabel.visible = canSeeSecondLine()
} else if (secondLineLabel.lastValue != 0 && secondLabelWhenZero) {
secondLineLabel.text = secondLineLabel.lastValue
secondLineLabel.visible = true
Expand Down
21 changes: 2 additions & 19 deletions package/contents/ui/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ Item {
RMComponents.SensorGraph {
id: cpuGraph
sampleSize: sampleCount
sensors: [sensorData.sensors.totalLoad]
sensors: [sensorData.sensors.totalLoad, sensorData.sensors.averageClock]
hideSensorIndexs: [1]
colors: [cpuColor]
defaultsMax: [100]

Expand All @@ -133,24 +134,6 @@ Item {
label: "CPU"
labelColor: cpuColor
secondLabel: showClock ? i18n("⏲ Clock") : ""

Connections {
property string units: sensorData.getUnits(sensorData.sensors.averageClock)
target: sensorData
enabled: showClock
function onDataTick() {
if (!sensorData.isConnectedSource(sensorData.sensors.averageClock)) {
sensorData.connectSource(sensorData.sensors.averageClock)
}

// Update labels
if (cpuGraph.valueVisible) {
cpuGraph.secondLineLabel.text = cpuGraph.secondLineLabel.lastValue = cpuGraph.formatLabel(
sensorData.getData(sensorData.sensors.averageClock), units)
cpuGraph.secondLineLabel.visible = true
}
}
}
}

RMComponents.SensorGraph {
Expand Down

0 comments on commit f4b05d7

Please sign in to comment.