Skip to content

Commit 73d764e

Browse files
committed
binWidth instead of radius
1 parent 634bab9 commit 73d764e

File tree

10 files changed

+811
-641
lines changed

10 files changed

+811
-641
lines changed

src/marks/hexgrid.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ export function hexgrid(options) {
1717
}
1818

1919
export class Hexgrid extends Mark {
20-
constructor({radius = 10, clip = true, ...options} = {}) {
20+
constructor({binWidth = 20, clip = true, ...options} = {}) {
2121
super(undefined, undefined, {clip, ...options}, defaults);
22-
this.radius = number(radius);
22+
this.binWidth = number(binWidth);
2323
}
2424
render(index, scales, channels, dimensions) {
25-
const {dx, dy, radius: rx} = this;
25+
const {dx, dy, binWidth} = this;
2626
const {marginTop, marginRight, marginBottom, marginLeft, width, height} = dimensions;
2727
const x0 = marginLeft - ox, x1 = width - marginRight - ox, y0 = marginTop - oy, y1 = height - marginBottom - oy;
28-
const ry = rx * sqrt4_3, hy = ry / 2, wx = rx * 2, wy = ry * 1.5;
28+
const rx = binWidth / 2, ry = rx * sqrt4_3, hy = ry / 2, wx = rx * 2, wy = ry * 1.5;
2929
const path = `m0,${-ry}l${rx},${hy}v${ry}l${-rx},${hy}`;
3030
const i0 = Math.floor(x0 / wx), i1 = Math.ceil(x1 / wx);
3131
const j0 = Math.floor((y0 + hy) / wy), j1 = Math.ceil((y1 - hy) / wy) + 1;

src/transforms/hexbin.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import {hasOutput, maybeOutputs} from "./group.js";
1111
export const ox = 0.5, oy = 0;
1212

1313
export function hexbin(outputs = {fill: "count"}, options = {}) {
14-
const {radius, ...rest} = outputs;
15-
return hexbinn(rest, {radius, ...options});
14+
const {binWidth, ...rest} = outputs;
15+
return hexbinn(rest, {binWidth, ...options});
1616
}
1717

1818
// TODO filter e.g. to show empty hexbins?
1919
// TODO disallow x, x1, x2, y, y1, y2 reducers?
20-
function hexbinn(outputs, {radius = 10, fill, stroke, z, ...options}) {
21-
radius = +radius;
20+
function hexbinn(outputs, {binWidth = 20, fill, stroke, z, ...options}) {
21+
binWidth = +binWidth;
2222
const [GZ, setGZ] = maybeChannel(z);
2323
const [vfill] = maybeColorChannel(fill);
2424
const [vstroke] = maybeColorChannel(stroke);
@@ -31,7 +31,7 @@ function hexbinn(outputs, {radius = 10, fill, stroke, z, ...options}) {
3131
}, {fill, stroke, ...options});
3232
return {
3333
symbol: "hexagon",
34-
...!hasOutput(outputs, "r") && {r: radius},
34+
...!hasOutput(outputs, "r") && {r: binWidth / 2},
3535
...!setGF && {fill},
3636
...((hasOutput(outputs, "fill") || setGF) && stroke === undefined) ? {stroke: "none"} : {stroke},
3737
...options,
@@ -55,7 +55,7 @@ function hexbinn(outputs, {radius = 10, fill, stroke, z, ...options}) {
5555
const binFacet = [];
5656
for (const o of outputs) o.scope("facet", facet);
5757
for (const index of Z ? group(facet, i => Z[i]).values() : [facet]) {
58-
for (const bin of hbin(index, X, Y, radius)) {
58+
for (const bin of hbin(index, X, Y, binWidth)) {
5959
binFacet.push(++i);
6060
BX.push(bin.x);
6161
BY.push(bin.y);
@@ -67,7 +67,7 @@ function hexbinn(outputs, {radius = 10, fill, stroke, z, ...options}) {
6767
const channels = {
6868
x: {value: BX},
6969
y: {value: BY},
70-
...Object.fromEntries(outputs.map(({name, output}) => [name, {scale: true, radius: name === "r" ? radius : undefined, value: output.transform()}]))
70+
...Object.fromEntries(outputs.map(({name, output}) => [name, {scale: true, binWidth: name === "r" ? binWidth : undefined, value: output.transform()}]))
7171
};
7272
if ("r" in channels) {
7373
const R = channels.r.value;
@@ -78,9 +78,8 @@ function hexbinn(outputs, {radius = 10, fill, stroke, z, ...options}) {
7878
};
7979
}
8080

81-
function hbin(I, X, Y, r) {
82-
const dx = r * 2;
83-
const dy = r * sqrt3;
81+
function hbin(I, X, Y, dx) {
82+
const dy = dx * sqrt3 / 2;
8483
const bins = new Map();
8584
for (const i of I) {
8685
let px = X[i] / dx;

test/output/hexbin.svg

Lines changed: 192 additions & 192 deletions
Loading

test/output/hexbinOranges.svg

Lines changed: 151 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)