order_value, with its breakdown
What is the order_value in total, and where does it come from?
2,623 individual orders across 205 stores, each store identified only by an opaque code, with one order value per row. Stores hold anywhere from a single order to 28, and order values are heavily right-skewed: a few very large orders against a much lower median. With that many stores there is no room for a bar per store, so it suits views of the distribution such as a beeswarm, a strip plot or a box plot, and a headline total with the spread behind it.
Order Value by Store, Raw Order Rows: 2623 rows, 2 columns. Download the CSV
| store_number | order_value |
|---|---|
| 101YM3248 | 4664.31 |
| 101YM3248 | 1983.54 |
| 101YM3248 | 2192.7 |
| 101YM3248 | 1471.61 |
| 101YM3248 | 2075.21 |
| 101YM3248 | 3901.92 |
| 101YM3248 | 1786.24 |
| 101YM3248 | 393.05 |
| 108JZ8159 | 10032.14 |
| 108JZ8159 | 2520.31 |

How to read it
The headline is the total, 8.1M; the small chart beside it says where it comes from. 413LT9876 contributes the most, 1.5%.
The chart's code (32 KB of JavaScript, D3)
It carries its own copies of these helpers: llmText, llmFrameTween, llmRangeBrush, llmSlider, llmNumberEntry, llmSegmented, llmPredictor, llmResponseCurves, llmTerritories, llmCutoff, llmContrast, llmSurvival, llmModelFormat, llmSubgroups, llmSegments, llmGlyphs, llmBreadcrumb, llmKeyframes, llmScrubber, llmValueColumn, llmValueRamp, llmFitLabel, llmThinLabels, llmFitAxisTicks, llmSymlog, llmClipOutliers, llmGeoZoom, llmGeoPointNotes, llmMapLabels, llmGeoMarks, llmPlaceUnit, llmWorldFit, llmRegionFit, llmFlowKey, llmFlowArcs, llmFrameFill, llmTooltip, llmSwarm, llmKde, llmOrbit3d, llmBoundedScale, llmBivariate, llmLegendLayout, llmPager, llmTableFrame, llmGrid, llmUtcDays, llmPeriodGrid, llmTileGrid, llmMermaid, llmDecomp, llmTopN, llmFlipDeck, llmFilterScope, llmDisplayName, llmThemeColors, llmWordCloud.
// bic:keyboard-marks
d3.llmFitLabel = function (sel, widthAccessor, opts) {
var TYPE_FLOOR = 10; // the minimum readable type, px: a string is fitted at no smaller size than a chart may draw it
if (widthAccessor != null && typeof widthAccessor === 'object' && !Array.isArray(widthAccessor)) {
var wobj = widthAccessor, wopts = {};
for (var wk in wobj) if (Object.prototype.hasOwnProperty.call(wobj, wk) && wk !== 'width' && wk !== 'room') wopts[wk] = wobj[wk];
if (opts != null && typeof opts === 'object') {
for (var wk2 in opts) if (Object.prototype.hasOwnProperty.call(opts, wk2)) wopts[wk2] = opts[wk2];
} else if (typeof opts === 'number' && opts > 0 && wopts.fontSize == null) {
wopts.fontSize = Math.max(TYPE_FLOOR, opts);
}
widthAccessor = wobj.width != null ? wobj.width : wobj.room;
opts = wopts;
}
if (typeof sel === 'string' || typeof sel === 'number') {
var so = (opts != null && typeof opts === 'object') ? opts
: ((arguments[3] != null && typeof arguments[3] === 'object') ? arguments[3] : {});
var sfs = Math.max(TYPE_FLOOR, (typeof opts === 'number' && opts > 0) ? opts : (+so.fontSize > 0 ? +so.fontSize : TYPE_FLOOR));
var sopts = {};
for (var sk in so) if (Object.prototype.hasOwnProperty.call(so, sk)) sopts[sk] = so[sk];
sopts.text = String(sel);
sopts.shrinkTo = null;
var holder = null, sn = null;
try {
if (typeof document !== 'undefined' && document && document.body && document.createElementNS) {
holder = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
holder.setAttribute('width', '0'); holder.setAttribute('height', '0');
holder.style.position = 'absolute'; holder.style.left = '-10000px'; holder.style.visibility = 'hidden';
sn = document.createElementNS('http://www.w3.org/2000/svg', 'text');
holder.appendChild(sn);
document.body.appendChild(holder);
}
} catch (e) { holder = null; sn = null; }
if (!sn) {
sn = { textContent: '', style: {}, fs: sfs,
getAttribute: function (a) { return a === 'font-size' ? String(sn.fs) : null; },
setAttribute: function (a, v) { if (a === 'font-size') sn.fs = +v; } };
}
var fitted;
try {
sn.setAttribute('font-size', String(sfs));
if (so.fontFamily) sn.setAttribute('font-family', String(so.fontFamily));
if (so.fontWeight) sn.setAttribute('font-weight', String(so.fontWeight));
d3.llmFitLabel({ each: function (fn) { fn.call(sn, null, 0); return this; } }, widthAccessor, sopts);
fitted = sn.textContent == null ? '' : String(sn.textContent);
} catch (e) { fitted = String(sel); }
try { if (holder && holder.parentNode) holder.parentNode.removeChild(holder); } catch (e) { /* detached */ }
return fitted;
}
opts = opts || {};
var pad = opts.pad == null ? 4 : opts.pad;
var minWidth = opts.minWidth == null ? 14 : opts.minWidth;
var ell = opts.ellipsis == null ? '\u2026' : opts.ellipsis;
var fallbackFs = opts.fontSize == null ? 10 : opts.fontSize;
var shrinkTo = opts.shrinkTo == null ? null : +opts.shrinkTo;
var overflow = opts.overflow === 'drop' ? 'drop' : 'truncate';
var sibs = Array.isArray(opts.distinctFrom) ? opts.distinctFrom : null;
if (!sel || typeof sel.each !== 'function') return sel;
var DIGIT = /[0-9\u0660-\u0669\u06F0-\u06F9\u0966-\u096F\u09E6-\u09EF\uFF10-\uFF19]/;
var JOIN = /[.,'\u00A0\u202F\u2019\u066B\u066C]/;
var GLUED = /[A-Za-z%\u2030]/;
var LEAD = /[+\-\u2212$\u20AC\u00A3\u00A5\u20B9]/;
function numberSafeCut(s, lo) {
if (!(lo > 0) || lo >= s.length) return lo;
var a = s.charAt(lo - 1), b = s.charAt(lo);
var splits = DIGIT.test(a)
? (DIGIT.test(b) || GLUED.test(b) || (JOIN.test(b) && DIGIT.test(s.charAt(lo + 1))))
: (lo >= 2 && JOIN.test(a) && DIGIT.test(s.charAt(lo - 2)) && DIGIT.test(b));
if (!splits) return lo;
var i = lo;
while (i > 0 && (DIGIT.test(s.charAt(i - 1))
|| (i >= 2 && JOIN.test(s.charAt(i - 1)) && DIGIT.test(s.charAt(i - 2))))) i--;
while (i > 0 && LEAD.test(s.charAt(i - 1))) i--;
while (i > 0 && /\s/.test(s.charAt(i - 1))) i--;
return i;
}
function fontSizeOf(node) {
try {
var a = parseFloat(node.getAttribute && node.getAttribute('font-size'));
if (a > 0) return a;
var s = node.style && node.style.fontSize ? parseFloat(node.style.fontSize) : NaN;
if (s > 0) return s;
} catch (e) { /* fall through */ }
return fallbackFs;
}
function setFontSize(node, fs) {
try { if (node.setAttribute) node.setAttribute('font-size', String(fs)); } catch (e) { /* detached */ }
}
function measure(node, txt) {
try {
if (typeof node.getComputedTextLength === 'function') {
var w = node.getComputedTextLength();
if (w > 0) return w;
}
} catch (e) { /* fall through */ }
return String(txt).length * fontSizeOf(node) * 0.6;
}
function ownerSvg(node) {
try {
if (node.ownerSVGElement) return node.ownerSVGElement;
for (var p = node.parentNode; p; p = p.parentNode) {
if (p.nodeName && String(p.nodeName).toLowerCase() === 'svg') return p;
}
} catch (e) { /* detached */ }
return null;
}
function shownText(node) {
var kids = node && node.childNodes;
if (!kids || !kids.length) return node.textContent == null ? '' : String(node.textContent);
var s = '';
for (var c = 0; c < kids.length; c++) {
var k = kids[c];
if (k.nodeType === 3) s += k.nodeValue;
else if (k.nodeType === 1 && String(k.nodeName).toLowerCase() !== 'title') s += shownText(k);
}
return s;
}
function setShown(node, s) {
var kids = node.childNodes, keep = [];
if (kids && kids.length) {
for (var c = 0; c < kids.length; c++) {
if (kids[c].nodeType === 1 && String(kids[c].nodeName).toLowerCase() === 'title') keep.push(kids[c]);
}
}
node.textContent = s;
for (var q = 0; q < keep.length; q++) node.appendChild(keep[q]);
}
function hiddenIn(node, svg) {
try {
for (var p = node, hop = 0; p && hop < 64; p = p.parentNode, hop++) {
if (p.getAttribute && (p.getAttribute('display') === 'none' || p.getAttribute('visibility') === 'hidden')) return true;
if (p.style && (p.style.display === 'none' || p.style.visibility === 'hidden')) return true;
if (p === svg) break;
}
} catch (e) { /* read as shown */ }
return false;
}
function fittedFrom(node) {
return node.__llmFitFrom != null ? node.__llmFitFrom : node.__llmFullLabel;
}
function live(svg, en) {
if (shownText(en.node) !== en.visible) return false;
if (fittedFrom(en.node) != null && String(fittedFrom(en.node)) !== en.full) return false;
try { if (typeof svg.contains === 'function' && !svg.contains(en.node)) return false; } catch (e) { /* keep */ }
return true;
}
function endsWithText(s, t) { return s.length >= t.length && s.slice(s.length - t.length) === t; }
function ending(en, others) {
var f = en.full, n = en.node, E = en.ell;
if (en.drop) return '';
var fits = function (s) { setShown(n, s); return measure(n, s) <= en.room; };
var t = -1;
for (var s = f.length - 1; s >= 1 && t < 0; s--) {
var tl = f.slice(s), alone = true;
for (var o = 0; o < others.length && alone; o++) if (endsWithText(others[o], tl)) alone = false;
if (alone) t = s;
}
if (t < 1) return '';
if (DIGIT.test(f.charAt(t)) && (DIGIT.test(f.charAt(t - 1)) || (t >= 2 && JOIN.test(f.charAt(t - 1)) && DIGIT.test(f.charAt(t - 2))))) {
while (t > 0 && (DIGIT.test(f.charAt(t - 1)) || (t >= 2 && JOIN.test(f.charAt(t - 1)) && DIGIT.test(f.charAt(t - 2))))) t--;
}
if (t < 1) return '';
var ws = t;
while (ws > 0 && !/\s/.test(f.charAt(ws - 1))) ws--;
if (ws > 0 && ws < t && fits(E + f.slice(ws))) t = ws;
else if (!fits(E + f.slice(t))) return '';
var tail = f.slice(t), lo = 0, hi = t - 1;
while (lo < hi) {
var mid = (lo + hi + 1) >> 1;
if (fits(f.slice(0, mid) + E + tail)) lo = mid; else hi = mid - 1;
}
var head = f.slice(0, numberSafeCut(f, lo)).replace(/\s+$/, '');
if (head.length < 2) head = '';
return head + E + tail;
}
function distinguish(group) {
var g, h, s, en, others;
for (g = 0; g < group.length; g++) {
en = group[g];
others = [];
for (h = 0; h < group.length; h++) if (group[h].full !== en.full) others.push(group[h].full);
try { s = ending(en, others); } catch (e) { s = ''; }
setShown(en.node, s);
en.visible = shownText(en.node);
}
for (g = 0; g < group.length; g++) {
for (h = g + 1; h < group.length; h++) {
var a = group[g], b = group[h];
if (a.visible && a.visible === b.visible && a.full !== b.full) {
setShown(a.node, ''); a.visible = '';
setShown(b.node, ''); b.visible = '';
}
}
}
}
function clashes(reg, svg, en) {
var group = [en], differ = false;
if (hiddenIn(en.node, svg)) return group;
for (var k = reg.length - 1; k >= 0; k--) {
var o = reg[k];
if (o === en || o.visible !== en.visible) continue;
if (!live(svg, o)) { reg.splice(k, 1); continue; }
if (hiddenIn(o.node, svg)) continue;
group.push(o);
if (o.full !== en.full) differ = true;
}
return differ ? group : [en];
}
function settle(node, room) {
var svg = ownerSvg(node);
if (!svg) return;
var reg = svg.__llmFitRegistry;
if (!Array.isArray(reg)) { reg = []; svg.__llmFitRegistry = reg; }
var k;
for (k = reg.length - 1; k >= 0; k--) if (reg[k].node === node) reg.splice(k, 1);
if (reg.length >= (reg.sweepAt || 256)) {
for (k = reg.length - 1; k >= 0; k--) if (!live(svg, reg[k])) reg.splice(k, 1);
reg.sweepAt = Math.max(256, reg.length * 2);
}
var vis = shownText(node);
if (!(room > 0) || !vis) return;
var me = { node: node, full: fittedFrom(node) == null ? vis : String(fittedFrom(node)),
visible: vis, room: room, ell: ell, drop: overflow === 'drop' };
reg.push(me);
var queue = [me];
for (var round = 0; queue.length && round < 32; round++) {
var en = queue.shift();
if (!en.visible) continue;
var group = clashes(reg, svg, en);
if (group.length < 2) continue;
distinguish(group);
for (var g = 0; g < group.length; g++) if (group[g].visible) queue.push(group[g]);
}
}
function fitOne(node, d, i) {
var full, alts = [];
if (opts.text != null) {
try {
full = typeof opts.text === 'function' ? opts.text.call(node, d, i) : opts.text;
} catch (e) { full = ''; }
if (Array.isArray(full)) { alts = full.slice(1); full = full[0]; }
full = full == null ? '' : String(full);
node.__llmFullLabel = full;
node.textContent = full; // authoritative: an EMPTY label must CLEAR the node,
if (!full) return; // never leave the previous frame's text standing
} else {
full = node.__llmFullLabel;
if (full == null) {
full = node.textContent == null ? '' : String(node.textContent);
node.__llmFullLabel = full;
}
if (!full) return;
}
node.__llmFitFrom = null;
var fo = opts.forms;
if (typeof fo === 'function') { try { fo = fo.call(node, d, i); } catch (e) { fo = null; } }
if (fo != null) alts = alts.concat(Array.isArray(fo) ? fo : [fo]);
var cands = [full];
for (var ci = 0; ci < alts.length; ci++) {
var alt = alts[ci] == null ? '' : String(alts[ci]);
if (alt && cands.indexOf(alt) < 0) cands.push(alt);
}
var room;
try {
room = typeof widthAccessor === 'function'
? widthAccessor.call(node, d, i)
: widthAccessor;
} catch (e) { room = 0; }
room = (room == null ? 0 : +room) - pad * 2;
if (!(room > 0) || room < minWidth) { node.textContent = ''; return room; }
if (shrinkTo != null) {
if (node.__llmFullFontSize == null) node.__llmFullFontSize = fontSizeOf(node);
setFontSize(node, node.__llmFullFontSize);
}
node.textContent = full;
var w = measure(node, full);
if (w <= room) return room; // fits whole - done
var c;
for (c = 1; c < cands.length; c++) {
node.textContent = cands[c];
if (measure(node, cands[c]) <= room) { node.__llmFitFrom = cands[c]; return room; }
}
if (shrinkTo != null && shrinkTo > 0) {
for (c = 0; c < cands.length; c++) {
setFontSize(node, node.__llmFullFontSize);
node.textContent = cands[c];
var cur = fontSizeOf(node), wc = measure(node, cands[c]);
if (!(cur > shrinkTo)) break;
var fs = Math.max(shrinkTo, Math.floor((cur * room / wc) * 2) / 2);
for (; fs >= shrinkTo; fs -= 0.5) {
setFontSize(node, fs);
if (measure(node, cands[c]) <= room) { if (c) node.__llmFitFrom = cands[c]; return room; }
}
setFontSize(node, shrinkTo);
if (measure(node, cands[c]) <= room) { if (c) node.__llmFitFrom = cands[c]; return room; }
}
}
if (cands.length > 1) { full = cands[cands.length - 1]; node.__llmFitFrom = full; }
node.textContent = full;
if (overflow === 'drop') { node.textContent = ''; return room; }
var lo = 0, hi = full.length;
while (lo < hi) {
var mid = (lo + hi + 1) >> 1;
node.textContent = full.slice(0, mid) + ell;
if (measure(node, node.textContent) <= room) lo = mid; else hi = mid - 1;
}
lo = numberSafeCut(full, lo);
if (sibs) {
var shared = 0;
for (var si = 0; si < sibs.length; si++) {
var o = sibs[si] == null ? '' : String(sibs[si]);
if (o === full) continue;
var k = 0;
while (k < o.length && k < full.length && o.charAt(k) === full.charAt(k)) k++;
if (k > shared) shared = k;
}
if (shared > 0 && lo <= shared) {
var a = 1, b = full.length;
while (a < b) {
var m = (a + b) >> 1;
node.textContent = ell + full.slice(m);
if (measure(node, node.textContent) <= room) b = m; else a = m + 1;
}
var inNum = function (i) {
return i > 0 && i < full.length && DIGIT.test(full.charAt(i))
&& (DIGIT.test(full.charAt(i - 1)) || (i >= 2 && JOIN.test(full.charAt(i - 1)) && DIGIT.test(full.charAt(i - 2))));
};
if (inNum(a)) {
var t0 = a;
while (t0 > 0 && (DIGIT.test(full.charAt(t0 - 1)) || (t0 >= 2 && JOIN.test(full.charAt(t0 - 1)) && DIGIT.test(full.charAt(t0 - 2))))) t0--;
node.textContent = ell + full.slice(t0);
a = measure(node, node.textContent) <= room ? t0 : full.length;
}
node.textContent = a < full.length ? ell + full.slice(a) : '';
return room;
}
}
node.textContent = lo > 0 ? full.slice(0, lo) + ell : '';
return room;
}
sel.each(function (d, i) {
var room = fitOne(this, d, i);
try { settle(this, room); } catch (e) { /* a failed sibling check leaves the fit as it stands */ }
});
return sel;
};
d3.llmDisplayName = function (name) {
var s = name === null || name === undefined ? '' : String(name);
if (!s || /\s/.test(s)) return s.trim();
var t = s.replace(/[_]+/g, ' ');
if (/^[a-z0-9]+(?:-+[a-z0-9]+)+$/.test(t)) t = t.replace(/-+/g, ' ');
t = t.trim();
if (t !== s) return t.charAt(0).toUpperCase() + t.slice(1);
var parts = [];
var re = /[a-z][A-Z][a-z]+|[A-Z][a-z][A-Z](?![a-z])|[A-Z]+[0-9]+|[A-Z]+(?=[A-Z][a-z])|[A-Z]?[a-z]+|[A-Z]+|[0-9]+[A-Z](?![a-z0-9])|[0-9]+|[^A-Za-z0-9]+/g;
var m;
while ((m = re.exec(s)) !== null) {
if (m[0] === '') { re.lastIndex++; continue; }
parts.push(m[0]);
}
if (parts.length < 2 || parts.join('') !== s) return s;
var out = '';
for (var i = 0; i < parts.length; i++) {
var punct = /^[^A-Za-z0-9]+$/.test(parts[i]), prevPunct = i > 0 && /^[^A-Za-z0-9]+$/.test(parts[i - 1]);
out += i === 0 || punct || prevPunct ? parts[i] : ' ' + parts[i];
}
return out.trim();
};
d3.llmThemeColors = function (options) {
options = options || {};
var pick = function (v, d) { return (typeof v === 'string' && v && v !== 'transparent') ? v : d; };
var bg = pick(options.backgroundColor, pick(options.themeBg, '#ffffff'));
var fg = pick(options.themeFg, '#252423');
var hc = !!options.isHighContrast;
var pal = (options.palette && options.palette.length) ? options.palette.slice() : ['#4C78A8', '#F58518', '#54A24B', '#E45756', '#72B7B2', '#B279A2', '#FF9DA6', '#9D755D'];
var rgb = function (c) { var x = null; try { x = d3.rgb(c); } catch (e) { x = null; } return x && isFinite(x.r) ? x : d3.rgb(128, 128, 128); };
var lum = function (c) {
var r = rgb(c);
var f = function (v) { v /= 255; return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4); };
return 0.2126 * f(r.r) + 0.7152 * f(r.g) + 0.0722 * f(r.b);
};
var contrast = function (a, b) { var x = lum(a) + 0.05, y = lum(b) + 0.05; return x > y ? x / y : y / x; };
var legible = function (col, floor) {
floor = floor || 3;
var c = d3.lab(rgb(col));
var step = lum(bg) > 0.5 ? -3 : 3; // a light background darkens the hue, a dark one lightens it
for (var i = 0; i < 34 && contrast(c.formatHex(), bg) < floor; i++) c.l = Math.max(0, Math.min(100, c.l + step));
return c.formatHex();
};
var good = hc ? pick(options.themeAccent, fg) : legible('#2e7d32');
var bad = hc ? fg : legible('#c62828');
var slot = function (value, n) {
var h = 2166136261, text = String(value);
for (var i = 0; i < text.length; i++) { h ^= text.charCodeAt(i); h = Math.imul(h, 16777619) >>> 0; }
return h % n;
};
var deltaE2000 = function (x, y) {
var rad = Math.PI / 180, L1 = x.l, a1 = x.a, b1 = x.b, L2 = y.l, a2 = y.a, b2 = y.b;
var C1 = Math.sqrt(a1 * a1 + b1 * b1), C2 = Math.sqrt(a2 * a2 + b2 * b2), Cm = (C1 + C2) / 2;
var G = 0.5 * (1 - Math.sqrt(Math.pow(Cm, 7) / (Math.pow(Cm, 7) + Math.pow(25, 7))));
var a1p = (1 + G) * a1, a2p = (1 + G) * a2;
var C1p = Math.sqrt(a1p * a1p + b1 * b1), C2p = Math.sqrt(a2p * a2p + b2 * b2);
var hp = function (bb, ap) { if (bb === 0 && ap === 0) return 0; var hh = Math.atan2(bb, ap) / rad; return hh < 0 ? hh + 360 : hh; };
var h1p = hp(b1, a1p), h2p = hp(b2, a2p);
var dLp = L2 - L1, dCp = C2p - C1p, dhp = 0;
if (C1p * C2p !== 0) { dhp = h2p - h1p; if (dhp > 180) dhp -= 360; else if (dhp < -180) dhp += 360; }
var dHp = 2 * Math.sqrt(C1p * C2p) * Math.sin(dhp * rad / 2);
var Lm = (L1 + L2) / 2, Cmp = (C1p + C2p) / 2, hm = h1p + h2p;
if (C1p * C2p !== 0) { if (Math.abs(h1p - h2p) > 180) hm += (hm < 360 ? 360 : -360); hm /= 2; }
var T = 1 - 0.17 * Math.cos((hm - 30) * rad) + 0.24 * Math.cos(2 * hm * rad) + 0.32 * Math.cos((3 * hm + 6) * rad) - 0.2 * Math.cos((4 * hm - 63) * rad);
var dTh = 30 * Math.exp(-Math.pow((hm - 275) / 25, 2));
var Rc = 2 * Math.sqrt(Math.pow(Cmp, 7) / (Math.pow(Cmp, 7) + Math.pow(25, 7)));
var Sl = 1 + 0.015 * Math.pow(Lm - 50, 2) / Math.sqrt(20 + Math.pow(Lm - 50, 2)), Sc = 1 + 0.045 * Cmp, Sh = 1 + 0.015 * Cmp * T;
var Rt = -Math.sin(2 * dTh * rad) * Rc;
var tl = dLp / Sl, tc = dCp / Sc, th = dHp / Sh;
return Math.sqrt(tl * tl + tc * tc + th * th + Rt * tc * th);
};
var order = null, pool = null, near = null;
var growOrder = function (count) {
if (!order) {
order = pal.map(function (c) { return rgb(c).formatHex(); });
var taken = order.map(function (c) { return d3.lab(c); });
var seenHex = {};
order.forEach(function (c) { seenHex[c] = 1; });
pool = [];
pal.forEach(function (c) {
var h = d3.hcl(rgb(c));
if (!isFinite(h.h)) h.h = 0;
var chroma = Math.max(isFinite(h.c) ? h.c : 0, 35);
[h.l, 38, 52, 66].forEach(function (L) {
for (var k = 1; k < 18; k++) {
var cc = d3.hcl((h.h + k * 20) % 360, chroma, L);
while (!cc.displayable() && cc.c > 20) cc.c -= 5;
if (!cc.displayable()) continue;
var hl = d3.hsl(cc).l;
if (!(hl > 0.15 && hl < 0.85)) continue;
var hx = cc.formatHex();
if (seenHex[hx]) continue;
seenHex[hx] = 1;
pool.push({ hex: hx, lab: d3.lab(hx) });
}
});
});
near = pool.map(function (q) {
var m = Infinity;
for (var t = 0; t < taken.length; t++) m = Math.min(m, deltaE2000(q.lab, taken[t]));
return m;
});
}
while (order.length < count && pool.length) {
var best = 0;
for (var q = 1; q < pool.length; q++) if (near[q] > near[best]) best = q;
var picked = pool.splice(best, 1)[0];
near.splice(best, 1);
order.push(picked.hex);
for (var r2 = 0; r2 < pool.length; r2++) near[r2] = Math.min(near[r2], deltaE2000(pool[r2].lab, picked.lab));
}
for (var round = 1; order.length < count && round < 40; round++) {
for (var e = 0; e < pal.length && order.length < count; e++) {
var hc = d3.hsl(pal[e]);
if (!isFinite(hc.h)) hc.h = 0;
var step = Math.ceil(round / 2) * 0.12 * (round % 2 ? 1 : -1);
hc.l = Math.max(0.12, Math.min(0.88, hc.l + step));
hc.h = (hc.h + round * 7) % 360;
var cx = hc.formatHex();
if (order.indexOf(cx) < 0) order.push(cx);
}
}
};
var extended = function (count) {
if (pal.length >= count) return pal.slice();
growOrder(count);
return pal.slice().concat(order.slice(pal.length, count));
};
var memory = function () {
var m = d3.__lchCategorySlots;
if (!m || !Array.isArray(m.groups)) { m = { groups: [] }; try { d3.__lchCategorySlots = m; } catch (e) { /* a frozen d3: no memory */ } }
return m;
};
var categories = function (values) {
var seen = {}, list = [];
(values || []).forEach(function (v) { var k = v === null || v === undefined ? '' : String(v); if (!seen[k]) { seen[k] = 1; list.push(k); } });
list.sort();
var mem = memory(), group = null, best = 0;
mem.groups.forEach(function (g) {
var shared = 0;
list.forEach(function (k) { if (Object.prototype.hasOwnProperty.call(g.slots, k)) shared++; });
if (shared > best) { best = shared; group = g; }
});
if (!group) {
group = { slots: {}, used: {}, size: 0 };
mem.groups.push(group);
if (mem.groups.length > 32) mem.groups.shift();
}
var fresh = list.filter(function (k) { return !Object.prototype.hasOwnProperty.call(group.slots, k); });
var total = group.size + fresh.length;
var cols = extended(Math.max(list.length, total));
var n = cols.length;
fresh.forEach(function (k) {
if (group.size >= 2000) return; // a category this long is not a palette's job: past it, a value is placed but not remembered
var i = 0;
while (group.used[i] && i < n) i++;
group.used[i] = 1;
group.slots[k] = i;
group.size++;
});
var map = {};
list.forEach(function (k) {
var i = Object.prototype.hasOwnProperty.call(group.slots, k) ? group.slots[k] : slot(k, n);
if (i >= cols.length) cols = extended(i + 1);
map[k] = cols[i % cols.length];
});
return function (v) {
var k = v === null || v === undefined ? '' : String(v);
return map[k] || cols[slot(k, n)];
};
};
return { bg: bg, fg: fg, good: good, bad: bad, categories: categories, legible: function (c) { return legible(c); }, contrast: contrast, palette: pal };
};
const MINI_FORM = 'beeswarm';
const REFERENCE_MEASURE = null;
const STATED_SCALE_MAX = null;
const HEADLINE_MEASURE = 'order_value';
const HEADLINE_AGGREGATION = null;
const VOCAB = [['arc gauge', 72, 56], ['bullet', 90, 24], ['sparkline', 60, 24], ['radar', 110, 96], ['donut', 80, 64], ['waffle', 72, 48], ['swatch row', 60, 16], ['beeswarm', 400, 180]];
const BEESWARM_MAX = 300;
function render(container, data, options) {
container.replaceChildren();
const TYPE_FLOOR = 10;
const { columns, rows } = data, W = options.width, H = options.height;
const clampPx = (lo, want, hi) => Math.round(want < lo ? lo : want > hi ? hi : want);
const fitText = (text, maxW, lo, want, hi) => clampPx(lo, Math.min(want, maxW / Math.max(1, String(text).length * 0.62)), hi);
const fg = options.themeFg || 'currentColor', bg = options.themeBg || '#fff';
const cardBg = options.cardBackgroundColor || bg;
const T = d3.llmThemeColors(options);
const accent = options.themeAccent || T.legible('#4C78A8');
const fmt = new Intl.NumberFormat(options.cultureCode, { notation: 'compact', maximumSignificantDigits: 3 });
const fmtFull = new Intl.NumberFormat(options.cultureCode, { maximumFractionDigits: 2 });
const svg = d3.select(container).append('svg').attr('width', W).attr('height', H);
const num = v => (v == null || v === '' || !isFinite(+v)) ? null : +v;
const hI = columns.findIndex(c => c.name === HEADLINE_MEASURE);
const cI = columns.findIndex(c => c.name === 'store_number');
if (hI < 0) throw new Error('INVALID:column "' + HEADLINE_MEASURE + '" not found');
if (cI < 0) throw new Error('INVALID:column "store_number" not found');
const aggMode = (options.aggregation || '').toLowerCase();
const agg = a => { if (!a.length) return null;
if (aggMode === 'average') return d3.mean(a); if (aggMode === 'median') return d3.median(a);
if (aggMode === 'min') return d3.min(a); if (aggMode === 'max') return d3.max(a);
if (aggMode === 'count') return a.length; if (aggMode === 'distinct') return new Set(a).size;
return d3.sum(a); };
const aggWord = { average: 'Average', median: 'Median', min: 'Min', max: 'Max', count: 'Count', distinct: 'Distinct' }[aggMode] || null;
const base = d3.llmDisplayName(columns[hI].name);
const label = aggWord && !new RegExp('^' + aggWord, 'i').test(base) ? aggWord + ' ' + base : base;
const hVals = rows.map(r => num(r[hI])).filter(v => v != null);
const headline = agg(hVals);
const itemName = d3.llmDisplayName(columns[cI].name);
const isBlank = v => v == null || String(v).trim() === '';
const noCat = '(no ' + itemName + ')';
const byCat = new Map();
rows.forEach((r, i) => {
const k = isBlank(r[cI]) ? noCat : String(r[cI]);
let e = byCat.get(k); if (!e) byCat.set(k, e = { k, vals: [], idx: [] });
const v = num(r[hI]); if (v != null) e.vals.push(v);
e.idx.push(i);
});
const cats = Array.from(byCat.values()).map(e => ({ k: e.k, v: agg(e.vals), idx: e.idx }))
.filter(e => e.v != null).sort((a, b) => d3.descending(a.v, b.v));
const PAD = 8;
const HH = clampPx(36, H * 0.42, 110);
const boxW = Math.max(0, W - 2 * PAD), boxH = Math.max(0, H - HH - 2 * PAD);
const ease = (share, ceiling) => share <= ceiling ? share : Math.sqrt(share * ceiling);
const CF = Math.max(TYPE_FLOOR, Math.round(ease(Math.min(W, H) / 22, 16)));
const SUB_FS = Math.max(TYPE_FLOOR, CF - 1);
const CAP_Y = H - PAD / 2 - Math.round(SUB_FS * 0.25);
const CAP_TOP = CAP_Y - SUB_FS;
const fitLine = (t, text) => {
d3.llmFitLabel(t, W - 2 * PAD, { pad: 0, text });
if (t.text() !== text) t.append('title').text(text);
return t;
};
const fitNumberLine = (t, forms) => {
for (const f of forms) {
d3.llmFitLabel(t, W - 2 * PAD, { pad: 0, text: f, overflow: 'drop' });
if (t.text()) break;
}
if (t.text() !== forms[0]) t.append('title').text(forms[0]);
return t;
};
const feasible = {
'arc gauge': false, 'bullet': false, 'sparkline': false, 'radar': false,
'donut': cats.length >= 2 && cats.length <= 4,
'waffle': false,
'swatch row': cats.length >= 2 && cats.length <= 8,
'beeswarm': cats.length >= 9 && cats.length <= BEESWARM_MAX,
};
const fits = f => boxW >= f[1] && boxH >= f[2];
const rowOf = name => VOCAB.find(f => f[0] === name);
const implemented = { 'donut': 1, 'swatch row': 1, 'beeswarm': 1 };
const pickForm = () => {
if (MINI_FORM && feasible[MINI_FORM] && rowOf(MINI_FORM) && fits(rowOf(MINI_FORM))) return MINI_FORM;
for (const f of VOCAB) if (feasible[f[0]] && implemented[f[0]] && fits(f)) return f[0];
return null;
};
const form = pickForm();
svg.append('style').text(
'.d3-mark.lch-card{stroke-width:1;stroke-opacity:.25;transition:stroke-width .12s,stroke-opacity .12s}' +
'.d3-mark.lch-card.lch-mark-selected{stroke-width:2.5;stroke-opacity:.95}');
const allIdx = rows.map((_, i) => i).join(',');
const card = svg.append('rect').attr('class', 'd3-mark lch-card').attr('x', 0.5).attr('y', 0.5)
.attr('width', Math.max(1, W - 1)).attr('height', Math.max(1, H - 1)).attr('rx', 6)
.attr('fill', cardBg).attr('stroke', accent).style('cursor', 'pointer').attr('data-row-idx', allIdx);
card.append('title').text(label + ': ' + (headline == null ? 'no data' : fmtFull.format(headline)) +
' (' + itemName + ': ' + cats.length + ' distinct, combined in the total)');
if (!rows.length || headline == null) {
svg.append('text').attr('x', W / 2).attr('y', H / 2).attr('text-anchor', 'middle').attr('fill', fg)
.attr('font-size', CF).text((typeof options.noDataText === 'string' && options.noDataText) ? options.noDataText : 'No data to display');
return;
}
const vtext = fmt.format(headline);
const VS = Math.min(120, fitText(vtext, W - 2 * PAD, CF + 6, HH * 0.62, 120));
const LS = fitText(label, W - 2 * PAD, CF, VS * 0.36, VS);
svg.append('text').attr('class', 'lch-label').attr('x', W / 2).attr('y', PAD + LS).attr('text-anchor', 'middle')
.attr('font-size', LS).attr('fill', fg).attr('fill-opacity', 0.75).style('pointer-events', 'none').text(label);
svg.append('text').attr('class', 'lch-headline').attr('x', W / 2).attr('y', PAD + LS + 4 + VS * 0.88)
.attr('text-anchor', 'middle').attr('font-size', VS).attr('font-weight', 'bold').attr('fill', fg)
.style('pointer-events', 'none').text(vtext);
if (!form) {
const need = rowOf('beeswarm');
let note = cats.length >= 9
? 'No room for its beeswarm (needs ' + Math.max(W, need[1] + 2 * PAD) + 'x' + Math.max(H, 330) + ')'
: 'No mini fits this data (' + cats.length + ' ' + (cats.length === 1 ? 'category' : 'categories') + ')';
fitLine(svg.append('text').attr('class', 'lch-mini-note').attr('x', PAD).attr('y', CAP_Y)
.attr('font-size', SUB_FS).attr('fill', fg).attr('fill-opacity', 0.7), note);
return;
}
const minRow = rowOf(form);
const drawH = Math.max(minRow[2], Math.min(boxH, CAP_TOP - 2 - HH - PAD));
const miniW = Math.min(boxW, Math.max(minRow[1], W * 0.60));
const miniH = Math.min(drawH, Math.max(minRow[2], H * 0.40));
const g = svg.append('g').attr('class', 'lch-mini').attr('data-mini-form', form)
.attr('transform', 'translate(' + (PAD + (boxW - miniW) / 2) + ',' + (HH + PAD + (drawH - miniH) / 2) + ')');
const colour = T.categories(cats.map(c => c.k));
const subLine = () => svg.append('text').attr('class', 'lch-mini-sub').attr('x', W / 2).attr('y', CAP_Y)
.attr('text-anchor', 'middle').attr('font-size', SUB_FS).attr('fill', fg).attr('fill-opacity', 0.75);
if (form === 'donut') {
const R = Math.max(8, Math.min(miniW, miniH) / 2 - 2), cx = miniW / 2, cy = miniH / 2;
const arc = d3.arc().innerRadius(R * 0.55).outerRadius(R);
const pie = d3.pie().value(c => Math.max(0, c.v)).sort(null)(cats);
g.selectAll('path.d3-mark').data(pie).join('path').attr('class', 'd3-mark')
.attr('transform', 'translate(' + cx + ',' + cy + ')').attr('d', arc)
.attr('fill', d => T.legible(colour(d.data.k))).attr('stroke', cardBg)
.attr('data-row-idx', d => d.data.idx.join(',')).style('cursor', 'pointer')
.append('title').text(d => d.data.k + ': ' + fmtFull.format(d.data.v));
fitLine(subLine(), 'by ' + itemName + ' - ' + cats.map(c => c.k).join(', '));
return;
}
if (form === 'swatch row') {
const sw = clampPx(8, miniH * 0.8, 18), gapS = Math.max(3, sw * 0.4), step = sw + gapS;
const ox = Math.max(0, (miniW - cats.length * step + gapS) / 2), oy = (miniH - sw) / 2;
g.selectAll('rect.d3-mark').data(cats).join('rect').attr('class', 'd3-mark')
.attr('x', (c, k) => ox + k * step).attr('y', oy).attr('width', sw).attr('height', sw).attr('rx', 2)
.attr('fill', c => T.legible(colour(c.k))).attr('data-row-idx', c => c.idx.join(',')).style('cursor', 'pointer')
.append('title').text(c => c.k + ': ' + fmtFull.format(c.v));
fitLine(subLine(), 'by ' + itemName);
return;
}
// beeswarm
const axisH = Math.round(SUB_FS * 1.5);
const bandH = Math.max(8, miniH - axisH), mid = bandH / 2;
let lo = d3.min(cats, c => c.v), hi = d3.max(cats, c => c.v);
if (lo === hi) { lo -= 1; hi += 1; }
const place = r => {
const xs = d3.scaleLinear().domain([lo, hi]).range([r + 1, miniW - r - 1]);
const placed = [];
cats.slice().sort((a, b) => d3.ascending(a.v, b.v) || d3.ascending(a.k, b.k)).forEach(c => {
const x = xs(c.v);
const near = placed.filter(p => Math.abs(p.x - x) < 2 * r);
const cands = [0];
near.forEach(p => { const dy = Math.sqrt(Math.max(0, 4 * r * r - (p.x - x) * (p.x - x))) + 0.01; cands.push(p.y + dy, p.y - dy); });
cands.sort((a, b) => Math.abs(a) - Math.abs(b) || a - b);
const y = cands.find(cy => near.every(p => (p.x - x) * (p.x - x) + (p.y - cy) * (p.y - cy) >= 4 * r * r - 0.01)) ?? d3.max(cands, Math.abs);
placed.push({ c, x, y });
});
return { xs, placed, reach: d3.max(placed, p => Math.abs(p.y)) + r };
};
let r = Math.max(2, Math.min(9, Math.sqrt(miniW * bandH * 0.3 / (cats.length * Math.PI))));
let swarm = place(r);
while (swarm.reach > mid && r > 1.5) { r = Math.max(1.5, r * 0.85); swarm = place(r); }
const squeeze = swarm.reach > mid ? (mid - r) / Math.max(1e-6, swarm.reach - r) : 1;
const med = d3.median(cats, c => c.v);
g.append('line').attr('class', 'lch-bee-median').attr('x1', swarm.xs(med)).attr('x2', swarm.xs(med))
.attr('y1', 0).attr('y2', bandH).attr('stroke', fg).attr('stroke-opacity', 0.55).attr('stroke-dasharray', '4,3');
g.selectAll('circle.d3-mark').data(swarm.placed).join('circle').attr('class', 'd3-mark lch-bee')
.attr('cx', p => p.x).attr('cy', p => mid + p.y * squeeze).attr('r', r)
.attr('fill', accent).attr('fill-opacity', 0.8).attr('stroke', cardBg).attr('stroke-width', 0.5)
.attr('data-row-idx', p => p.c.idx.join(',')).style('cursor', 'pointer')
.append('title').text(p => itemName + ' ' + p.c.k + ': ' + fmtFull.format(p.c.v));
g.append('line').attr('x1', 0).attr('x2', miniW).attr('y1', bandH + 2).attr('y2', bandH + 2).attr('stroke', fg).attr('stroke-opacity', 0.25);
g.append('text').attr('x', swarm.xs(lo)).attr('y', bandH + 2 + SUB_FS).attr('text-anchor', 'start')
.attr('font-size', SUB_FS).attr('fill', fg).attr('fill-opacity', 0.75).text(fmt.format(lo));
g.append('text').attr('x', swarm.xs(hi)).attr('y', bandH + 2 + SUB_FS).attr('text-anchor', 'end')
.attr('font-size', SUB_FS).attr('fill', fg).attr('fill-opacity', 0.75).text(fmt.format(hi));
fitNumberLine(subLine(), [
'each dot is one ' + itemName + ' (' + cats.length + ') - median ' + fmt.format(med) + ' (dashed)',
'each dot is one ' + itemName + ' - median ' + fmt.format(med),
'median ' + fmt.format(med)]);
}
Keep going
This chart was made by RegiaBI from the data above. Get it for Power BI or see pricing.
New articles in your inbox
Drop your email and we'll tell you when we publish something new. No spam — unsubscribe anytime.