Multi-metric Water Quality at Monitoring Sections
90 sections each carry pH, dissolved oxygen and ammonia nitrogen; switching the metric changes both the colour direction and the radius basis.
Business context
River basin monitoring centres and river chief offices use section maps to identify which reach exceeds standards and by how much, then to plan source tracing and remediation. Data comes from manual sampling and online monitoring at national and provincial sections, with pH, dissolved oxygen, ammonia nitrogen, permanganate index and sampling time. A basin may have hundreds of sections; this example uses 90, updated monthly or weekly.
Each metric is converted to a class or an exceedance factor against the surface water quality standard before rendering; colouring raw concentrations prevents comparison between metrics with different units. Missing values and results below the detection limit are handled separately and never filled with zero. All three metrics share one ScatterplotLayer, so switching only changes metric in updateTriggers, with transitions carrying the colour over 450 ms and the section objects left intact.
The frequent error is colour direction: pH is diverging with green at neutral, while dissolved oxygen is safe at high values and ammonia nitrogen exceeds at high values, so the two run in opposite directions; sharing one ramp makes severe ammonia nitrogen look safe. Radius encodes the exceedance factor, not concentration, and the two cannot be read interchangeably. A section represents an instantaneous condition at that point and does not extrapolate to the whole river; period averages are what assessment uses.
How it works
The 90 sections are distributed across 114.5–119.9°E, 33.5–37.2°N. pH is the mean of three uniform random values, giving 5.9–9.1; dissolved oxygen ranges 0.8–11.4 mg/L; ammonia nitrogen uses rand^2.2 to produce a skewed 0.03–4.2 mg/L. Three five-band ramps are hardcoded: pH runs from blue [44,96,184] through green [112,200,132] to red [238,106,72] over 5.9–9.1 with green near 7.5; dissolved oxygen uses red [232,88,64] at low values and blue [64,138,224] at high values; ammonia nitrogen uses blue [64,146,220] at low values and red [208,52,52] at high values. Radius is 4 + exceedance × 3.4 px, with pH counted as |pH−7.5|−1 against a limit of 9, capped at 18 px.
pH must use a diverging ramp with neutral falling on green and acid and alkaline at either end; dissolved oxygen and ammonia nitrogen are both sequential but point in opposite directions, because high dissolved oxygen is safe while high ammonia nitrogen is a violation. Giving ammonia nitrogen the same high-value-blue ramp as dissolved oxygen would render a 3 mg/L violation in the same blue as oxygen-rich water. On switching, updateTriggers is keyed on metric, the 90 section objects are not rebuilt, and transitions interpolate colour from the old metric to the new one over 450 ms.
| Configuration | What it does |
|---|---|
| pH diverging ramp 5.9–9.1 | Green near 7.5, with acid and alkaline at opposite ends |
| Dissolved oxygen / ammonia nitrogen sequential ramps | Low red and high blue, opposite to ammonia nitrogen's low blue and high red |
| getRadius: 4 + exceedance × 3.4 | Radius encodes exceedance, up to 18 px |
| updateTriggers: { getFillColor: metric } | Forces colour recomputation when the metric changes |
Note. When one ScatterplotLayer carries several metrics, keep ramp, domain and exceedance basis in the metric config; high values can mean opposite things, so never share one ramp.
Source
<!--
示例:水质断面多指标
要点:同一批断面切换 pH / 溶解氧 / 氨氮时同时更换色带方向与半径口径
依赖: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="n">90</b></span>
<span class="sep"></span>
<button data-m="ph" class="on">pH</button>
<button data-m="oxy">溶解氧</button>
<button data-m="nh3">氨氮</button>
<span class="sep"></span>
<span id="std">pH · Ⅲ类 6–9</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(5309);
var sections = [];
var i;
for (i = 0; i < 90; i++) {
sections.push({
pos: [114.5 + rand() * 5.4, 33.5 + rand() * 3.7],
ph: 5.9 + ((rand() + rand() + rand()) / 3) * 3.2,
oxy: 0.8 + ((rand() + rand()) / 2) * 10.6,
nh3: 0.03 + Math.pow(rand(), 2.2) * 4.2
});
}
var METRIC = {
ph: {
name: 'pH', key: 'ph', range: [5.9, 9.1],
ramp: [[44, 96, 184], [86, 178, 214], [112, 200, 132], [242, 202, 92], [238, 106, 72]],
std: 'Ⅲ类 6–9'
},
oxy: {
name: '溶解氧', key: 'oxy', range: [0, 11.4],
ramp: [[232, 88, 64], [238, 162, 80], [240, 214, 110], [122, 204, 182], [64, 138, 224]],
std: 'Ⅲ类 ≥5 mg/L'
},
nh3: {
name: '氨氮', key: 'nh3', range: [0, 4.2],
ramp: [[64, 146, 220], [96, 196, 190], [238, 210, 110], [236, 132, 72], [208, 52, 52]],
std: 'Ⅲ类 ≤1.0 mg/L'
}
};
var metric = 'ph';
function rampColor(cfg, v) {
var t = (v - cfg.range[0]) / (cfg.range[1] - cfg.range[0]);
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 = cfg.ramp[i0];
var c1 = cfg.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),
232
];
}
// pH 以 7.5 为中心取偏离,溶解氧低于 5、氨氮高于 1.0 计超标
function exceedOf(d, key) {
var ex;
if (key === 'ph') {
ex = Math.abs(d.ph - 7.5) - 1;
return ex > 0 ? ex : 0;
}
if (key === 'oxy') {
ex = 5 - d.oxy;
return ex > 0 ? ex : 0;
}
ex = d.nh3 - 1;
return ex > 0 ? ex : 0;
}
function radiusOf(d) {
var ex = exceedOf(d, metric);
var r = 4 + ex * (metric === 'ph' ? 9 : 3.4);
return r > 18 ? 18 : r;
}
function buildLayer() {
var cfg = METRIC[metric];
return new deck.ScatterplotLayer({
id: 'wq-section',
data: sections,
getPosition: function (d) { return d.pos; },
getRadius: radiusOf,
radiusUnits: 'pixels',
radiusMinPixels: 3,
getFillColor: function (d) { return rampColor(cfg, d[cfg.key]); },
stroked: true,
getLineColor: [12, 18, 26, 190],
lineWidthUnits: 'pixels',
getLineWidth: 1,
transitions: { getFillColor: 450, getRadius: 450 },
updateTriggers: { getFillColor: metric, getRadius: metric },
pickable: true
});
}
var deckgl = new deck.Deck({
canvas: 'cv',
width: '100%',
height: '100%',
initialViewState: { longitude: 117.2, latitude: 35.3, zoom: 7.6, pitch: 0, bearing: 0 },
controller: true,
layers: [buildLayer()],
getTooltip: function (info) {
if (!info.object) return null;
var d = info.object;
return 'pH ' + d.ph.toFixed(1) + ' · 溶解氧 ' + d.oxy.toFixed(1) +
' mg/L · 氨氮 ' + d.nh3.toFixed(2) + ' mg/L';
}
});
var buttons = document.querySelectorAll('[data-m]');
function selectMetric(m) {
metric = m;
for (var k = 0; k < buttons.length; k++) {
buttons[k].classList.toggle('on', buttons[k].getAttribute('data-m') === m);
}
var cfg = METRIC[m];
document.getElementById('std').textContent = cfg.name + ' · ' + cfg.std;
deckgl.setProps({ layers: [buildLayer()] });
}
for (i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', function (e) {
selectMetric(e.currentTarget.getAttribute('data-m'));
});
}
window.__deck = deckgl;
})();
</script>
</body>
</html>