Data Filter
Runnable deck.gl example: Data Filter. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:数据过滤扩展
核心逻辑:DataFilterExtension 在 GPU 上按数值区间裁剪实例,滑块只改 filterRange
数据:页面内生成的 4000 个点,每个点带一个建成年份
依赖:deck.gl 9.0 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>数据过滤扩展</title>
<style>
html,body{height:100%;margin:0;background:#0d1117}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
.bar{position:relative;z-index:5;flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
display:flex;gap:14px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600}
.bar input[type=range]{width:130px}
#cv{flex:1 1 auto;min-height:0;width:100%}
</style>
</head>
<body>
<div class="bar">
<span>总点数 <b id="n">—</b></span>
<span>区间 <b id="rg">1970–2020</b></span>
<label>起 <input id="a" type="range" min="1970" max="2020" step="1" value="1970"></label>
<label>止 <input id="b" type="range" min="1970" max="2020" step="1" value="2020"></label>
<span>过滤在着色器里完成,不改数据</span>
</div>
<canvas id="cv"></canvas>
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.0.35/dist.min.js"></script>
<script>
(function () {
'use strict';
if (typeof deck === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#ff8c74">deck.gl 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var cities = [
[116.39, 39.90], [121.47, 31.23], [113.26, 23.12], [104.06, 30.57],
[108.94, 34.34], [114.30, 30.59], [126.64, 45.75], [87.61, 43.82],
[120.15, 30.28], [118.80, 32.06], [106.55, 29.56], [102.71, 25.04]
];
var data = [];
for (var i = 0; i < 4000; i++) {
var c = cities[i % cities.length];
data.push({
lng: c[0] + (Math.random() + Math.random() - 1) * 2.4,
lat: c[1] + (Math.random() + Math.random() - 1) * 1.8,
/* 年份分布偏新,越接近 2020 的点越多 */
year: Math.round(1970 + Math.pow(Math.random(), 0.6) * 50)
});
}
document.getElementById('n').textContent = data.length.toLocaleString();
var range = [1970, 2020];
function build(filterRange) {
return new deck.ScatterplotLayer({
id: 'buildings',
data: data,
getPosition: function (d) { return [d.lng, d.lat]; },
getRadius: 1.6,
radiusUnits: 'pixels',
radiusMinPixels: 1,
getFillColor: function (d) {
var t = (d.year - 1970) / 50;
return [Math.round(80 + 175 * t), Math.round(180 - 60 * t), 220, 190];
},
getFilterValue: function (d) { return d.year; },
filterRange: filterRange,
extensions: [new deck.DataFilterExtension({ filterSize: 1 })],
pickable: true
});
}
var current = build(range);
var deckgl = new deck.Deck({
canvas: document.getElementById('cv'),
width: '100%',
height: '100%',
initialViewState: {
longitude: 108,
latitude: 34,
zoom: 3.4,
pitch: 25,
bearing: 0,
maxPitch: 65
},
controller: true,
layers: [current],
getTooltip: function (info) {
return info.object ? info.object.year + ' 年' : null;
}
});
function apply() {
var lo = parseInt(document.getElementById('a').value, 10);
var hi = parseInt(document.getElementById('b').value, 10);
/* 两个滑块可能交叉,这里保证区间方向正确 */
if (lo > hi) { var t = lo; lo = hi; hi = t; }
document.getElementById('rg').textContent = lo + '–' + hi;
current = current.clone({ filterRange: [lo, hi] });
deckgl.setProps({ layers: [current] });
}
document.getElementById('a').addEventListener('input', apply);
document.getElementById('b').addEventListener('input', apply);
window.__deck = deckgl;
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.