Atlaslog
Atlaslog / Examples / deck.gl / Hexagon
deck.gl · Runnable Example

Hexagon

Runnable deck.gl example: Hexagon. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All deck.gl examples
/demos/deckgl-hexagon.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/deckgl-hexagon.html
<!--
  示例:deck.gl 六边形聚合
  要点:HexagonLayer 按米制半径分箱,挤出高度表达总量,半径可切换
  依赖: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:8px;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%}
</style>
</head>
<body>
<div class="bar">
  <span>点数 <b id="n">—</b></span>
  <span class="sep"></span>
  <span>半径</span>
  <button data-radius="600">600 m</button>
  <button data-radius="1000" class="on">1 km</button>
  <button data-radius="1600">1.6 km</button>
  <button data-radius="2500">2.5 km</button>
  <span class="sep"></span>
  <button id="ex">挤出</button>
  <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 hubs = [
    [116.390, 39.900, 0.20],
    [116.470, 39.980, 0.16],
    [116.300, 39.860, 0.14],
    [116.520, 39.840, 0.12],
    [116.350, 40.010, 0.11],
    [116.440, 39.930, 0.18]
  ];

  var points = [];
  for (var i = 0; i < 90000; i++) {
    var h = hubs[i % hubs.length];
    var r = Math.sqrt(Math.random()) * h[2];
    var a = Math.random() * Math.PI * 2;
    points.push({
      lng: h[0] + Math.cos(a) * r,
      lat: h[1] + Math.sin(a) * r * 0.85,
      weight: 0.4 + Math.random() * 1.6
    });
  }

  document.getElementById('n').textContent = points.length.toLocaleString();

  var colorRange = [
    [18, 32, 66, 170],
    [36, 88, 148, 195],
    [52, 152, 176, 210],
    [150, 205, 130, 220],
    [244, 200, 84, 235],
    [235, 104, 52, 250]
  ];

  var radius = 1000;
  var extruded = true;

  function buildLayer() {
    return new deck.HexagonLayer({
      id: 'hex',
      data: points,
      getPosition: function (d) { return [d.lng, d.lat]; },
      radius: radius,
      coverage: 0.9,
      extruded: extruded,
      elevationScale: radius >= 1600 ? 2.4 : 1.1,
      elevationRange: [0, 3200],
      getColorWeight: function (d) { return d.weight; },
      colorAggregation: 'SUM',
      getElevationWeight: function (d) { return d.weight; },
      elevationAggregation: 'SUM',
      colorRange: colorRange,
      upperPercentile: 100,
      material: { ambient: 0.52, diffuse: 0.72, shininess: 18 },
      pickable: true
    });
  }

  var deckgl = new deck.Deck({
    canvas: 'cv',
    width: '100%',
    height: '100%',
    initialViewState: {
      longitude: 116.40,
      latitude: 39.93,
      zoom: 10.5,
      pitch: 48,
      bearing: -12,
      maxPitch: 70
    },
    controller: true,
    layers: [buildLayer()],
    getTooltip: function (info) {
      if (!info.object) return null;
      return info.object.count + ' 个点 · 权重和 ' +
        Math.round(info.object.elevationValue);
    }
  });

  var buttons = document.querySelectorAll('[data-radius]');
  for (var b = 0; b < buttons.length; b++) {
    buttons[b].addEventListener('click', function (e) {
      radius = Number(e.currentTarget.getAttribute('data-radius'));
      for (var k = 0; k < buttons.length; k++) {
        buttons[k].classList.toggle('on', buttons[k] === e.currentTarget);
      }
      deckgl.setProps({ layers: [buildLayer()] });
    });
  }

  document.getElementById('ex').addEventListener('click', function (e) {
    extruded = !extruded;
    e.currentTarget.classList.toggle('on', extruded);
    deckgl.setProps({ layers: [buildLayer()] });
  });

  document.getElementById('ex').classList.toggle('on', true);
  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.

More deck.gl examples

deck.gl · EXAMPLEHexagon Radius deck.gl · EXAMPLEHighway deck.gl · EXAMPLEHover Highlight