-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
272 lines (243 loc) · 7.11 KB
/
script.js
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
const apiKey = "your-api-key";
const locationForm = document.querySelector("#location-form");
const locationInput = document.querySelector("#location-input");
const currentWeather = document.querySelector(".current-weather");
const locationEl = document.querySelector(".location");
const temperatureEl = document.querySelector(".temperature");
const descriptionEl = document.querySelector(".description");
const iconEl = document.querySelector(".icon");
const forecastEl = document.querySelector(".forecast-data");
const wind = document.querySelector('.wind');
const tempfeels = document.querySelector('.tempfeels');
const pressure = document.querySelector('.pressure');
const humidity = document.querySelector('.humidity');
locationForm.addEventListener("submit", (event) => {
event.preventDefault();
const location = locationInput.value;
getWeather(location);
});
async function getWeather(location) {
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${location}&appid=${apiKey}&units=metric`);
const data = await response.json();
locationEl.textContent = `${data.name},${data.sys.country}`;
temperatureEl.textContent = `${Math.round(data.main.temp)}°C`;
descriptionEl.textContent = data.weather[0].description;
wind.textContent =`prędkość wiatru: ${data.wind.speed}`;
tempfeels.textContent=`temperatura odczuwana: ${data.main.feels_like}`;
pressure.textContent=`ciśnienie: ${data.main.pressure} hPa`;
humidity.textContent=`wilgotność: ${data.main.humidity}%`;
let currentWeatherEL=``;
const iconCod = data.weather[0].icon
const iconcodUrl = `https://openweathermap.org/img/w/${iconCod}.png`;
currentWeatherEL += `<img class="weather-icon" src="${iconcodUrl}">`;
currentWeather.innerHTML+=currentWeatherEL;
const forecastResponse = await fetch(`https://api.openweathermap.org/data/2.5/forecast?q=${location}&appid=${apiKey}&units=metric`);
const forecastData = await forecastResponse.json();
let forecastHtml = "";
let filteredForecastData = forecastData.list.filter((item) => {
return item.dt_txt.includes("12:00:00");
});
let labels = [];
let tempdata = [];
let winddata = [];
let tempfeeldata = [];
let pressuredata=[];
let humiditydata=[];
filteredForecastData.forEach((item) => {
const date = new Date(item.dt * 1000);
const dayOfWeek = date.toLocaleString("en-US", { weekday: "short" });
const temperature = `${Math.round(item.main.temp)}°C`;
const description = item.weather[0].description;
const iconCode = item.weather[0].icon;
const iconUrl = `https://openweathermap.org/img/w/${iconCode}.png`;
forecastHtml += `
<div class="forecast-item">
<div class="forecast-day">${dayOfWeek}</div>
<img class="forecast-icon" src="${iconUrl}">
<div class="forecast-temp">${temperature}</div>
<div class="forecast-desc">${description}</div>
</div>
`;
labels.push(date.toLocaleString("en-US", { weekday: "short" }) );
tempdata.push(Math.round(item.main.temp));
winddata.push(item.wind.speed);
tempfeeldata.push(Math.round(item.main.feels_like));
pressuredata.push(item.main.pressure);
humiditydata.push(item.main.humidity);
});
forecastEl.innerHTML = forecastHtml;
// Clear input field
locationInput.value = "";
//wykresy
const temperatureChart = document.querySelector("#temperature-chart");
const windChart = document.querySelector("#wind-chart");
const tempfeelsChart = document.querySelector("#tempfeels-chart");
const pressureChart= document.querySelector("#pressure-chart");
const humidityChart = document.querySelector("#humidity-chart");
const tempchart = new Chart(temperatureChart, {
type: "bar",
data: {
labels: labels,
datasets: [
{
label: "Temperatura",
data: tempdata,
fill: false,
backgroundColor: 'rgba(65, 105, 255, 0.2)',
},
],
},
options: {
responsive: true,
plugins: {
legend: {
position: "top",
},
title: {
display: true,
text: "Wyker temperatury w ciągu 5 dni",
},
},
scales: {
y: {
beginAtZero: true,
},
},
},
});
const windart = new Chart(windChart, {
type: "line",
data: {
labels: labels,
datasets: [
{
label: "prędkość wiatru",
data: winddata,
fill: false,
borderColor: "rgb(75, 192, 192)",
tension: 0.1,
},
],
},
options: {
responsive: true,
plugins: {
legend: {
position: "top",
},
title: {
display: true,
text: "prędkość wiatru w km/h",
},
},
scales: {
y: {
beginAtZero: true,
},
},
},
});
const tempfeelchart = new Chart(tempfeelsChart, {
type: "bar",
data: {
labels: labels,
datasets: [
{
label: "Temperatura",
data: tempdata,
fill: false,
backgroundColor: 'rgba(65, 105, 255, 0.5)',
},{
type: 'bar',
label: 'odczuwalna',
data:tempfeeldata,
backgroundColor: 'rgb(255, 99, 132, 0.5)',
}
],
},
options: {
responsive: true,
plugins: {
legend: {
position: "top",
},
title: {
display: true,
text: "Porównanie temperatury z temperaturą odczuwalną",
},
},
scales: {
y: {
beginAtZero: true,
},
},
},
});
const pressurechart = new Chart(pressureChart, {
type: "bar",
data: {
labels: labels,
datasets: [
{
label: "ciśnienie",
data: pressuredata,
fill: false,
backgroundColor: 'rgba(65, 105, 255, 0.4)',
},
],
},
options: {
responsive: true,
indexAxis: 'y',
plugins: {
legend: {
position: "top",
},
title: {
display: true,
text: "Wyker ciśnienia w ciągu 5 dni",
},
},
scales: {
x: {
beginAtZero: false,
suggestedMin: 900,
suggestedMax: 1100,
},
},
},
});
const humiditychart = new Chart(humidityChart, {
type: "line",
data: {
labels: labels,
datasets: [
{
label: "wilgotność",
data: humiditydata,
fill: false,
borderColor: "rgb(75, 192, 192)",
tension: 0.1,
},
],
},
options: {
responsive: true,
plugins: {
legend: {
position: "top",
},
title: {
display: true,
text: "% wilgotności w ciągu 5 dni",
},
},
scales: {
y: {
beginAtZero: false,
suggestedMin: 20,
},
},
},
});
}