-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
126 lines (107 loc) · 3.68 KB
/
Copy pathscript.js
File metadata and controls
126 lines (107 loc) · 3.68 KB
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
const weatherDisplay = document.querySelector("#forecastContainer");
const chartDisplay = document.querySelector("#myChartArea");
function getForecastData() {
const apiKey = "1b0d4e056d91c25a4fe8658fd55f3f06";
const cityInput = document.getElementById("cityInput");
const cityName = cityInput.value;
if (cityName === "") {
alert("Please enter a city name!");
return;
}
const url = `https://api.openweathermap.org/data/2.5/forecast?q=${cityName}&appid=${apiKey}&units=metric`;
axios
.get(url)
.then((response) => {
const forecastData = response.data;
console.log(response.data);
displayWeatherForecast(forecastData);
})
.catch((error) => {
console.log("Error:", error);
});
}
function displayWeatherForecast(data) {
clear(); // Prevent duplication in card when searching new city.
for (let i = 0; i < 6; i++) {
const weatherData = data.list[i];
const date = new Date(weatherData.dt_txt);
const day = date.toLocaleDateString(undefined, { weekday: 'long' });
const time = date.toLocaleTimeString("en-US", {
hour: "2-digit",
minute: "2-digit",
});
const div_card = document.createElement("div");
div_card.classList.add("col");
div_card.innerHTML = `
<div class="card shadow weather-card">
<div class="card-body text-center">
<h4 class="mb-3 sfw-normal">${time}</h4>
<p>Temperature: ${weatherData.main.temp} °C</p>
<p>Humidity: ${weatherData.main.humidity} %</p>
<p>Atmospheric Pressure: ${weatherData.main.pressure} hPa</p>
<p>
Weather Description: ${weatherData.weather[0].description}
</p>
</div>
</div>
`;
// Set background color based on weather description
const weatherDescription = weatherData.weather[0].description.toLowerCase();
if (weatherDescription.includes('clear')) {
div_card.querySelector('.card').classList.add('sunny');
} else if (weatherDescription.includes('cloud')) {
div_card.querySelector('.card').classList.add('cloudy');
} else if (weatherDescription.includes('rain')) {
div_card.querySelector('.card').classList.add('rainy');
} else if (weatherDescription.includes('snow')) {
div_card.querySelector('.card').classList.add('snowy');
} else if (weatherDescription.includes('storm')) {
div_card.querySelector('.card').classList.add('stormy');
}
document.querySelector("#forecastContainer").appendChild(div_card);
} // End loop for Hourly Forecast
// Throwing data inside chart
const ctx = document.getElementById('myChart');
let myChart =new Chart(ctx, {
type: 'bar',
data: {
labels: [],
datasets: [{
label: 'Temperature °C',
data: [],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true,
max:50
}
}
}
});
for (let k = 0; k < 5; k++) {
const options = { weekday: "short" };
const date = new Date();
const d = date.setDate(date.getDate() + k);
const longday = new Intl.DateTimeFormat("en-US", options).format(d);
console.log(longday);
const t=[];
myChart.data.labels.push(longday);
}
myChart.update();
for (let k = 0; k < 40; k=k+8) {
const t =data.list[k].main.temp;
console.log(data.list[k].dt_txt);
console.log(data.list[k].main.temp);
myChart.data.datasets[0].data.push(t)
}
myChart.update();
function purge(){
weatherDataContainer.innerHTML=``;
}
} // end display weather data
function clear() {
forecastContainer.innerHTML = "";
}