Changeset View
Changeset View
Standalone View
Standalone View
Modules/energy/package/contents/ui/Graph.qml
Show All 31 Lines | |||||
32 | Canvas | 32 | Canvas | ||
33 | { | 33 | { | ||
34 | width: 500 | 34 | width: 500 | ||
35 | height: 500 | 35 | height: 500 | ||
36 | id: canvas | 36 | id: canvas | ||
37 | antialiasing: true | 37 | antialiasing: true | ||
38 | 38 | | |||
39 | property int xPadding: 45 | 39 | property int xPadding: 45 | ||
40 | property int yPadding: 10 | 40 | property int yPadding: 40 | ||
41 | 41 | | |||
42 | property var data //expect an array of QPointF | 42 | property var data //expect an array of QPointF | ||
43 | 43 | | |||
44 | property real yMax: 100 | 44 | property real yMax: 100 | ||
45 | property real xMax: 100 | 45 | property real xMax: 100 | ||
46 | property real yMin: 0 | 46 | property real yMin: 0 | ||
47 | property real xMin: 0 | 47 | property real xMin: 0 | ||
48 | property real yStep: 20 | 48 | property real yStep: 20 | ||
49 | 49 | | |||
50 | property string yUnits: "" | 50 | property string yUnits: "" | ||
51 | property string xUnits: "" | 51 | property string xUnits: "" | ||
52 | 52 | | |||
53 | property real xDuration: 0 | ||||
54 | property real xDivisions: 0 | ||||
55 | property real xDivisionWidth: 0 | ||||
56 | property real xTicksAt: Graph.dontCare | ||||
57 | | ||||
53 | //internal | 58 | //internal | ||
54 | 59 | | |||
55 | property real plotWidth: width - xPadding | 60 | property real plotWidth: width - xPadding | ||
56 | property real plotHeight: height - yPadding *2 | 61 | property real plotHeight: height - yPadding *2 | ||
57 | 62 | | |||
58 | onDataChanged: { | 63 | onDataChanged: { | ||
59 | canvas.requestPaint(); | 64 | canvas.requestPaint(); | ||
60 | } | 65 | } | ||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Line(s) | 132 | for(var i = 0; i <= yMax; i += yStep) { | |||
129 | 134 | | |||
130 | c.fillText(i + canvas.yUnits, xPadding - 10, y); | 135 | c.fillText(i + canvas.yUnits, xPadding - 10, y); | ||
131 | 136 | | |||
132 | //grid line | 137 | //grid line | ||
133 | c.moveTo(xPadding,y) | 138 | c.moveTo(xPadding,y) | ||
134 | c.lineTo(canvas.width, y) | 139 | c.lineTo(canvas.width, y) | ||
135 | c.stroke() | 140 | c.stroke() | ||
136 | } | 141 | } | ||
142 | | ||||
143 | // Draw the X value texts | ||||
144 | c.textAlign = "center" | ||||
145 | c.lineWidth = 1; | ||||
146 | c.strokeStyle = 'rgba(0, 0, 0, 0.02)'; | ||||
147 | | ||||
148 | var xDivisions = xDuration / xDivisionWidth * 1000; | ||||
149 | var xTickPadding = (canvas.width - xPadding) / xDivisions; | ||||
150 | var xTickPos; | ||||
151 | var xTickDate; | ||||
152 | var xTickTime; | ||||
153 | | ||||
154 | var currentUnixTime = Date.now(); | ||||
155 | var currentDateTime = new Date(); | ||||
156 | var lastDate = ""; | ||||
157 | | ||||
158 | var hours = currentDateTime.getHours(); | ||||
159 | var minutes = currentDateTime.getMinutes(); | ||||
160 | var seconds = currentDateTime.getSeconds(); | ||||
161 | | ||||
162 | var diff = 0; | ||||
163 | | ||||
164 | if (xTicksAt == xTicksAtTwelveOClock) { | ||||
165 | diff = ((hours - 12) * 60 * 60 + minutes * 60 + seconds)*1000; // ms | ||||
166 | } | ||||
167 | | ||||
meven: A switch statement should be clearer here. | |||||
168 | if (xTicksAt == xTicksAtFullHour) { | ||||
169 | diff = (minutes * 60 + seconds)*1000; // ms | ||||
170 | } | ||||
171 | | ||||
172 | if (xTicksAt == xTicksAtHalfHour) { | ||||
173 | diff = ((minutes - 30) * 60 + seconds)*1000; // ms | ||||
174 | } | ||||
175 | | ||||
176 | var xCanvasWidth = (canvas.width - xPadding); | ||||
177 | var xTickOffset = plotWidth * (diff/1000) / xDuration; | ||||
178 | | ||||
179 | for (var i = xDivisions; i >= 0; i--) { | ||||
180 | xTickPos = i*xTickPadding + xPadding - xTickOffset; | ||||
181 | | ||||
182 | if ((xTickPos < canvas.width - 0.5*xPadding) && | ||||
183 | (xTickPos > 0.5*xPadding)) | ||||
184 | { | ||||
185 | currentDateTime = new Date(currentUnixTime - (xDivisions - i) * xDivisionWidth - diff); | ||||
186 | xTickDate = currentDateTime.toLocaleDateString(Qt.locale(), Locale.ShortFormat); | ||||
187 | xTickTime = currentDateTime.toLocaleTimeString(Qt.locale(), Locale.ShortFormat); | ||||
188 | | ||||
189 | if (((i+1) % 2 == 0) | ||||
190 | || (xDivisions < 10)) | ||||
191 | { | ||||
192 | // Display the time | ||||
193 | c.fillText(xTickTime, xTickPos, canvas.height - yPadding / 2); | ||||
194 | | ||||
195 | // If the date has changed, display it | ||||
196 | if (xTickDate != lastDate) { | ||||
197 | c.fillText(xTickDate, xTickPos, canvas.height - yPadding / 4); | ||||
198 | } | ||||
199 | | ||||
200 | // Tick markers | ||||
201 | c.moveTo(xTickPos, canvas.height - yPadding); | ||||
202 | c.lineTo(xTickPos, canvas.height - (yPadding*4) / 5); | ||||
203 | c.stroke(); | ||||
204 | } | ||||
205 | // Vertical grid line | ||||
206 | c.moveTo(xTickPos, yPadding) | ||||
207 | c.lineTo(xTickPos, canvas.height - yPadding) | ||||
208 | c.stroke() | ||||
209 | lastDate = xTickDate; | ||||
210 | } | ||||
211 | } | ||||
212 | | ||||
137 | } | 213 | } | ||
138 | } | 214 | } |
A switch statement should be clearer here.