summaryrefslogtreecommitdiff
path: root/src/graphs/VerticalLabels.qml
diff options
context:
space:
mode:
authordodoradio <dodoradio@outlook.com>2023-07-19 14:12:49 +0100
committerdodoradio <dodoradio@outlook.com>2023-07-20 19:17:44 +0100
commita4275a238f2a913923ee5b09edecba9c89ee4df1 (patch)
tree4956a412bbe0e395e3a7bd80ee0edf0fe2485055 /src/graphs/VerticalLabels.qml
parent19d485261dbb9f990935b4e677692207cf6422d3 (diff)
Rewrite the labelling system
The label code has been split into its own set of components: -TimeLabels: These try to display time labels as best as possible. They can label intervals from a minute to a day, but the code for labelling weeks is still missing. There's also no code protecting against strings overlapping with each other. - VerticalLabels: The code for these is a bit more 'analogue' than that of the time labels, but it's roughly analogous to previous code. These have also been implemented for the heartrate graph.
Diffstat (limited to '')
-rw-r--r--src/graphs/VerticalLabels.qml79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/graphs/VerticalLabels.qml b/src/graphs/VerticalLabels.qml
new file mode 100644
index 0000000..5cb796b
--- /dev/null
+++ b/src/graphs/VerticalLabels.qml
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2023 Arseniy Movshev <dodoradio@outlook.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+import QtQuick 2.15
+import org.asteroid.controls 1.0
+
+Item {
+ id: root
+ property real startValue: 0
+ property real endValue: 0
+ property int minLabels: 3
+ property int maxLabels: 8
+ property real valueDivisionsInterval: 0
+ property real startValueDivision: 0
+ property real valuesDelta
+ onStartValueChanged: update()
+ onEndValueChanged: update()
+ onMinLabelsChanged: update()
+ onMaxLabelsChanged: update()
+
+ function update() { // this tries to guess how to generate nice looking labels
+ valuesDelta = endValue - startValue
+ var powTen = Math.pow(10,Math.trunc(Math.log10(valuesDelta)))
+ var interval = powTen
+ var numLabel = Math.floor(valuesDelta/interval)
+ if (numLabel > maxLabels) {
+ interval = powTen*2
+ numLabel = valuesDelta/interval
+ if (numLabel > maxLabels) {
+ interval = powTen*5
+ numLabel = valuesDelta/interval
+ if (numLabel > maxLabels) {
+ interval = powTen*10
+ numLabel = valuesDelta/interval
+ }
+ }
+ }
+ if (numLabel < minLabels) {
+ interval = powTen/2
+ numLabel = valuesDelta/interval
+ if (numLabel < minLabels) {
+ interval = powTen/5
+ numLabel = valuesDelta/interval
+ if (numLabel < minLabels) {
+ interval = powTen/10
+ numLabel = valuesDelta/interval
+ }
+ }
+ }
+ valueDivisionsInterval = interval
+ labelsRepeater.model = Math.round(numLabel) + 1
+ startValueDivision = powTen*Math.trunc(startValue/powTen)
+ }
+ Repeater {
+ id: labelsRepeater
+ delegate: Label {
+ anchors.right: parent.right
+ property real value: root.startValueDivision + root.valueDivisionsInterval*index
+ text: value > 1000 ? value/1000 + "k" : value
+ font.pixelSize: Dims.w(5)
+ y: parent.height - (parent.height)*(value/root.valuesDelta) - height/2
+ verticalAlignment: Text.AlignVCenter
+ }
+ }
+}