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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the E-Bike demo project.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick 2.9
import QtCharts 2.2
import DataStore 1.0
import "./BikeStyle"
ChartView {
property var exampleTrips: [
[[0, 0], [15, 250], [30, 0], [30, 0], [25, 450], [20, 400], [10, 200], [10, 200], [20, 350], [20, 300], [10, 200], [10, 100], [10, 100], [10, 175], [10, 150], [15, 200], [15, 250], [10, 200], [15, 250], [0, 0]],
[[0, 0], [5, 100], [10, 200], [10, 200], [10, 150], [15, 250], [15, 275], [15, 200], [20, 350], [5, 50], [15, 200], [15, 250], [15, 225], [20, 300], [25, 425], [25, 400], [15, 200], [8, 125], [10, 175], [0, 0]],
[[0, 0], [20, 375], [15, 250], [15, 300], [15, 275], [0, 0], [10, 200], [10, 100], [10, 100], [15, 250], [10, 200], [10, 100], [10, 100], [8, 100], [25, 50], [15, 200], [15, 250], [10, 200], [8, 100], [0, 0]],
[[0, 0], [15, 300], [15, 200], [15, 250], [10, 450], [20, 375], [20, 350], [20, 250], [15, 200], [15, 225], [8, 150], [8, 100], [8, 125], [8, 100], [10, 150], [15, 200], [15, 250], [20, 300], [15, 250], [0, 0]]
]
property bool animationRunning: false
property int currentIndex
property var tripDetails
id: tripChart
antialiasing: true
backgroundColor: "transparent"
backgroundRoundness: 0
title: ""
legend {
markerShape: Legend.MarkerShapeCircle
reverseMarkers: true
labelColor: "#ffffff"
font {
family: "Montserrat, Regular"
weight: Font.Normal
pixelSize: UILayout.chartLegendTextSize
}
}
margins {
top: 0
bottom: 0
left: 0
right: 0
}
// X-axis is a timestamp value
DateTimeAxis {
id: axisX
format: Qt.locale("en_US").timeFormat(Locale.ShortFormat)
tickCount: 3
// No grid and no line
gridVisible: false
lineVisible: false
titleVisible: false
labelsColor: Colors.chartTimeLabel
labelsFont {
family: "Montserrat, Light"
weight: Font.Light
pixelSize: UILayout.chartTimeLabelSize
}
}
ValueAxis {
id: speedAxis
min: 0
max: 50
gridLineColor: Colors.chartGridLine
labelsColor: Colors.chartSpeed
labelsFont {
family: "Montserrat, Regular"
weight: Font.Normal
pixelSize: UILayout.chartSpeedLabelSize
}
titleVisible: false
lineVisible: false
labelFormat: "%.0f"
}
ValueAxis {
id: assistAxis
min: 0
max: 500
labelsColor: Colors.chartAssistpower
labelsFont {
family: "Montserrat, Regular"
weight: Font.Normal
pixelSize: UILayout.chartAssistpowerLabelSize
}
titleVisible: false
lineVisible: false
gridVisible: false
labelFormat: "%.0f"
}
SplineSeries {
id: assistSeries
name: qsTr("Pedal assist (W)")
axisX: axisX
axisYRight: assistAxis
pointsVisible: true
}
SplineSeries {
id: speedSeries
name: datastore.unit === DataStore.Kmh ? qsTr("Speed (km/h)") : qsTr("Speed (mph)")
axisX: axisX
axisY: speedAxis
pointsVisible: true
}
function updateTripGraph() {
if (currentIndex === 0 ) {
// Clear all current values (resets the graph)
speedSeries.removePoints(0, speedSeries.count);
assistSeries.removePoints(0, assistSeries.count);
} else if (currentIndex > 0) {
// Clear all current values
speedSeries.removePoints(0, speedSeries.count);
assistSeries.removePoints(0, assistSeries.count);
var seriesdata = exampleTrips[currentIndex % 4];
var now = tripDetails.starttime * 1000;
var duration = tripDetails.duration / seriesdata.length;
axisX.min = new Date(now - 60000);
for (var i = 0; i < seriesdata.length; i++) {
speedSeries.append(now, seriesdata[i][0]);
assistSeries.append(now, seriesdata[i][1]);
now += duration * 1000;
}
now -= duration * 1000;
axisX.max = new Date(now + 60000);
}
}
onTripDetailsChanged: updateTripGraph()
onCurrentIndexChanged: updateTripGraph()
// Make sure we have a proper value here
onAnimationRunningChanged: tripAnimationTimer.lastUpdate = new Date().getTime()
Timer {
id: tripAnimationTimer
property real lastUpdate: new Date().getTime()
property int currentIndex: 0
property var values: [[0, 0],
[10, 200],
[15, 250],
[15, 250],
[8, 150],
[5, 100],
[20, 400],
[20, 375],
[15, 275],
[15, 250],
[25, 450],
[15, 200],
[15, 200],
[10, 175],
[10, 150],
[5, 100],
[8, 125],
[10, 200],
[15, 250],
[0, 0]]
// Animate only if visible on screen
running: animationRunning && (swipeView.currentIndex === 0)
repeat: true
interval: 200
onTriggered: {
var now = new Date().getTime();
// Load a few initial numbers if empty
if (speedSeries.count === 0) {
speedSeries.append(now, values[0][0]);
assistSeries.append(now, values[0][1]);
speedSeries.append(now + 5000, values[1][0]);
assistSeries.append(now + 5000, values[1][1]);
speedSeries.append(now + 10000, values[2][0]);
assistSeries.append(now + 10000, values[2][1]);
speedSeries.append(now + 15000, values[3][0]);
assistSeries.append(now + 15000, values[3][1]);
currentIndex = 4;
}
if (now - lastUpdate > 5000) {
speedSeries.append(now + 15000, values[currentIndex][0]);
assistSeries.append(now + 15000, values[currentIndex][1]);
if (speedSeries.count > 17)
speedSeries.remove(0);
if (assistSeries.count > 17)
assistSeries.remove(0);
currentIndex += 1;
if (currentIndex == 20)
currentIndex = 0;
lastUpdate = now;
}
axisX.min = new Date(now - 50 * 1000);
axisX.max = new Date(now);
}
}
}
|