How to Animate Charts in QML: Heart Rate and Pace Visualization


On this post I want to replicate an animation from this page, it uses SwiftUI. I use basic animations from QML, for instance PropertyAnimation and ParallelAnimation to animate properties such as color, height, y and x. Additionally I use Repeater to dynamically create any number of lines for chart. You can find the full project here. Below you can find the main files.

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
import QtQuick
import QtQuick.Controls.iOS
import QtQuick.Layouts

Window {
width: 640
height: 480
visible: true
title: qsTr("From Data to Visualization: Building Animated QML Charts")

property int iDuration: 400
property int interval: 60

property var chartData :
[{duration: iDuration, height: 20, y: 170, x: 10, newY: 170, fHeight: 20, paceY: 10, sHeight: 20}
, {duration: iDuration + interval, height: 25, y: 163, x: 40, newY: 163, fHeight: 25, paceY: 20, sHeight: 25 }
, {duration: iDuration + interval * 2, height: 20, y: 160, x: 70, x: 70, newY: 160, fHeight: 20, paceY: 20, sHeight: 20}
, {duration: iDuration + interval * 3, height: 20, y: 150, x: 100, newY: 150, fHeight: 20, paceY: 23, sHeight: 30}
, {duration: iDuration + interval * 4, height: 25, y: 140, x: 130, newY: 140, fHeight: 25, paceY: 23, sHeight: 35}
, {duration: iDuration + interval * 5, height: 20, y: 150, x: 160, newY: 150, fHeight: 20, paceY: 10, sHeight: 20}
, {duration: iDuration + interval * 6, height: 20, y: 150, x: 190, newY: 150, fHeight: 20, paceY: 20, sHeight: 45}
, {duration: iDuration + interval * 7, height: 100, y: 50, x: 220, newY: 50, fHeight: 100, paceY: 90, sHeight: 75}
, {duration: iDuration + interval * 8, height: 30, y: 40, x: 250, newY: 40, fHeight: 35, paceY: 50, sHeight: 85}
, {duration: iDuration + interval * 9, height: 30, y: 45, x: 280, newY: 75, fHeight: 50, paceY: 10, sHeight: 20}
, {duration: iDuration + interval * 10, height: 80, y: 100, x: 310, newY: 140, fHeight: 20, paceY: 10, sHeight: 25}
, {duration: iDuration + interval * 11, height: 20, y: 170, x: 340, newY: 120, fHeight: 20, paceY: 10, sHeight: 20}]

property int rowSpacing: 10
property int lineWidth: 20
property int graphWidth: lineWidth * chartData.length + rowSpacing * (chartData.length + 1)

ColumnLayout {
anchors.fill: parent
spacing: 10

Item {
Layout.fillWidth: true
Layout.fillHeight: true
}

Item {
height: 200

Rectangle {
id: chart
width: graphWidth
height: 200
color: "dodgerblue"
x: 640

Repeater {
id: repeater
model: chartData

RoundedLine {
required property var modelData
property alias line: line
property alias parallelAnim: parallelAnim
property alias paceAnim: paceAnim

id: line
recHeight: modelData.height
recY: modelData.y
recX: modelData.x

ParallelAnimation {
id: parallelAnim

PropertyAnimation {
target: line
property: "color"
to: "red"
duration: modelData.duration
}

PropertyAnimation {
target: line
property: "y"
easing.type: Easing.OutBounce
to: modelData.newY
duration: modelData.duration
}

PropertyAnimation {
target: line
property: "height"
easing.type: Easing.OutBounce
to: modelData.fHeight
duration: modelData.duration
}
}

ParallelAnimation {
id: paceAnim

PropertyAnimation {
target: line
property: "color"
to: "mediumturquoise"
duration: modelData.duration
}

PropertyAnimation {
target: line
property: "y"
easing.type: Easing.OutBounce
to: modelData.paceY
duration: modelData.duration
}

PropertyAnimation {
target: line
property: "height"
easing.type: Easing.OutBounce
to: modelData.sHeight
duration: modelData.duration
}
}
}
}

PropertyAnimation {
id: recAnim
target: chart
property: "x"
easing.type: Easing.Linear
to: 320 - graphWidth / 2
duration: 500
}
}
}

Button {
text: "Heart Rate"
Layout.alignment: Qt.AlignHCenter
onClicked: {
for (let i = 0; i < repeater.count; i++) {
let item = repeater.itemAt(i);
if (item) {
item.line.parallelAnim.start();
}
}
}
}

Button {
text: "Pace"
Layout.alignment: Qt.AlignHCenter
onClicked: {
for (let i = 0; i < repeater.count; i++) {
let item = repeater.itemAt(i);
if (item) {
item.line.paceAnim.start();
}
}
}
}

Button {
text: "Reset Animations"
Layout.alignment: Qt.AlignHCenter
onClicked: {
for (let i = 0; i < repeater.count; i++) {
let item = repeater.itemAt(i).line;
if (item) {
item.parallelAnim.stop();
item.paceAnim.stop()
item.color = "darkgrey";
item.height = chartData[i].height
item.y = chartData[i].y
}
}
}
}

Button {
text: "Show Chart"
Layout.alignment: Qt.AlignHCenter
onClicked: {
recAnim.start()
}
}

Item {
Layout.fillWidth: true
Layout.fillHeight: true
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import QtQuick

Rectangle {

property int recHeight: 20
property string recColor: "darkgrey"
property int recY: 0
property int recX: 0

width: 20
y: recY
x: recX
height: recHeight
color: recColor
bottomLeftRadius: 10
bottomRightRadius: 10
topLeftRadius: 10
topRightRadius: 10
}