Patterns for visualizing data on maps including choropleth maps, heat maps, 3D visualizations, data-driven styling, and animated data. Covers layer types, color scales, and performance optimization.
在Mapbox地图上可视化数据的全面模式。涵盖等值区域图、热力图、3D拉伸、数据驱动样式、动画可视化以及数据密集型应用的性能优化。
在以下情况下使用此技能:
最佳用途: 区域数据(州、县、邮政编码)、统计比较
模式: 根据数据值对多边形进行颜色编码
javascript
map.on(load, () => {
// 添加数据源(带属性的GeoJSON)
map.addSource(states, {
type: geojson,
data: https://example.com/states.geojson // 带人口属性的要素
});
// 添加数据驱动颜色的填充图层
map.addLayer({
id: states-layer,
type: fill,
source: states,
paint: {
fill-color: [
interpolate,
[linear],
[get, population],
0,
#f0f9ff, // 低人口为浅蓝色
500000,
#7fcdff,
1000000,
#0080ff,
5000000,
#0040bf, // 高人口为深蓝色
10000000,
#001f5c
],
fill-opacity: 0.75
}
});
// 添加边界图层
map.addLayer({
id: states-border,
type: line,
source: states,
paint: {
line-color: #ffffff,
line-width: 1
}
});
// 添加可复用的悬停弹窗
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
map.on(mousemove, states-layer, (e) => {
if (e.features.length > 0) {
map.getCanvas().style.cursor = pointer;
const feature = e.features[0];
popup
.setLngLat(e.lngLat)
.setHTML(
人口: ${feature.properties.population.toLocaleString()}
)
.addTo(map);
}
});
map.on(mouseleave, states-layer, () => {
map.getCanvas().style.cursor = ;
popup.remove();
});
});
step vs interpolate: 上面的示例使用interpolate实现平滑的颜色渐变。对于离散颜色桶(例如低/中/高),使用[step, [get, population], #f0f0f0, 500000, #fee0d2, 2000000, #fc9272, 10000000, #de2d26]。当数据有自然分类或边界值精确时,优先使用step。
色阶策略:
javascript
// 线性插值(连续色阶)
fill-color: [
interpolate,
[linear],
[get, value],
0, #ffffcc,
25, #78c679,
50, #31a354,
100, #006837
]
// 步进区间(离散桶)
fill-color: [
step,
[get, value],
#ffffcc, // 默认颜色
25, #c7e9b4,
50, #7fcdbb,
75, #41b6c4,
100, #2c7fb8
]
// 基于案例(分类数据)
fill-color: [
match,
[get, category],
residential, #ffd700,
commercial, #ff6b6b,
industrial, #4ecdc4,
park, #45b7d1,
#cccccc // 默认
]
最佳用途: 点密度、事件位置、事件聚类
模式: 可视化点的密度
javascript
map.on(load, () => {
// 添加数据源(点)
map.addSource(incidents, {
type: geojson,
data: {
type: FeatureCollection,
features: [
{
type: Feature,
geometry: {
type: Point,
coordinates: [-122.4194, 37.7749]
},
properties: {
intensity: 1
}
}
// ... 更多点
]
}
});
// 添加热力图图层
map.addLayer({
id: incidents-heat,
type: heatmap,
source: incidents,
maxzoom: 15,
paint: {
// 根据强度属性增加权重
heatmap-weight: [interpolate, [linear], [get, intensity], 0, 0, 6, 1],
// 随着缩放级别增加强度
heatmap-intensity: [interpolate, [linear], [zoom], 0, 1, 15, 3],
// 热力图颜色渐变
heatmap-color: [
interpolate,
[linear],
[heatmap-density],
0,
rgba(33,102,172,0),
0.2,
rgb(103,169,207),
0.4,
rgb(209,229,240),
0.6,
rgb(253,219,199),
0.8,
rgb(239,138,98),
1,
rgb(178,24,43)
],
// 根据缩放级别调整半径
heatmap-radius: [interpolate, [linear], [zoom], 0, 2, 15, 20],
// 在更高缩放级别降低不透明度
heatmap-opacity: [interpolate, [linear], [zoom], 7, 1, 15, 0]
}
});
// 在高缩放级别添加单个点的圆形图层
map.addLayer({
id: incidents-point,
type: circle,
source: incidents,
minzoom: 14,
paint: {
circle-radius: [interpolate, [linear], [zoom], 14, 4, 22, 30],
circle-color: #ff4444,
circle-opacity: 0.8,
circle-stroke-color: #fff,
circle-stroke-width: 1
}
});
});
javascript
// 使用ColorBrewer色阶确保无障碍性
// https://colorbrewer2.org/
// 好:顺序(单一色调)
const sequentialScale = [#f0f9ff, #bae4ff, #7fcdff, #0080ff, #001f5c];
// 好:发散(双色调)
const divergingScale = [#d73027, #fc8d59, #fee08b, #d9ef8b, #91cf60, #1a9850];
// 好:定性(不同类别)
const qualitativeScale = [#e41a1c, #377eb8, #4daf4a, #984ea3, #ff7f00];
// 避免:红绿色盲无障碍性
// 使用:蓝橙或紫绿代替
javascript
// 处理缺失或无效数据
map.on(load, () => {
map.addSource(data, {
type: geojson,
data: dataUrl
});
map.addLayer({
id: data-viz,
type: fill,
source: data,
paint: {
fill-color: [
case,
[has, value], // 检查属性是否存在
[interpolate, [linear], [get, value], 0, #f0f0f0, 100, #0080ff],
#cccccc // 缺失数据的默认颜色
]
}
});
// 处理地图错误
map.on(error, (e) => {
console.error(地图错误:, e.error);
});
});
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 mapbox-data-visualization-patterns-1775930132 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 mapbox-data-visualization-patterns-1775930132 技能
skillhub install mapbox-data-visualization-patterns-1775930132
文件大小: 18.18 KB | 发布时间: 2026-4-12 10:30