-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisulaize_cooker_static.html
120 lines (113 loc) · 3.37 KB
/
visulaize_cooker_static.html
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
<!DOCTYPE html>
<html>
<head>
<title>Data Chart</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
.heading {
margin-left: 460px;
margin-bottom: 20px;
}
body {
background: #2b5876;
/* fallback for old browsers */
background: -webkit-linear-gradient(to right, deepskyblue, lightblue);
/* Chrome 10-25, Safari 5.1-6 */
background: -webkit-gradient(linear, left top, right top, from(deepskyblue), to(lightblue));
background: -webkit-linear-gradient(left, deepskyblue, lightblue);
background: -o-linear-gradient(left, deepskyblue, lightblue);
background: linear-gradient(to right, deepskyblue, lightblue);
/* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
-webkit-box-shadow: 0 0 20px rgb(54, 54, 123);
box-shadow: 0 0 40px darkblue;
border-radius: 10px;
}
</style>
</head>
<body>
<div class="heading">
<h1>Analyzing Electric Cooker Power Consumption</h1>
</div>
<canvas id="myChart"></canvas>
<script>
function readThingSpeakData(channelId, readApiKey, results, callback) {
const url = `https://api.thingspeak.com/channels/2080598/feeds.json?api_key=RNBBORSUXTKRNS0B&results=1000 `;
var sum = 0;
fetch(url)
.then(response => response.json())
.then(data => {
if (data.feeds && data.feeds.length > 0) {
const entries = data.feeds.map(feed => ({
time: new Date(feed.created_at),
field1: parseFloat(feed.field1)
}));
callback(null, entries);
} else {
callback(new Error('No data found'));
}
})
.catch(error => {
callback(error);
});
}
const chartConfig = {
type: 'line',
data: {
datasets: [{
label: 'Field 1',
data: [],
borderColor: 'red',
fill: false
}, {
label: 'Field 2',
data: [],
borderColor: 'blue',
fill: false
}]
},
options: {
responsive: true,
scales: {
xAxes: [{
type: 'time',
time: {
displayFormats: {
hour: 'MMM D, hA'
}
}
}]
}
}
};
const chartData = {
labels: [],
datasets: [{
label: 'Current',
data: [],
borderColor: 'blue',
fill: false
}, {
label: 'Power Consumption In Watts',
data: [],
borderColor: 'red',
fill: false
}
]
};
readThingSpeakData('2080598', 'RNBBORSUXTKRNS0B', 100, (error, entries) => {
if (error) {
console.error(error);
} else {
chartData.labels = entries.map(entry => entry.time);
chartData.datasets[0].data = entries.map(entry => entry.field1);
chartData.datasets[1].data = entries.map(entry => entry.field1 * 230);
// chartData.datasets[1].data = entries.map(entry => entry.field2);
const ctx = document.getElementById('myChart').getContext('2d');
const chart = new Chart(ctx, chartConfig);
chart.data = chartData;
chart.update();
}
});
</script>
</body>
</html>