Supply-Demand Gap Binning
Order points are weighted +1 and idle-vehicle points -0.6, merged into a single HexagonLayer that renders each cell's supply-demand gap on a deep blue to orange-red ramp.
Business context
Pricing and supply strategy teams use this binned view to locate areas that should trigger surge pricing, and to review afterwards whether a surge actually covered the gap. The inputs are two live position streams, order points and idle-vehicle points, sliced into 15-minute to one-hour windows; a city produces tens of thousands of points per window, doubling on holidays and in bad weather, so the data is downsampled before it reaches the layer.
Both point sets are first aligned to one coordinate system and time window, then idle vehicles are converted into equivalent order volume through an operations-calibrated coefficient. Only after the signed weights are merged does the array go to HexagonLayer. A single layer with signed weights is used instead of two colored layers because the gap is itself a signed quantity; SUM preserves direction, so one ramp crossing zero reads both surplus and shortage.
The grey midpoint is the easiest thing to misread: zero does not sit at the middle of the ramp, because total negative weight is smaller than total positive weight, so the true equilibrium falls at roughly 37%. Centering grey makes the balanced zone look like a capacity surplus. The -0.6 coefficient is also an empirical value that must be recalibrated per city and per time band; reusing it unchanged shifts every gap direction.
How it works
30,000 order points carry weight +1 and 30,000 idle-vehicle points carry -0.6; the two sets are concatenated into one array passed to the HexagonLayer with id hex-surge, radius 1200, coverage 0.9, extruded false, getColorWeight returning d.w directly and colorAggregation set to SUM. Negative cells shade deep blue, meaning idle capacity exceeds orders; positive cells run through grey and orange to orange-red, meaning unmet demand.
The -0.6 means one idle vehicle is counted as 0.6 units of effective supply, so equilibrium holds when orders roughly equal 0.6 times idle vehicles. With 30,000 points per set the SUM domain is about -18,000 to +30,000, putting zero at roughly 37% of the ramp; the nine-step band therefore places neutral grey at step 4 rather than dead center. Putting grey in the center would make the equilibrium point read as a cool surplus.
| Configuration | What it does |
|---|---|
| getColorWeight | +1 for orders and -0.6 for idle vehicles; the sign sets the gap direction |
| colorAggregation | SUM keeps the sign; MEAN erases the total difference |
| radius | 1200 m cells make area-level gaps easier to see |
| extruded | false, so color alone carries the signal without column occlusion |
Note. Aggregating negative weights produces a ramp domain that crosses zero; align the grey step to the percentage where zero actually falls, then tune either end color.
Source
<!--
示例:供需缺口分箱
要点:订单记 +1、空驶车记 -0.6,单层 HexagonLayer 用 SUM 把正负权重压进一条跨零色带
依赖: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:10px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600}
.bar button{font:inherit;font-size:12px;padding:3px 10px;border-radius:5px;cursor:pointer;
border:1px solid #303845;background:#1d2531;color:#c8d2e0}
.bar button:hover{background:#253040}
.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 id="m">缺口权重</b></span>
<span>点数 <b id="n">—</b></span>
<span class="sep"></span>
<button data-mode="net" class="on">缺口权重</button>
<button data-mode="orders">仅订单</button>
<span class="sep"></span>
<span>radius 1200 m · SUM · 不挤出</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 demand = [
[116.390, 39.900, 0.18],
[116.470, 39.980, 0.15],
[116.300, 39.860, 0.13],
[116.520, 39.840, 0.12],
[116.350, 40.010, 0.11],
[116.440, 39.930, 0.16]
];
var supply = [];
for (var s = 0; s < demand.length; s++) {
supply.push([demand[s][0] + 0.022, demand[s][1] - 0.018, 0.10]);
}
var orders = [];
var vehicles = [];
var i, h, r, a;
for (i = 0; i < 30000; i++) {
h = demand[i % demand.length];
r = Math.sqrt(Math.random()) * h[2];
a = Math.random() * Math.PI * 2;
orders.push({
lng: h[0] + Math.cos(a) * r,
lat: h[1] + Math.sin(a) * r * 0.85,
w: 1
});
}
for (i = 0; i < 30000; i++) {
h = supply[i % supply.length];
r = Math.sqrt(Math.random()) * h[2];
a = Math.random() * Math.PI * 2;
vehicles.push({
lng: h[0] + Math.cos(a) * r,
lat: h[1] + Math.sin(a) * r * 0.85,
w: -0.6
});
}
var points = orders.concat(vehicles);
var ramp = [
[12, 36, 78, 220],
[30, 78, 132, 220],
[72, 124, 168, 220],
[128, 136, 142, 225],
[186, 148, 102, 230],
[226, 132, 72, 235],
[236, 92, 44, 240],
[214, 52, 32, 245],
[170, 24, 26, 250]
];
var mode = 'net';
function buildLayer() {
return new deck.HexagonLayer({
id: 'hex-surge',
data: mode === 'net' ? points : orders,
getPosition: function (d) { return [d.lng, d.lat]; },
radius: 1200,
coverage: 0.9,
extruded: false,
getColorWeight: function (d) { return d.w; },
colorAggregation: 'SUM',
colorRange: ramp,
upperPercentile: 100,
opacity: 0.88,
pickable: true
});
}
var deckgl = new deck.Deck({
canvas: 'cv',
width: '100%',
height: '100%',
initialViewState: {
longitude: 116.40,
latitude: 39.93,
zoom: 10.4,
pitch: 0,
bearing: 0
},
controller: true,
layers: [buildLayer()],
getTooltip: function (info) {
if (!info.object) return null;
return info.object.count + ' 个点 · SUM ' + Math.round(info.object.colorValue);
}
});
var buttons = document.querySelectorAll('[data-mode]');
for (var b = 0; b < buttons.length; b++) {
buttons[b].addEventListener('click', function (e) {
mode = e.currentTarget.getAttribute('data-mode');
for (var k = 0; k < buttons.length; k++) {
buttons[k].classList.toggle('on', buttons[k] === e.currentTarget);
}
document.getElementById('m').textContent = mode === 'net' ? '缺口权重' : '仅订单';
document.getElementById('n').textContent =
(mode === 'net' ? points.length : orders.length).toLocaleString();
deckgl.setProps({ layers: [buildLayer()] });
});
}
document.getElementById('n').textContent = points.length.toLocaleString();
window.__deck = deckgl;
})();
</script>
</body>
</html>