Atlaslog
Atlaslog / Examples / deck.gl / Business scenarios / Two-Period Sales Comparison
deck.gl · Business scenario

Two-Period Sales Comparison

For the same 100 trade areas, current and previous period columns stand side by side offset 220 m each, comparing heights and total difference directly.

Open standalone → Chinese notes All business examples
/demos/deckgl-biz-retail-column-diff.htmlLIVE
single HTML file · CDN dependencies onlyinterface labels are in Chinese

Business context

Regional managers and finance analysts use paired columns to check sales gains and losses in each trade area, identify which areas are dragging, and support monthly business reviews and budget variance explanations. Data comes from current and previous period sales aggregated over the same trade areas, with the partitioning and centroids held fixed; typically dozens to a hundred-plus areas are refreshed in fixed batches.

The two periods are aligned by trade-area ID, missing values filled with zero and flagged, and with identical centroids a fixed longitude offset is applied so paired columns do not occlude each other. Both layers declare the same elevationScale so height stays strictly proportional to value; switching views only changes visible without rebuilding data, so position and height attributes are reused.

The relation between offset and column radius is the easiest thing to get wrong: if the gap is smaller than twice the radius, two columns blur into one at oblique angles and growth versus decline becomes unreadable. Another trap is concluding from total growth alone, since the total may rise on a few large areas; per-column structure must be checked or the overall trend is misjudged.

How it works

The 100 trade-area centers are identical, with the two column sets offset 220 m in longitude, 220/(111320*cos(39.92°)) ≈ 0.00258°, and a 160 m radius leaving a 120 m gap so side-by-side columns do not occlude each other. The previous period is cool blue and the current warm orange; getElevation reads prev and cur respectively, and diskResolution stays 16.

Both layers declare the same SCALE = 420, so height stays strictly proportional to value; scaling only the current period would look like universal growth while being nothing but visual magnification. The total difference in the bar is the current sum over 100 trade areas minus the previous sum, and the side-by-side view lets each column be checked for which areas are dragging. Switching to current only sets the other layer's visible to false, rebuilding neither data nor position attributes.

ConfigurationWhat it does
220 m offsetEach column set gives up half the spacing to avoid occlusion
radius 160 m320 m diameter under 440 m spacing, leaving a 120 m gap
Shared SCALE 420Both periods on one ruler, so differences read directly
visible toggleHides one set without rebuilding data or re-uploading attributes

Note. The gap between paired columns must exceed twice the radius; otherwise the two columns merge at oblique angles and growth or decline cannot be seen at all.

Source

demos/deckgl-biz-retail-column-diff.html
<!--
  示例:两期销售对比柱
  要点:同一批商圈用两个 ColumnLayer 并排,本期与上期左右偏移 220 m、共用 elevationScale
  依赖: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 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">100</b></span>
  <span>总额差 <b id="diff">—</b></span>
  <span class="sep"></span>
  <span>视图</span>
  <button id="side" class="on">并排</button>
  <button id="curOnly">只看本期</button>
  <button id="prevOnly">只看上期</button>
  <span class="sep"></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;
  }

  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(31058);
  var STEP_LNG = 0.01231;
  var STEP_LAT = 0.00950;
  var DX = 220 / (111320 * Math.cos(39.92 * Math.PI / 180));
  var SCALE = 420;

  var cells = [];
  var sumPrev = 0;
  var sumCur = 0;

  for (var i = 0; i < 10; i++) {
    for (var j = 0; j < 10; j++) {
      var prev = 0.3 + 2.4 * Math.pow(rand(), 1.6);
      var cur = prev * (1 + (rand() * 0.42 - 0.16));
      if (cur < 0.2) cur = 0.2;
      sumPrev += prev;
      sumCur += cur;
      cells.push({
        lng: 116.40 + (i - 4.5) * STEP_LNG,
        lat: 39.92 + (j - 4.5) * STEP_LAT,
        prev: prev,
        cur: cur
      });
    }
  }

  function prevPos(d) { return [d.lng - DX, d.lat]; }
  function curPos(d) { return [d.lng + DX, d.lat]; }

  var showPrev = true;
  var showCur = true;

  function buildLayers() {
    return [
      new deck.ColumnLayer({
        id: 'columns-prev',
        data: cells,
        getPosition: prevPos,
        diskResolution: 16,
        radius: 160,
        extruded: true,
        getElevation: function (d) { return d.prev; },
        elevationScale: SCALE,
        getFillColor: [86, 150, 238, 225],
        material: { ambient: 0.5, diffuse: 0.72, shininess: 22 },
        visible: showPrev,
        pickable: true
      }),
      new deck.ColumnLayer({
        id: 'columns-cur',
        data: cells,
        getPosition: curPos,
        diskResolution: 16,
        radius: 160,
        extruded: true,
        getElevation: function (d) { return d.cur; },
        elevationScale: SCALE,
        getFillColor: [240, 110, 72, 235],
        material: { ambient: 0.5, diffuse: 0.72, shininess: 22 },
        visible: showCur,
        pickable: true
      })
    ];
  }

  var diff = sumCur - sumPrev;
  document.getElementById('diff').textContent =
    (diff >= 0 ? '+' : '') + diff.toFixed(1) + ' 亿元';

  var deckgl = new deck.Deck({
    canvas: 'cv',
    width: '100%',
    height: '100%',
    initialViewState: {
      longitude: 116.40,
      latitude: 39.92,
      zoom: 13.2,
      pitch: 55,
      bearing: -22
    },
    controller: true,
    layers: buildLayers(),
    getTooltip: function (info) {
      var o = info.object;
      if (!o) return null;
      return '本期 ' + o.cur.toFixed(2) + ' 亿 · 上期 ' + o.prev.toFixed(2) +
        ' 亿 · 差 ' + (o.cur - o.prev >= 0 ? '+' : '') +
        (o.cur - o.prev).toFixed(2);
    }
  });

  var side = document.getElementById('side');
  var curOnly = document.getElementById('curOnly');
  var prevOnly = document.getElementById('prevOnly');

  function setMode(mode) {
    showPrev = mode !== 'cur';
    showCur = mode !== 'prev';
    side.classList.toggle('on', mode === 'both');
    curOnly.classList.toggle('on', mode === 'cur');
    prevOnly.classList.toggle('on', mode === 'prev');
    deckgl.setProps({ layers: buildLayers() });
  }

  side.addEventListener('click', function () { setMode('both'); });
  curOnly.addEventListener('click', function () { setMode('cur'); });
  prevOnly.addEventListener('click', function () { setMode('prev'); });

  window.__deck = deckgl;
})();
</script>
</body>
</html>
Language: this page is in English; the demo keeps Chinese interface labels and code comments. The Chinese page carries the full notes and the same runnable file.

More business examples

deck.gl · APIColumnLayer deck.gl · APIView deck.gl · APIDeck