Skip to content

Commit d49119b

Browse files
Keith BannisterKeith Bannister
authored andcommitted
Add chart
1 parent 4ddd5fd commit d49119b

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

ekayak.js

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,89 @@ L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
3434
var breadCrumbLine = L.polyline([]).addTo(map);
3535
var locationCircle = L.circle([0,0]).addTo(map);
3636

37+
38+
// Create charts
39+
const temperatureData = {
40+
labels: [],
41+
datasets: [
42+
{
43+
label: 'Internal Temp',
44+
data: [],
45+
borderColor: 'rgba(54, 162, 235, 1)',
46+
backgroundColor: 'rgba(54, 162, 235, 0.2)',
47+
yAxisID: 'y',
48+
},
49+
{
50+
label: 'External Temp',
51+
data: [],
52+
borderColor: 'rgba(0, 99, 182, 1)',
53+
backgroundColor: 'rgba(0, 99, 182, 0.2)',
54+
yAxisID: 'y',
55+
},
56+
{
57+
label: 'Humidity',
58+
data: [],
59+
borderColor: 'rgba(255, 99, 132, 1)',
60+
backgroundColor: 'rgba(255, 99, 132, 0.2)',
61+
yAxisID: 'y1',
62+
}
63+
]
64+
};
65+
66+
const temperatureConfig = {
67+
type: 'line',
68+
data: temperatureData,
69+
options: {
70+
responsive: true,
71+
interaction: {
72+
mode: 'index',
73+
intersect: false,
74+
},
75+
stacked: false,
76+
plugins: {
77+
title: {
78+
display: true,
79+
text: 'Temperature & humidity'
80+
}
81+
},
82+
scales: {
83+
y: {
84+
type: 'linear',
85+
display: true,
86+
position: 'left',
87+
title: {
88+
display:true,
89+
text: 'Temperature (deg)'
90+
}
91+
},
92+
y1: {
93+
type: 'linear',
94+
display: true,
95+
position: 'right',
96+
title: {
97+
display:true,
98+
text: 'Relative humidity (%)'
99+
},
100+
101+
// grid line settings
102+
grid: {
103+
drawOnChartArea: false, // only want the grid lines for one axis to show up
104+
},
105+
},
106+
}
107+
},
108+
};
109+
110+
const temperatureChart = new Chart('temperatureChart', temperatureConfig);
111+
37112
function locationSuccess(position) {
38113
const c = position.coords;
39114
locationText.textContent = `lat/long:${c.latitude}/${c.longitude} speed:${c.speed} heading:${c.heading} altitude:${c.altitude} accuracy:${c.accuracy} altaccuracy:${c.altitudeAccuracy} timestamp:${position.timestamp}`;
40115
const latlng = [c.latitude, c.longitude];
41116
if (npositions == 0) {
42-
console.log("Flying to", zoomLevel, latlng);
43117
//map.flyTo(latlng, Number(zoomLevel), {animate:true}); // Flying takes too long and stops when the next location comes in
44118
map.setView(latlng, zoomLevel);
45-
46119
} else {
47-
console.log("Panning to", latlng);
48120
map.panTo(latlng);
49121
}
50122
breadCrumbLine.addLatLng(latlng);
@@ -111,6 +183,7 @@ function onConnectionLost(responseObject) {
111183
}
112184

113185
// called when a message arrives
186+
let nmsg = 0;
114187
function onMessageArrived(message) {
115188
console.log("onMessageArrived:",message.payloadString);
116189
console.log(message)
@@ -122,4 +195,14 @@ function onMessageArrived(message) {
122195
let out = document.getElementById("lastmessage");
123196
out.textContent = message.payloadString;
124197

198+
let data = JSON.parse(message.payloadString);
199+
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+
206+
nmsg += 1;
207+
125208
}

index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ <h1> last MQTT message</h1>
2727
<h1>Location</h1>
2828
<div id="location"></div>
2929
<div id="map"></div>
30-
30+
<canvas id="temperatureChart" width="100%" height="30"></canvas>
3131

3232
</body>
3333

@@ -41,6 +41,8 @@ <h1>Location</h1>
4141
integrity="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ=="
4242
crossorigin=""></script>
4343

44+
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
45+
4446
<script src="ekayak.js"></script>
4547

4648

0 commit comments

Comments
 (0)