Skip to content

Commit 05cd55e

Browse files
committed
fix the dx/dy mixup
simpler _store simpler w0
1 parent 2dae268 commit 05cd55e

File tree

5 files changed

+532
-557
lines changed

5 files changed

+532
-557
lines changed

src/transforms/hexbin.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ const defaults = {
1313
};
1414

1515
// width factor (allows the hexbin transform to work with circular dots!)
16-
const w0 = 1 / Math.sin(Math.PI / 3);
16+
const w0 = Math.sin(Math.PI / 3);
1717

1818
function hbin(I, X, Y, r) {
19-
const dx = r * 2 / w0;
19+
const dx = r * 2 * w0;
2020
const dy = r * 1.5;
21+
const keys = new Map();
2122
return groups(I, i => {
22-
let px = X[i] / dy;
23-
let py = Y[i] / dx;
23+
let px = X[i] / dx;
24+
let py = Y[i] / dy;
2425
if (isNaN(px) || isNaN(py)) return;
2526
let pj = Math.round(py),
26-
pi = Math.round(px - (pj & 1) / 2),
27-
py1 = py - pj;
27+
pi = Math.round(px = px - (pj & 1) / 2),
28+
py1 = py - pj;
2829
if (Math.abs(py1) * 3 > 1) {
2930
let px1 = px - pi,
3031
pi2 = pi + (px < pi ? -1 : 1) / 2,
@@ -33,30 +34,32 @@ function hbin(I, X, Y, r) {
3334
py2 = py - pj2;
3435
if (px1 * px1 + py1 * py1 > px2 * px2 + py2 * py2) pi = pi2 + (pj & 1 ? 1 : -1) / 2, pj = pj2;
3536
}
36-
return `${pi}|${pj}`;
37+
const key = `${pi}|${pj}`;
38+
keys.set(key, [pi, pj]);
39+
return key;
3740
})
3841
.filter(([p]) => p)
3942
.map(([p, bin]) => {
40-
const [pi, pj] = p.split("|");
41-
bin.x = (+pi + (pj & 1) / 2) * dx;
42-
bin.y = +pj * dy;
43+
const [pi, pj] = keys.get(p);
44+
bin.x = (pi + (pj & 1) / 2) * dx;
45+
bin.y = pj * dy;
4346
return bin;
4447
});
4548
}
4649

47-
function hexbinLayout({_store, radius, r: _r, opacity: _o, fill: _f, text: _t, title: _l, value: _v}, options) {
50+
function hexbinLayout(_store, {radius, r: _r, opacity: _o, fill: _f, text: _t, title: _l, value: _v}, options) {
4851
radius = +radius;
4952
_r = !!_r;
5053
_o = !!_o;
5154
_f = !!_f;
52-
return layout({_store, ...options}, function(I, {r, color, opacity}, {x: X, y: Y}) {
55+
return layout(options, function(I, {r, color, opacity}, {x: X, y: Y}) {
5356
if (!_store.channels) {
5457
const bins = _store.facets.map(I => hbin(I, X, Y, radius));
5558
const values = bins.map(b => valueof(b, _v));
5659
const maxValue = max(values.flat());
5760
color = _f && color.copy().domain([1, maxValue]);
5861
opacity = _o && opacity.copy().domain([1, maxValue]);
59-
r = _r && r && r.copy().domain([0, maxValue]).range([0, radius / w0]);
62+
r = _r && r && r.copy().domain([0, maxValue]).range([0, radius * w0]);
6063
const {data} = this;
6164
_store.channels = Array.from(bins, (bin, i) => ({
6265
x: valueof(bin, "x"),
@@ -87,7 +90,7 @@ function hexbinLayout({_store, radius, r: _r, opacity: _o, fill: _f, text: _t, t
8790
// going to splice them
8891
export function hexbinTransform(hexbinOptions, options) {
8992
const _store = {};
90-
return basic(hexbinLayout({_store, ...hexbinOptions}, options), (data, facets) => {
93+
return basic(hexbinLayout(_store, hexbinOptions, options), (data, facets) => {
9194
facets = Array.from(facets, facet => Array.from(facet));
9295
_store.facets = facets;
9396
return {data, facets};

0 commit comments

Comments
 (0)