DualPlotterGraph.qml 6.45 KB
Newer Older
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright 2019 Barry Strong <bstrong@softtechok.com>
 *
 * This file is part of System Monitor Plasmoid
 *
 * System Monitor Plasmoid 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.
 *
 * System Monitor Plasmoid 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 System Monitor Plasmoid.  If not, see <https://www.gnu.org/licenses/>.
 */

20
import QtQuick 2.0
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
21
22
23
24
25
26


Canvas {
    id: plotCanvas
    property var config: []
    property int plots: 0
bstrong5280's avatar
V2,0    
bstrong5280 committed
27
    property double devisor: 1024
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
28
29
30
31
32
33
34
    property var plotValues: []
    property bool showLabels: false

    QtObject {
        id: data
        property var graphValues: []
        property int numValues: 0
bstrong5280's avatar
bstrong5280 committed
35
        property int maxValues: 33
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
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
        property real segSize: width / (maxValues - 1 )
        property bool valuesChanged : false
        property var paths: []
        property bool smoothing: plasmoid.configuration.useSmoothing
        property bool reset: false
        onSmoothingChanged: resetPaths()
    }

    onPlotValuesChanged: updateValues()
    onConfigChanged: requestPaint()

    function updateValues () {
        data.valuesChanged = true
        requestPaint()
    }

    function resetPaths() {
        data.reset = true
    }

    Component {
        id: line
        PathLine {}
    }

    Component {
        id: curve
bstrong5280's avatar
V2,0    
bstrong5280 committed
63
        PathCubic {}
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    }

    Path {
        id: plot
    }

    onPaint: {
        var ctx
        var idx
        var idy
        var pointCnt
        var curXLoc
        var curYLoc
        var clr
        var pw
        var alpha
bstrong5280's avatar
V2,0    
bstrong5280 committed
80
        var xDiff
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
81
82
83

        function addPoint(xLoc, yLoc) {
            if (data.paths.length < pointCnt + 1) {
bstrong5280's avatar
V2,0    
bstrong5280 committed
84
85
86
                if (data.smoothing) {
                    data.paths[pointCnt] = curve.createObject(plotCanvas, {})
                } else {
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
87
                    data.paths[pointCnt] = line.createObject(plotCanvas, {})
bstrong5280's avatar
V2,0    
bstrong5280 committed
88
89
90
91
92
93
94
95
96
97
                }
            }
            if (data.smoothing) {
                data.paths[pointCnt].x = Math.round(xLoc)
                data.paths[pointCnt].y = Math.round(yLoc)
                if (pointCnt == 0) {
                    data.paths[pointCnt].control1X = plot.startX
                    data.paths[pointCnt].control1Y = plot.startY
                    data.paths[pointCnt].control2X = xLoc
                    data.paths[pointCnt].control2Y = yLoc
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
98
                } else {
bstrong5280's avatar
bstrong5280 committed
99
                    xDiff = ((data.paths[pointCnt - 1].x - xLoc) / 3)
bstrong5280's avatar
V2,0    
bstrong5280 committed
100
101
102
103
                    data.paths[pointCnt].control1X = data.paths[pointCnt - 1].x - xDiff
                    data.paths[pointCnt].control1Y = data.paths[pointCnt - 1].y
                    data.paths[pointCnt].control2X = xLoc + xDiff
                    data.paths[pointCnt].control2Y = yLoc
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
104
                }
bstrong5280's avatar
V2,0    
bstrong5280 committed
105
106
107
            } else {
                data.paths[pointCnt].x = Math.round(xLoc)
                data.paths[pointCnt].y = Math.round(yLoc)
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
108
109
110
            }
            pointCnt += 1
        }
bstrong5280's avatar
V2,0    
bstrong5280 committed
111
        
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
112
        function scaleit (v) {
bstrong5280's avatar
V2,0    
bstrong5280 committed
113
114
115
            if (v >= 1073741824) {
                v = v / 1073741824
                v = spaceit(v) + v.toFixed(2) + " GBs"
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
116
            } else {
bstrong5280's avatar
V2,0    
bstrong5280 committed
117
118
119
                if ( v >= 1048576 ) {
                    v = v / 1048576
                    v = spaceit(v) + v.toFixed(2) + " MBs"
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
120
                } else {
bstrong5280's avatar
V2,0    
bstrong5280 committed
121
122
123
124
                    if (v >= 1024 ) {
                        v = v / 1024
                        v = spaceit(v) + v.toFixed(2) + " KBs"
                    }
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
125
126
127
128
                }
            }
            return v
        }
129
        
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
        if (data.reset) {
            data.paths.length = 0
            data.reset = false
        }
        if (data.valuesChanged) {
            if (data.graphValues.length != plots) {
                for (idx = 0; idx < plots; idx++)
                    data.graphValues[idx] = []
            }
            if (data.numValues >= data.maxValues) {
                for (idx = 0; idx < plots; idx++)
                    data.graphValues[idx].shift()
                data.numValues -= 1
            }
            data.numValues += 1
            for (idx = 0; idx < plots; idx++)
                data.graphValues[idx].push(plotValues[idx + 1])
            data.valuesChanged = false
        }
        ctx = getContext("2d")
        ctx.clearRect(0, 0, width, Math.ceil(height))
        if (data.numValues > 1) {
bstrong5280's avatar
V2,0    
bstrong5280 committed
152
            devisor = 1024
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
153
154
155
156
157
158
159
160
161
            for (idy = 0; idy < plots; idy++) {
                for (idx = 0; idx < data.numValues; idx++) {
                    if (data.graphValues[idy][idx] > devisor)
                        devisor = data.graphValues[idy][idx]
                }
            }
            pw = 0
            while (Math.pow(2, pw) < devisor)
                pw += 1
162
            devisor = Math.pow(2, pw)
bstrong@softtechok.com's avatar
bstrong@softtechok.com committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
            plot.startX = width
            plot.startY = height
            for (idy = 0; idy < plots ; idy++) {
                clr = config[idy]
                alpha = .5
                if (idy === 0)
                    alpha = 1
                ctx.fillStyle = Qt.hsla(clr.hslHue, clr.hslSaturation, clr.hslLightness, alpha)
                curXLoc = width
                curYLoc = height
                pointCnt = 0
                for (idx = data.numValues - 1; idx >= 0; idx--) {
                    curYLoc = height - (data.graphValues[idy][idx] * height / devisor)
                    addPoint(curXLoc, curYLoc)
                    curXLoc -= data.segSize
                }
                plot.pathElements = data.paths
                ctx.path = plot
                ctx.lineTo(curXLoc + data.segSize, height)
                ctx.fill()
            }
        }
        if(showLabels) {
            ctx.save()
            ctx.font = Qt.application.font.pointSize + 'pt "' + Qt.application.font.family + '"'
            ctx.textAlign = "left"
            ctx.textAlign = "top"
            ctx.textBaseline = "top"
            ctx.fillStyle = theme.textColor
            ctx.fillText(scaleit(devisor), 0, 0)
            ctx.textAlign = "left"
            ctx.textAlign = "bottom"
            ctx.textBaseline = "bottom"
            ctx.fillText(plotValues[0], 0, height)
            ctx.restore()
        }
    }
}