Network Density and Focus
The same 100,000-node data is read as density on a screen grid, then overlaid with the densest 5% of nodes; cell granularity is adjustable.
Business context
Density views serve analysts who need to locate clusters quickly within 100,000 nodes; social operations, threat intelligence and supply-chain screening all use them to narrow the scope before drilling down. Data is the same graph export as the scatter view, at the 100,000 order. Production setups also add a time window and keep only recently active nodes, so long accumulation does not flatten the density.
Two aggregations are typically prepared before entering deck.gl: a coarse world-coordinate grid computed on the data side that yields a view-independent head set, and a ScreenGridLayer that bins in screen space in real time and follows the camera. The first is reproducible and quotable in reports, the second supports exploration. Cesium has no screen-space aggregation and three.js would need custom binning and shaders; ScreenGridLayer provides both paths.
Misreading concentrates in two places: grid cell contents are reshuffled by rotationX and zoom, so hot spots in two screenshots may not be the same nodes; and cell color expresses counts rather than density, so larger cells naturally count more and colors are not comparable across cellSize values. The top 5% is also selected on the world grid, so it need not coincide with the bright cells on screen.
How it works
Density is precomputed on a coarse world grid: cells of 12 units count nodes, and nodes are taken from the highest cells down to 5% of the total (5,000). This selection is independent of the view and does not change with rotation or zoom. ScreenGridLayer does the opposite: cellSizePixels: 20 aggregates in screen space, and which cell a node projects into depends on the current rotationX and zoom, so cell contents rearrange as the view turns.
The cell slider runs from 8 to 64 px: at 8 px, secondary clusters inside a community appear, but there are thousands of cells and the color of any single cell is diluted; at 64 px only a dozen blocks remain and community boundaries blur together. The density layer uses opacity 0.9, and the top 5% of nodes sit on the grid as 2.5 px orange dots, making it possible to read which community holds the peak. Switching to 'raw scatter' shows 100,000 blue dots of 1.2 px, where density differences become visually invisible, which is the reason to aggregate first and drill down after.
| Setting | Effect |
|---|---|
| cellSizePixels: 20 | Screen-space cell edge length, slider 8–64 |
| getWeight: 1 | Each node counts as one; color expresses node count |
| top 5% nodes | Head of the ranking by counts on the 12-unit world grid |
| raw scatter | Switches between scatter and screen-grid views |
Note. Screen grid cells change with zoom, so fix the camera before comparing densities, then adjust cellSizePixels and colorRange.
Source
<!--
示例:网络密度与聚焦
要点:同一份 10 万节点数据,ScreenGridLayer 在屏幕空间聚合密度,再叠出密度前 5% 的节点
依赖: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 label{display:flex;align-items:center;gap:5px;cursor:pointer}
.bar input[type=checkbox]{accent-color:#4f9ef8}
.bar input[type=range]{width:150px;accent-color:#4f9ef8}
.bar button{font:inherit;font-size:12px;padding:3px 10px;border-radius:4px;cursor:pointer;
border:1px solid #2f3947;background:#1d242e;color:#c9d3e0}
.bar button:hover{background:#26303d}
.bar button.on{background:#2c4a7a;border-color:#4f8ad6;color:#dfe9ff}
.bar .sep{width:1px;height:18px;background:#2a313c;margin:0 4px}
#cv{flex:1 1 auto;min-height:0;width:100%;display:block}
</style>
</head>
<body>
<div class="bar">
<span>节点 <b>100,000</b> · 热点 <b>5,000</b></span><span class="sep"></span>
<label><input id="raw" type="checkbox">显示原图</label>
<label>网格 <input id="cell" type="range" min="8" max="64" step="4" value="20"><b id="gridv">20 px</b></label>
</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;
}
function rng(seed) {
var a = seed >>> 0;
return function () {
a = (a + 0x6D2B79F5) >>> 0;
var t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
var N = 100000, C = 12, TAU = Math.PI * 2;
var rand = rng(90210);
var i, j;
var nodes = new Array(N);
for (i = 0; i < N; i++) {
var c = i % C;
var ang = c / C * TAU;
nodes[i] = {
p: [
Math.cos(ang) * 160 + (rand() + rand() + rand() - 1.5) * 38,
Math.sin(ang) * 160 + (rand() + rand() + rand() - 1.5) * 38,
(rand() - 0.5) * 55
]
};
}
var GX = 40, CELL = 12, OFF = 240;
var counts = new Int32Array(GX * GX);
var buckets = new Array(GX * GX);
for (i = 0; i < buckets.length; i++) buckets[i] = null;
for (i = 0; i < N; i++) {
var ix = Math.floor((nodes[i].p[0] + OFF) / CELL);
var iy = Math.floor((nodes[i].p[1] + OFF) / CELL);
if (ix < 0) ix = 0;
if (ix >= GX) ix = GX - 1;
if (iy < 0) iy = 0;
if (iy >= GX) iy = GX - 1;
var key = ix + iy * GX;
counts[key]++;
if (!buckets[key]) buckets[key] = [];
buckets[key].push(i);
}
var order = [];
for (i = 0; i < counts.length; i++) {
if (counts[i] > 0) order.push(i);
}
order.sort(function (a, b) { return counts[b] - counts[a]; });
var hot = [], need = Math.round(N * 0.05);
for (i = 0; i < order.length && hot.length < need; i++) {
var b = buckets[order[i]];
for (j = 0; j < b.length && hot.length < need; j++) {
hot.push(nodes[b[j]].p);
}
}
var raw = false, cell = 20;
function buildLayers() {
if (raw) {
return [new deck.ScatterplotLayer({
id: 'density-raw',
data: nodes,
coordinateSystem: deck.COORDINATE_SYSTEM.CARTESIAN,
getPosition: function (d) { return d.p; },
getRadius: 1.2,
radiusUnits: 'pixels',
radiusMinPixels: 1,
getFillColor: [96, 152, 224, 150],
pickable: false
})];
}
return [
new deck.ScreenGridLayer({
id: 'density-grid',
data: nodes,
coordinateSystem: deck.COORDINATE_SYSTEM.CARTESIAN,
getPosition: function (d) { return d.p; },
getWeight: function () { return 1; },
cellSizePixels: cell,
colorRange: [
[24, 38, 76, 120],
[32, 92, 150, 160],
[56, 170, 178, 190],
[246, 200, 88, 215],
[236, 90, 58, 235]
],
opacity: 0.9,
pickable: false
}),
new deck.ScatterplotLayer({
id: 'density-hot',
data: hot,
coordinateSystem: deck.COORDINATE_SYSTEM.CARTESIAN,
getPosition: function (d) { return d; },
getRadius: 2.5,
radiusUnits: 'pixels',
radiusMinPixels: 1.5,
getFillColor: [255, 196, 96, 235],
stroked: true,
getLineColor: [40, 28, 16, 200],
getLineWidth: 1,
lineWidthUnits: 'pixels',
pickable: false
})
];
}
var deckgl = new deck.Deck({
canvas: 'cv',
width: '100%',
height: '100%',
views: new deck.OrbitView({ target: [0, 0, 0], fovy: 20 }),
initialViewState: { target: [0, 0, 0], rotationX: 26, rotationOrbit: -30, zoom: 0.3 },
controller: true,
layers: buildLayers()
});
document.getElementById('raw').addEventListener('change', function () {
raw = this.checked;
deckgl.setProps({ layers: buildLayers() });
});
document.getElementById('cell').addEventListener('input', function () {
cell = +this.value;
document.getElementById('gridv').textContent = cell + ' px';
deckgl.setProps({ layers: buildLayers() });
});
window.__deck = deckgl;
})();
</script>
</body>
</html>