Weather Station Time Series
28 weather stations each carry 48 hours of temperature and wind speed; dragging the time slider or playing it back interpolates colour and radius smoothly.
Business context
Meteorological services, agro-meteorology and environmental agencies use station time series to read diurnal variation and regional differences in temperature, humidity and wind, supporting warnings and farming advice. Data comes from automatic surface stations reporting hourly or every ten minutes, with temperature, wind speed, precipitation and visibility. A province has thousands of stations and up to 100,000 records per day; this example uses 28 stations over 48 hours.
Quality control comes first: values beyond thresholds and long-constant stuck values are removed, and missing periods are set to None rather than zero. Stations are normalised to WGS84 with site relocations corrected. Changing the hour does not rebuild station objects, only the index the accessor reads, so transitions can interpolate colour and radius while object identity persists; radius encodes wind speed and the colour domain stays fixed.
Misreading usually comes from a colour domain that adapts per frame: normalising to the current minimum and maximum makes the same temperature at dawn and afternoon appear identical, which defeats comparison across time. Radius is often read as precipitation when it encodes only wind speed. Station sparsity matters too: with few mountain sites, a large area of one colour means no observation, not uniform weather.
How it works
The 28 stations are scattered across 115.2–120.8°E, 33.8–37.8°N, each generating 48 hours of temperature from a base of 4–16 °C plus an amplitude of 3.5–8.5 °C applied to sin((h−14+phase)/24·2π), with ±0.7 °C of noise; wind speed comes from a half-wave sine between 0.8 and 10.5 m/s. ScatterplotLayer getFillColor reads d.temp[hour] and getRadius returns 3 + wind speed × 0.85 in pixels, with transitions set to 600 ms for both attributes and updateTriggers keyed on hour.
Changing the hour rebuilds neither the stations array nor any object inside it; only the index the accessor reads changes, so deck.gl can look up the previous frame's attributes by object identity and interpolate. Regenerating station objects every hour presents them as an entirely new collection, the transition fades in from default values, and 28 × 48 array allocations are added. The temperature ramp is fixed to five bands over −6–32 °C and must not be normalised to the current min and max, or 8 °C at dawn and 8 °C in the afternoon map to the same middle colour and cross-time comparison fails.
| Configuration | What it does |
|---|---|
| transitions: { getFillColor: 600, getRadius: 600 } | Colour and radius each interpolate over 600 ms |
| updateTriggers: { getFillColor: hour, getRadius: hour } | Forces attribute recomputation when the index changes |
| getRadius: 3 + wind speed × 0.85 | Radius encodes wind speed; pixel units keep it visible on screen |
| Domain −6–32 °C / 5 bands | Fixed range so colours compare directly across hours |
Note. Transitions depend on object reuse: change only the closed-over index per hour and never rebuild the data array; keep the colour domain fixed rather than normalising per frame.
Source
<!--
示例:气象站时间序列
要点:ScatterplotLayer 的 transitions 让 28 个站点的颜色与半径在 600 ms 内平滑过渡
依赖: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:12px;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:140px;accent-color:#4f9ef8}
.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="h">0</b> 小时</span>
<input id="time" type="range" min="0" max="47" step="1" value="0">
<span class="sep"></span>
<button id="play" class="on">暂停</button>
<span>半径 = 风速 · 色带 −6–32 ℃ 固定</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;
}
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 rand = rng(3719);
var stations = [];
var i, h;
for (i = 0; i < 28; i++) {
var pos = [115.2 + rand() * 5.6, 33.8 + rand() * 4.0];
var base = 4 + rand() * 12;
var amp = 3.5 + rand() * 5;
var phase = rand() * 24;
var temp = [];
var wind = [];
for (h = 0; h < 48; h++) {
var diurnal = Math.sin((h - 14 + phase) / 24 * Math.PI * 2);
temp.push(base + amp * diurnal + (rand() - 0.5) * 1.4);
wind.push(0.8 + Math.abs(Math.sin(h / 8 + phase * 0.4)) * 6.5 + rand() * 3.2);
}
stations.push({ pos: pos, temp: temp, wind: wind });
}
var T_MIN = -6;
var T_MAX = 32;
var RAMP = [
[38, 52, 120], [46, 118, 196], [84, 190, 196],
[240, 214, 112], [236, 92, 64]
];
// 固定 −6–32 ℃ 的值域,跨时刻颜色才可比
function tempColor(v) {
var t = (v - T_MIN) / (T_MAX - T_MIN);
t = t < 0 ? 0 : (t > 1 ? 1 : t);
var k = t * 4;
var i0 = Math.floor(k);
if (i0 > 3) i0 = 3;
var f = k - i0;
var c0 = RAMP[i0];
var c1 = RAMP[i0 + 1];
return [
Math.round(c0[0] + (c1[0] - c0[0]) * f),
Math.round(c0[1] + (c1[1] - c0[1]) * f),
Math.round(c0[2] + (c1[2] - c0[2]) * f),
236
];
}
var hour = 0;
var playing = true;
var lastStep = performance.now();
function buildLayer() {
return new deck.ScatterplotLayer({
id: 'station-series',
data: stations,
getPosition: function (d) { return d.pos; },
getRadius: function (d) { return 3 + d.wind[hour] * 0.85; },
radiusUnits: 'pixels',
radiusMinPixels: 2.5,
getFillColor: function (d) { return tempColor(d.temp[hour]); },
stroked: true,
getLineColor: [10, 15, 22, 200],
getLineWidth: 1,
lineWidthUnits: 'pixels',
transitions: { getFillColor: 600, getRadius: 600 },
updateTriggers: { getFillColor: hour, getRadius: hour },
pickable: true
});
}
var deckgl = new deck.Deck({
canvas: 'cv',
width: '100%',
height: '100%',
initialViewState: { longitude: 118.0, latitude: 35.8, zoom: 7.6, pitch: 0, bearing: 0 },
controller: true,
layers: [buildLayer()],
getTooltip: function (info) {
if (!info.object) return null;
return '第 ' + hour + ' 小时 · ' + info.object.temp[hour].toFixed(1) +
' ℃ · 风速 ' + info.object.wind[hour].toFixed(1) + ' m/s';
}
});
function render() {
document.getElementById('h').textContent = hour;
document.getElementById('time').value = hour;
deckgl.setProps({ layers: [buildLayer()] });
}
document.getElementById('time').addEventListener('input', function (e) {
hour = Number(e.target.value);
if (playing) {
playing = false;
document.getElementById('play').textContent = '播放';
document.getElementById('play').classList.remove('on');
}
render();
});
document.getElementById('play').addEventListener('click', function (e) {
playing = !playing;
e.currentTarget.textContent = playing ? '暂停' : '播放';
e.currentTarget.classList.toggle('on', playing);
lastStep = performance.now();
});
function tick(now) {
if (playing && now - lastStep > 620) {
lastStep = now;
hour = (hour + 1) % 48;
render();
}
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
window.__deck = deckgl;
})();
</script>
</body>
</html>