-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisualizations.js
More file actions
371 lines (307 loc) · 11.5 KB
/
visualizations.js
File metadata and controls
371 lines (307 loc) · 11.5 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Advanced Weather Visualizations
// Implements sophisticated data representation including 3D maps and interactive radar
class WeatherVisualizations {
constructor() {
this.canvas = null;
this.ctx = null;
this.animationId = null;
this.isAnimating = false;
}
// Initialize visualization system
init() {
this.canvas = document.getElementById('weatherCanvas');
if (this.canvas) {
this.ctx = this.canvas.getContext('2d');
this.resize();
window.addEventListener('resize', () => this.resize());
}
}
// Resize canvas to match window
resize() {
if (this.canvas) {
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
}
}
// Create temperature heatmap visualization
createTemperatureHeatmap(temperatureData, width, height) {
if (!this.ctx) return;
const imageData = this.ctx.createImageData(width, height);
const data = imageData.data;
// Generate heatmap based on temperature data
for (let i = 0; i < data.length; i += 4) {
const x = (i / 4) % width;
const y = Math.floor(i / 4 / width);
// Get temperature at this point (simplified)
const temp = this.getTemperatureAtPoint(x, y, temperatureData);
// Convert temperature to color
const color = this.temperatureToColor(temp);
data[i] = color.r; // Red
data[i + 1] = color.g; // Green
data[i + 2] = color.b; // Blue
data[i + 3] = 255; // Alpha
}
this.ctx.putImageData(imageData, 0, 0);
}
// Get temperature at a specific point
getTemperatureAtPoint(x, y, temperatureData) {
// Simplified implementation - in real app, this would interpolate from actual data
const centerX = this.canvas.width / 2;
const centerY = this.canvas.height / 2;
const distance = Math.sqrt(Math.pow(x - centerX, 2) + Math.pow(y - centerY, 2));
// Create a radial temperature pattern
const maxDistance = Math.sqrt(Math.pow(centerX, 2) + Math.pow(centerY, 2));
const normalizedDistance = distance / maxDistance;
// Temperature range from -10 to 35°C
return 35 - (normalizedDistance * 45);
}
// Convert temperature to color for heatmap
temperatureToColor(temp) {
// Color scale: blue (cold) -> green -> yellow -> red (hot)
if (temp < 0) {
// Blue to cyan for very cold
const intensity = Math.min(1, Math.abs(temp) / 10);
return { r: 0, g: Math.floor(255 * (1 - intensity)), b: 255 };
} else if (temp < 10) {
// Cyan to green for cold
const intensity = temp / 10;
return { r: 0, g: 255, b: Math.floor(255 * (1 - intensity)) };
} else if (temp < 20) {
// Green to yellow for mild
const intensity = (temp - 10) / 10;
return { r: Math.floor(255 * intensity), g: 255, b: 0 };
} else if (temp < 30) {
// Yellow to orange for warm
const intensity = (temp - 20) / 10;
return { r: 255, g: Math.floor(255 * (1 - intensity * 0.5)), b: 0 };
} else {
// Orange to red for hot
const intensity = Math.min(1, (temp - 30) / 10);
return { r: 255, g: Math.floor(128 * (1 - intensity)), b: 0 };
}
}
// Create animated wind flow visualization
createWindFlowVisualization(windData) {
if (!this.ctx || this.isAnimating) return;
this.isAnimating = true;
this.animateWindFlow(windData);
}
// Animate wind flow
animateWindFlow(windData) {
if (!this.ctx) {
this.isAnimating = false;
return;
}
// Clear canvas
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
// Draw wind particles
const particleCount = 200;
const time = Date.now() * 0.001;
for (let i = 0; i < particleCount; i++) {
// Particle position
const x = (i * 17) % this.canvas.width;
const y = (i * 13) % this.canvas.height;
// Animate with wind data
const windSpeed = windData.speed || 5;
const windDirection = windData.deg || 0;
// Calculate movement
const dx = Math.cos(windDirection * Math.PI / 180) * windSpeed;
const dy = Math.sin(windDirection * Math.PI / 180) * windSpeed;
// Apply animation
const posX = (x + dx * time) % this.canvas.width;
const posY = (y + dy * time) % this.canvas.height;
// Draw particle
this.ctx.fillStyle = 'rgba(200, 220, 255, 0.7)';
this.ctx.beginPath();
this.ctx.arc(posX, posY, 2, 0, Math.PI * 2);
this.ctx.fill();
}
// Continue animation
this.animationId = requestAnimationFrame(() => this.animateWindFlow(windData));
}
// Stop animation
stopAnimation() {
if (this.animationId) {
cancelAnimationFrame(this.animationId);
this.animationId = null;
}
this.isAnimating = false;
}
// Create pressure trend visualization
createPressureTrendVisualization(pressureData) {
if (!this.ctx) return;
// Clear a section of the canvas for the pressure chart
const chartWidth = 300;
const chartHeight = 150;
const x = this.canvas.width - chartWidth - 20;
const y = 20;
this.ctx.clearRect(x, y, chartWidth, chartHeight);
// Draw chart background
this.ctx.fillStyle = 'rgba(0, 0, 0, 0.3)';
this.ctx.fillRect(x, y, chartWidth, chartHeight);
// Draw pressure trend line
this.ctx.beginPath();
this.ctx.strokeStyle = '#4A90E2';
this.ctx.lineWidth = 2;
// Generate sample pressure data points
const points = [];
for (let i = 0; i < 24; i++) {
// Simulate pressure changes over 24 hours
const basePressure = 1013.25;
const variation = Math.sin(i * Math.PI / 12) * 10;
const noise = (Math.random() - 0.5) * 5;
points.push(basePressure + variation + noise);
}
// Draw the line
const stepX = chartWidth / (points.length - 1);
for (let i = 0; i < points.length; i++) {
const px = x + i * stepX;
// Normalize pressure to chart height (980-1040 hPa range)
const py = y + chartHeight - ((points[i] - 980) / 60) * chartHeight;
if (i === 0) {
this.ctx.moveTo(px, py);
} else {
this.ctx.lineTo(px, py);
}
}
this.ctx.stroke();
// Draw labels
this.ctx.fillStyle = 'white';
this.ctx.font = '12px Arial';
this.ctx.fillText('Pressure (hPa)', x + 10, y + 20);
this.ctx.fillText('24h', x + chartWidth - 30, y + chartHeight - 10);
}
// Create humidity comfort zone visualization
createHumidityComfortChart(humidity, temperature) {
if (!this.ctx) return;
// Clear a section for the comfort chart
const chartWidth = 200;
const chartHeight = 200;
const x = 20;
const y = this.canvas.height - chartHeight - 20;
this.ctx.clearRect(x, y, chartWidth, chartHeight);
// Draw comfort zones
const centerX = x + chartWidth / 2;
const centerY = y + chartHeight / 2;
const maxRadius = Math.min(chartWidth, chartHeight) / 2 - 10;
// Draw comfort zones based on temperature-humidity relationship
const zones = [
{ name: 'Comfortable', radius: maxRadius * 0.3, color: 'rgba(34, 197, 94, 0.3)' },
{ name: 'Moderate', radius: maxRadius * 0.6, color: 'rgba(251, 191, 36, 0.3)' },
{ name: 'Uncomfortable', radius: maxRadius, color: 'rgba(239, 68, 68, 0.3)' }
];
// Draw zones from outer to inner
for (let i = zones.length - 1; i >= 0; i--) {
this.ctx.fillStyle = zones[i].color;
this.ctx.beginPath();
this.ctx.arc(centerX, centerY, zones[i].radius, 0, Math.PI * 2);
this.ctx.fill();
}
// Draw current position
const comfortLevel = this.calculateComfortLevel(temperature, humidity);
const positionRadius = this.getComfortRadius(comfortLevel, maxRadius);
this.ctx.fillStyle = 'white';
this.ctx.beginPath();
this.ctx.arc(centerX, centerY, 5, 0, Math.PI * 2);
this.ctx.fill();
// Draw label
this.ctx.fillStyle = 'white';
this.ctx.font = '14px Arial';
this.ctx.textAlign = 'center';
this.ctx.fillText(`Comfort: ${comfortLevel}`, centerX, y + chartHeight + 20);
}
// Calculate comfort level
calculateComfortLevel(temperature, humidity) {
const dewPoint = this.calculateDewPoint(temperature, humidity);
if (dewPoint < 10) return 'Very Comfortable';
if (dewPoint < 16) return 'Comfortable';
if (dewPoint < 20) return 'Moderate';
if (dewPoint < 24) return 'Humid';
return 'Very Humid';
}
// Calculate dew point
calculateDewPoint(temp, humidity) {
const a = 17.27;
const b = 237.7;
const alpha = ((a * temp) / (b + temp)) + Math.log(humidity / 100.0);
return (b * alpha) / (a - alpha);
}
// Get radius for comfort level
getComfortRadius(comfortLevel, maxRadius) {
switch (comfortLevel) {
case 'Very Comfortable': return maxRadius * 0.15;
case 'Comfortable': return maxRadius * 0.25;
case 'Moderate': return maxRadius * 0.45;
case 'Humid': return maxRadius * 0.75;
case 'Very Humid': return maxRadius * 0.9;
default: return maxRadius * 0.5;
}
}
// Create UV index visualization
createUVIndexVisualization(uvIndex) {
if (!this.ctx) return;
// Clear a section for the UV chart
const chartWidth = 150;
const chartHeight = 150;
const x = this.canvas.width - chartWidth - 20;
const y = this.canvas.height - chartHeight - 20;
this.ctx.clearRect(x, y, chartWidth, chartHeight);
// Draw UV meter
const centerX = x + chartWidth / 2;
const centerY = y + chartHeight / 2;
const radius = Math.min(chartWidth, chartHeight) / 2 - 10;
// Draw background circle
this.ctx.beginPath();
this.ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
this.ctx.fillStyle = 'rgba(0, 0, 0, 0.3)';
this.ctx.fill();
// Draw UV scale
const uvColors = [
{ max: 3, color: '#22C55E' }, // Low - Green
{ max: 6, color: '#FACC15' }, // Moderate - Yellow
{ max: 8, color: '#F97316' }, // High - Orange
{ max: 11, color: '#EF4444' }, // Very High - Red
{ max: 20, color: '#7C3AED' } // Extreme - Purple
];
// Fill according to UV index
let uvColor = '#22C55E'; // Default to green
for (const level of uvColors) {
if (uvIndex <= level.max) {
uvColor = level.color;
break;
}
}
// Draw filled portion
const angle = (uvIndex / 11) * Math.PI * 1.5; // 1.5π for 270° meter
const startAngle = -Math.PI * 0.75; // Start at -135°
this.ctx.beginPath();
this.ctx.moveTo(centerX, centerY);
this.ctx.arc(centerX, centerY, radius, startAngle, startAngle + angle);
this.ctx.closePath();
this.ctx.fillStyle = uvColor;
this.ctx.fill();
// Draw center circle
this.ctx.beginPath();
this.ctx.arc(centerX, centerY, radius * 0.3, 0, Math.PI * 2);
this.ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
this.ctx.fill();
// Draw UV value
this.ctx.fillStyle = 'white';
this.ctx.font = 'bold 20px Arial';
this.ctx.textAlign = 'center';
this.ctx.textBaseline = 'middle';
this.ctx.fillText(uvIndex.toFixed(1), centerX, centerY);
// Draw label
this.ctx.font = '12px Arial';
this.ctx.fillText('UV Index', centerX, y + chartHeight - 10);
}
}
// Create and export visualization instance
const weatherVisualizations = new WeatherVisualizations();
window.weatherVisualizations = weatherVisualizations;
// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
weatherVisualizations.init();
});
// Export for use in other modules
export { WeatherVisualizations, weatherVisualizations };