@@ -63,6 +63,27 @@ const temperatureData = {
63
63
]
64
64
} ;
65
65
66
+ const locationData = {
67
+ labels : [ ] ,
68
+ datasets : [
69
+ {
70
+ label : 'Speed' ,
71
+ data : [ ] ,
72
+ borderColor : 'rgba(54, 162, 235, 1)' ,
73
+ backgroundColor : 'rgba(54, 162, 235, 0.2)' ,
74
+ yAxisID : 'y' ,
75
+ } ,
76
+ {
77
+ label : 'Heading' ,
78
+ data : [ ] ,
79
+ borderColor : 'rgba(255, 99, 132, 1)' ,
80
+ backgroundColor : 'rgba(255, 99, 132, 0.2)' ,
81
+ yAxisID : 'y1' ,
82
+ }
83
+ ]
84
+ } ;
85
+
86
+
66
87
const temperatureConfig = {
67
88
type : 'line' ,
68
89
data : temperatureData ,
@@ -107,7 +128,70 @@ const temperatureConfig = {
107
128
} ,
108
129
} ;
109
130
131
+
132
+ const locationConfig = {
133
+ type : 'line' ,
134
+ data : locationData ,
135
+ options : {
136
+ responsive : true ,
137
+ interaction : {
138
+ mode : 'index' ,
139
+ intersect : false ,
140
+ } ,
141
+ stacked : false ,
142
+ plugins : {
143
+ title : {
144
+ display : true ,
145
+ text : 'Location'
146
+ }
147
+ } ,
148
+ scales : {
149
+ y : {
150
+ type : 'linear' ,
151
+ display : true ,
152
+ position : 'left' ,
153
+ title : {
154
+ display :true ,
155
+ text : 'Speed (m/s)'
156
+ }
157
+ } ,
158
+ y1 : {
159
+ type : 'linear' ,
160
+ display : true ,
161
+ position : 'right' ,
162
+ title : {
163
+ display :true ,
164
+ text : 'Heading (deg)'
165
+ } ,
166
+
167
+ // grid line settings
168
+ grid : {
169
+ drawOnChartArea : false , // only want the grid lines for one axis to show up
170
+ } ,
171
+ } ,
172
+ }
173
+ } ,
174
+ } ;
175
+
110
176
const temperatureChart = new Chart ( 'temperatureChart' , temperatureConfig ) ;
177
+ const locationChart = new Chart ( 'locationChart' , locationConfig ) ;
178
+
179
+ // Chart update functions: https://www.chartjs.org/docs/latest/developers/updates.html
180
+ function addData ( chart , label , data ) {
181
+ chart . data . labels . push ( label ) ;
182
+ chart . data . datasets . forEach ( ( dataset , i ) => {
183
+ dataset . data . push ( data [ i ] ) ;
184
+ } ) ;
185
+ chart . update ( ) ;
186
+ }
187
+
188
+ function removeData ( chart ) {
189
+ chart . data . labels . pop ( ) ;
190
+ chart . data . datasets . forEach ( ( dataset ) => {
191
+ dataset . data . pop ( ) ;
192
+ } ) ;
193
+ chart . update ( ) ;
194
+ }
111
195
112
196
function locationSuccess ( position ) {
113
197
const c = position . coords ;
@@ -124,6 +208,8 @@ function locationSuccess(position) {
124
208
locationCircle . setRadius ( c . accuracy ) ;
125
209
126
210
npositions = npositions + 1 ;
211
+ addData ( locationChart , npositions , [ c . speed , c . heading ] )
212
+
127
213
}
128
214
129
215
function locationError ( error ) {
@@ -197,12 +283,8 @@ function onMessageArrived(message) {
197
283
198
284
let data = JSON . parse ( message . payloadString ) ;
199
285
200
- temperatureConfig . data . labels . push ( nmsg ) ;
201
- temperatureConfig . data . datasets [ 0 ] . data . push ( data . internal_temp ) ;
202
- temperatureConfig . data . datasets [ 1 ] . data . push ( data . external_temp ) ;
203
- temperatureConfig . data . datasets [ 2 ] . data . push ( data . relative_humidity ) ;
204
- temperatureChart . update ( ) ;
205
-
286
+ addData ( temperatureChart , nmsg , [ data . internal_temp , data . external_temp , data . relative_humidity ] )
287
+
206
288
nmsg += 1 ;
207
289
208
290
}
0 commit comments