-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
166 lines (147 loc) · 6.94 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
const apikey = "094a99116cb5eeaa1ceb6f345298f200";
const apiurl = "https://api.openweathermap.org/data/2.5/weather?&units=metric&q=";
function getWeatherData(city) {
const currurl = `${apiurl}${city}&appid=${apikey}`;
fetch(currurl)
.then(response => response.json())
.then(data => {
if (data.cod === 200) {
const temperature = data.main.temp;
const feelsLike = data.main.feels_like;
const humidity = data.main.humidity;
const pressure = data.main.pressure;
const windSpeed = data.wind.speed;
const weather = data.weather[0].main;
document.getElementById("currentWeather").innerHTML = `
<h3>Current Weather in ${city}</h3>
<p>Temperature: ${temperature} °C</p>
<p>Feels Like: ${feelsLike} °C</p>
<p>Humidity: ${humidity}%</p>
<p>Pressure: ${pressure} hPa</p>
<p>Wind Speed: ${windSpeed} m/s</p>
<p>Weather: ${weather}</p>
`;
const latitude = data.coord.lat;
const longitude = data.coord.lon;
const weeklyUrl = `https://api.openweathermap.org/data/2.5/forecast?lat=${latitude}&lon=${longitude}&units=metric&appid=${apikey}`;
fetch(weeklyUrl)
.then(response => response.json())
.then(weeklyData => {
if (weeklyData.cod === "200") {
let dates = [];
let temperatures = [];
let pressures = [];
let windSpeeds = [];
let feelsLike = [];
let humidities = [];
for (let i = 0; i < weeklyData.list.length; i++) {
const forecast = weeklyData.list[i];
const dateTime = forecast.dt_txt;
if (dateTime.includes("12:00:00")) {
const temperature = forecast.main.temp;
const pressure = forecast.main.pressure;
const windSpeed = forecast.wind.speed;
const feelsLikeTemp = forecast.main.feels_like;
const humidity = forecast.main.humidity;
const date = new Date(dateTime);
const dayOfWeek = date.toLocaleDateString('en-US', { weekday: 'short' });
dates.push(dayOfWeek);
temperatures.push(temperature);
pressures.push(pressure);
windSpeeds.push(windSpeed);
feelsLike.push(feelsLikeTemp);
humidities.push(humidity);
}
}
showCharts();
createChart('temperatureChart', dates, temperatures, 'Temperature (°C)', 'rgba(255, 99, 132, 1)', 'rgba(255, 99, 132, 0.2)');
createChart('pressureChart', dates, pressures, 'Pressure (hPa)', 'rgba(54, 162, 235, 1)', 'rgba(54, 162, 235, 0.2)');
createChart('windSpeedChart', dates, windSpeeds, 'Wind Speed (m/s)', 'rgba(255, 206, 86, 1)', 'rgba(255, 206, 86, 0.2)');
createChart('feelsLikeChart', dates, feelsLike, 'Feels Like (°C)', 'rgba(75, 192, 192, 1)', 'rgba(75, 192, 192, 0.2)');
createChart('humidityChart', dates, humidities, 'Humidity (%)', 'rgba(153, 102, 255, 1)', 'rgba(153, 102, 255, 0.2)');
} else {
document.getElementById("res").innerText = "Unable to retrieve forecast";
}
})
.catch(error => {
console.error("Error fetching forecast:", error);
document.getElementById("res").innerText = "Error fetching forecast";
});
} else {
document.getElementById("res").innerText = "City not found";
}
})
.catch(error => {
console.error("Error fetching weather data:", error);
document.getElementById("res").innerText = "Error fetching weather data";
});
}
function createChart(canvasId, labels, data, label, borderColor, backgroundColor) {
const ctx = document.getElementById(canvasId).getContext('2d');
if (window[canvasId] instanceof Chart) {
window[canvasId].destroy();
}
window[canvasId] = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: label,
data: data,
borderColor: borderColor,
backgroundColor: backgroundColor,
fill: true
}]
},
options: {
responsive: true,
scales: {
x: {
ticks: { color: '#ffffff' },
grid: { color: 'rgba(255, 255, 255, 0.1)' }
},
y: {
ticks: { color: '#ffffff' },
grid: { color: 'rgba(255, 255, 255, 0.1)' }
}
},
plugins: {
legend: {
labels: { color: '#ffffff' }
}
}
}
});
}
function showCharts() {
document.querySelectorAll('.chart-card').forEach(chart => {
chart.style.display = 'block';
});
}
function fetchWeatherByLocation(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
const locationUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&units=metric&appid=${apikey}`;
fetch(locationUrl)
.then(response => response.json())
.then(data => {
getWeatherData(data.name);
})
.catch(error => {
console.error("Error fetching weather data:", error);
document.getElementById("res").innerText = "Error fetching weather data";
});
}
// Get user's current location
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(fetchWeatherByLocation, (error) => {
console.error("Error getting location:", error);
document.getElementById("res").innerText = "Unable to retrieve location";
});
} else {
document.getElementById("res").innerText = "Geolocation is not supported by this browser.";
}
document.getElementById("curr-btn").addEventListener("click", () => {
const city = document.getElementById("cityname").value;
getWeatherData(city);
});