-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart.html
More file actions
183 lines (164 loc) · 6.33 KB
/
chart.html
File metadata and controls
183 lines (164 loc) · 6.33 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Charts</title>
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0"/>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #1b1a52;
color: #fff;
}
.location {
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
margin-bottom: 20px;
}
.search-bar {
width: 300px;
padding: 10px;
border-radius: 5px;
border: 1px solid #ccc;
font-size: 1rem;
}
.btn {
padding: 10px 20px;
font-size: 1rem;
color: white;
background-color: #1a73e8;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.btn:hover {
background-color: #1558b0;
}
select {
padding: 10px;
font-size: 1rem;
border-radius: 5px;
margin-left: 10px;
}
.chart-container {
max-width: 600px;
margin: 20px auto;
background: #fff;
border-radius: 12px;
padding: 20px;
box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1);
}
.back-button {
position: absolute;
top: 20px;
left: 20px;
background-color: #1a73e8;
color: white;
padding: 10px 15px;
border-radius: 50%;
font-size: 1.5rem;
cursor: pointer;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
transition: background-color 0.3s;
}
.back-button:hover {
background-color: #1558b0;
}
</style>
</head>
<body>
<a href="index.html" class="back-button">
<span class="material-symbols-outlined">arrow_back</span>
</a>
<h1>Weather Data</h1>
<div class="location">
<input type="text" class="search-bar" id="location" placeholder="Search for location">
<button type="button" class="btn" id="search" onclick="fetchInfo()">Search</button>
<select id="graph">
<option value="wind">Wind Speed</option>
<option value="temperature">Temperature</option>
<option value="humidity">Humidity</option>
</select>
</div>
<div class="chart-container">
<canvas id="chart" width="400" height="400"></canvas>
</div>
<script>
function fetchInfo() {
const location = document.getElementById("location").value;
const apiKeyWeather = '1992acd757593192e5b6ed69c774d2f2';
if (location) {
const locationURL = `https://api.openweathermap.org/geo/1.0/direct?q=${location}&appid=${apiKeyWeather}`;
$.ajax({
method: "GET",
url: locationURL,
}).done(function (locationData) {
if (locationData && locationData.length > 0) {
const lat = locationData[0].lat;
const lon = locationData[0].lon;
// Fetch weather and pollution data using the retrieved coordinates
const weatherURL = `https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&appid=${apiKeyWeather}`;
$.ajax({ method: "GET", url: weatherURL })
.done(function (weatherData) {
updateChart(weatherData);
}).fail(function (jqXHR, textStatus) {
alert(`Failed to retrieve weather data: ${textStatus}`);
});
} else {
alert("Location not found. Please try a different location.");
}
}).fail(function (jqXHR, textStatus) {
alert(`Failed to retrieve location: ${textStatus}`);
});
} else {
alert("Please enter a location name.");
}
}
function updateChart(weatherData) {
const ctx = document.getElementById('chart').getContext('2d');
const selectedOption = document.getElementById('graph').value;
let labels = weatherData.list.slice(0, 7).map((data) => data.dt_txt.split(' ')[1].slice(0, 5));
let dataPoints;
if (selectedOption === 'wind') {
dataPoints = weatherData.list.slice(0, 7).map((data) => data.wind.speed);
} else if (selectedOption === 'temperature') {
dataPoints = weatherData.list.slice(0, 7).map((data) => Math.round(data.main.temp - 273.15));
} else if (selectedOption === 'humidity') {
dataPoints = weatherData.list.slice(0, 7).map((data) => data.main.humidity);
}
if (window.myChart) {
window.myChart.destroy();
}
window.myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: `${selectedOption.charAt(0).toUpperCase() + selectedOption.slice(1)} Data`,
data: dataPoints,
backgroundColor: 'rgba(54, 162, 235, 0.6)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1
}]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
}
}
});
}
</script>
</body>
</html>