aboutsummaryrefslogtreecommitdiff
path: root/src/graphs/VerticalLabels.qml
blob: 5cb796b59178d04f8f7cc542fc997316a95ca404 (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
/*
 * 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
        }
    }
}