用前端帮我实现一个可视化基站地图分布的前端项目,只用前端,数据可以先用模拟数据,要求基站分布地图可视化,故障站点、断站、退服小区用不同状态颜色显示,同时有小窗口对不同状态站点有统…
- User
用前端帮我实现一个可视化基站地图分布的前端项目,只用前端,数据可以先用模拟数据,要求基站分布地图可视化,故障站点、断站、退服小区用不同状态颜色显示,同时有小窗口对不同状态站点有统计表,全部代码写进一个html
Assistant
好的!下面是一个 完整的 HTML 文件,它包含了 所有前端代码(HTML、CSS、JavaScript),可以直接运行,无需额外文件。
这个页面实现了以下功能:
- 使用 Leaflet 显示地图。
- 使用 模拟数据 渲染基站,不同状态(正常、故障、断站、退服)用不同颜色显示。
- 地图右上角有一个 统计窗口,使用 Chart.js 展示饼图统计。
- 所有代码写在一个
.html文件中,方便直接打开使用。
✅ 完整 HTML 文件(直接复制保存为
.html文件即可运行)<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>基站地图分布可视化</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Leaflet 样式 --> <link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" /> <style> body { margin: 0; font-family: Arial, sans-serif; } #map { height: 100vh; width: 100%; } .stats-window { position: absolute; top: 10px; right: 10px; background: white; padding: 10px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); width: 300px; z-index: 1000; } canvas { max-width: 100%; } </style> </head> <body> <div id="map"></div> <!-- 统计信息窗口 --> <div class="stats-window"> <canvas id="statsChart"></canvas> </div> <!-- Leaflet JS --> <script src="https://unpkg.com/leaflet/dist/leaflet.js"></script> <!-- Chart.js --> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <script> document.addEventListener('DOMContentLoaded', function () { // 初始化地图 const map = L.map('map').setView([39.9042, 116.4074], 12); // 北京中心 // 添加地图图层 L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> 贡献者' }).addTo(map); // 模拟基站数据 const baseStations = [ { lat: 39.9042, lng: 116.4074, status: 'normal' }, { lat: 39.9142, lng: 116.4174, status: 'fault' }, { lat: 39.9242, lng: 116.4274, status: 'disconnected' }, { lat: 39.8942, lng: 116.3974, status: 'retired' }, { lat: 39.9342, lng: 116.4374, status: 'normal' }, { lat: 39.8842, lng: 116.3874, status: 'fault' }, { lat: 39.9442, lng: 116.4474, status: 'disconnected' }, { lat: 39.8742, lng: 116.3774, status: 'retired' }, ]; // 状态颜色映射 const statusColors = { normal: '#00FF00', fault: '#FF0000', disconnected: '#FFA500', retired: '#808080' }; // 统计数据 const stats = { normal: 0, fault: 0, disconnected: 0, retired: 0 }; // 添加基站标记 baseStations.forEach(station => { const color = statusColors[station.status]; L.circleMarker([station.lat, station.lng], { radius: 8, color: '#000', weight: 1, fillColor: color, fillOpacity: 0.8 }).addTo(map); stats[station.status]++; }); // 创建饼图统计 const ctx = document.getElementById('statsChart').getContext('2d'); new Chart(ctx, { type: 'pie', data: { labels: ['正常', '故障', '断站', '退服'], datasets: [{ label: '基站状态统计', data: [stats.normal, stats.fault, stats.disconnected, stats.retired], backgroundColor: [ statusColors.normal, statusColors.fault, statusColors.disconnected, statusColors.retired ], borderColor: [ '#000', '#000', '#000', '#000' ], borderWidth: 1 }] }, options: { responsive: true, plugins: { legend: { position: 'right' }, title: { display: true, text: '基站状态统计' } } } }); }); </script> </body> </html>
✅ 功能说明
功能 说明 地图显示 使用 Leaflet 加载 OpenStreetMap 地图 基站标记 不同状态用不同颜色表示:绿色(正常)、红色(故障)、橙色(断站)、灰色(退服) 统计图表 使用 Chart.js 在右上角显示饼图,统计各状态数量 数据来源 使用模拟数据,可替换为真实数据接口 响应式 图表和地图自动适配屏幕大小
✅ 使用方法
- 将上面的代码复制到一个
.html文件中,比如base_station_map.html。 - 用浏览器打开即可看到地图和统计信息。
- 如需替换真实数据,只需修改
baseStations数组即可。
如果你还需要 点击基站弹窗显示详细信息、搜索基站、导出统计图 等功能,也可以继续扩展这个模板。需要我帮你加上这些功能吗?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.