blob: 1273207b8fce5d81ccd8874405505708d485c235 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
/*
* 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 real scaleFactor: 1 // to avoid float issues, set the range using ints and then apply this scale factor to the output
property int minLabels: 3
property int maxLabels: 8
property real valueDivisionsInterval: 0
property real startValueDivision: 0
property real valuesDelta
property bool labelK: true
onStartValueChanged: update()
onEndValueChanged: update()
onMinLabelsChanged: update()
onMaxLabelsChanged: update()
implicitWidth: childrenRect.width
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 = interval*Math.trunc(startValue/interval)
}
Repeater {
id: labelsRepeater
delegate: Label {
anchors.right: parent.right
property real value: root.startValueDivision + root.valueDivisionsInterval*index
text: (value*root.scaleFactor > 1000 & root.labelK) ? value*root.scaleFactor/1000 + "k" : value*root.scaleFactor //maybe this spaghetti could be better written but it's only run once on item load
font.pixelSize: Dims.w(5)
y: parent.height - (parent.height)*((value-root.startValue)/root.valuesDelta) - height/2
verticalAlignment: Text.AlignVCenter
}
}
}
|