Atlaslog
Atlaslog / Examples / deck.gl / Line Layer
deck.gl · Runnable Example

Line Layer

Runnable deck.gl example: Line Layer. 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-line-layer.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/deckgl-line-layer.html
<!--
  示例:线图层
  要点:LineLayer 以像素宽度画 OD 连线,宽度取流量的平方根
  依赖: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}
  #cv{flex:1 1 auto;min-height:0;width:100%;display:block}

</style>
</head>
<body>
<div class="bar"><span>线路 <b id="n">—</b></span><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 cities = {
    '北京': [116.39, 39.90], '上海': [121.47, 31.23], '广州': [113.26, 23.12],
    '成都': [104.06, 30.57], '西安': [108.94, 34.34], '武汉': [114.30, 30.59],
    '哈尔滨': [126.64, 45.75], '乌鲁木齐': [87.61, 43.82], '昆明': [102.71, 25.04]
  };
  var flows = [
    ['北京', '上海', 780], ['北京', '广州', 620], ['北京', '哈尔滨', 340],
    ['北京', '乌鲁木齐', 260], ['上海', '武汉', 380], ['上海', '成都', 310],
    ['广州', '昆明', 230], ['成都', '西安', 190], ['西安', '北京', 250],
    ['武汉', '广州', 300], ['昆明', '广州', 210], ['哈尔滨', '上海', 180]
  ].map(function (f) {
    return { a: cities[f[0]], b: cities[f[1]], v: f[2], from: f[0], to: f[1] };
  });

  document.getElementById('n').textContent = flows.length;

  var deckgl = new deck.Deck({
    canvas: 'cv',
    width: '100%',
    height: '100%',
    initialViewState: { longitude: 108, latitude: 34, zoom: 3.5, pitch: 0, bearing: 0 },
    controller: true,
    layers: [
      new deck.LineLayer({
        id: 'od',
        data: flows,
        getSourcePosition: function (d) { return d.a; },
        getTargetPosition: function (d) { return d.b; },
        getColor: function (d) {
          return d.v > 400 ? [255, 168, 90, 220] : [92, 178, 255, 200];
        },
        getWidth: function (d) { return Math.sqrt(d.v) / 5; },
        widthUnits: 'pixels',
        widthMinPixels: 1.5,
        pickable: true
      })
    ],
    getTooltip: function (info) {
      if (!info.object) return null;
      return info.object.from + ' → ' + info.object.to + ' · ' + info.object.v;
    }
  });
  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 · EXAMPLEMaplibre Overlay deck.gl · EXAMPLEMap View deck.gl · EXAMPLEMask