Skip to content

Commit 827000c

Browse files
committed
default insets for intervals
1 parent 30f6216 commit 827000c

File tree

7 files changed

+169
-183
lines changed

7 files changed

+169
-183
lines changed

src/transforms/bin.js

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
import {bin as binner, extent, thresholdFreedmanDiaconis, thresholdScott, thresholdSturges, utcTickInterval} from "d3";
22
import {valueof, range, identity, maybeLazyChannel, maybeTuple, maybeColor, maybeValue, mid, labelof, isTemporal} from "../mark.js";
3-
import {offset} from "../style.js";
43
import {basic} from "./basic.js";
54
import {maybeEvaluator, maybeGroup, maybeOutput, maybeOutputs, maybeReduce, maybeSort, maybeSubgroup, reduceCount, reduceIdentity} from "./group.js";
5+
import {maybeInsetX, maybeInsetY} from "./inset.js";
66

77
// Group on {z, fill, stroke}, then optionally on y, then bin x.
8-
export function binX(outputs = {y: "count"}, {inset, insetLeft, insetRight, ...options} = {}) {
9-
let {x, y} = options;
10-
x = maybeBinValue(x, options, identity);
11-
([insetLeft, insetRight] = maybeInset(inset, insetLeft, insetRight));
12-
return binn(x, null, null, y, outputs, {inset, insetLeft, insetRight, ...options});
8+
export function binX(outputs = {y: "count"}, options = {}) {
9+
const {x, y} = options;
10+
return binn(maybeBinValue(x, options, identity), null, null, y, outputs, maybeInsetX(options));
1311
}
1412

1513
// Group on {z, fill, stroke}, then optionally on x, then bin y.
16-
export function binY(outputs = {x: "count"}, {inset, insetTop, insetBottom, ...options} = {}) {
17-
let {x, y} = options;
18-
y = maybeBinValue(y, options, identity);
19-
([insetTop, insetBottom] = maybeInset(inset, insetTop, insetBottom));
20-
return binn(null, y, x, null, outputs, {inset, insetTop, insetBottom, ...options});
14+
export function binY(outputs = {x: "count"}, options = {}) {
15+
const {x, y} = options;
16+
return binn(null, maybeBinValue(y, options, identity), x, null, outputs, maybeInsetY(options));
2117
}
2218

2319
// Group on {z, fill, stroke}, then bin on x and y.
24-
export function bin(outputs = {fill: "count"}, {inset, insetTop, insetRight, insetBottom, insetLeft, ...options} = {}) {
20+
export function bin(outputs = {fill: "count"}, options = {}) {
2521
const {x, y} = maybeBinValueTuple(options);
26-
([insetTop, insetBottom] = maybeInset(inset, insetTop, insetBottom));
27-
([insetLeft, insetRight] = maybeInset(inset, insetLeft, insetRight));
28-
return binn(x, y, null, null, outputs, {inset, insetTop, insetRight, insetBottom, insetLeft, ...options});
22+
return binn(x, y, null, null, outputs, maybeInsetX(maybeInsetY(options)));
2923
}
3024

3125
function binn(
@@ -252,9 +246,3 @@ function binfilter([{x0, x1}, set]) {
252246
function binempty() {
253247
return new Uint32Array(0);
254248
}
255-
256-
function maybeInset(inset, inset1, inset2) {
257-
return inset === undefined && inset1 === undefined && inset2 === undefined
258-
? (offset ? [1, 0] : [0.5, 0.5])
259-
: [inset1, inset2];
260-
}

src/transforms/inset.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {offset} from "../style.js";
2+
3+
export function maybeInsetX({inset, insetLeft, insetRight, ...options} = {}) {
4+
([insetLeft, insetRight] = maybeInset(inset, insetLeft, insetRight));
5+
return {inset, insetLeft, insetRight, ...options};
6+
}
7+
8+
export function maybeInsetY({inset, insetTop, insetBottom, ...options} = {}) {
9+
([insetTop, insetBottom] = maybeInset(inset, insetTop, insetBottom));
10+
return {inset, insetTop, insetBottom, ...options};
11+
}
12+
13+
function maybeInset(inset, inset1, inset2) {
14+
return inset === undefined && inset1 === undefined && inset2 === undefined
15+
? (offset ? [1, 0] : [0.5, 0.5])
16+
: [inset1, inset2];
17+
}

src/transforms/interval.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {labelof, maybeValue, valueof} from "../mark.js";
2+
import {maybeInsetX, maybeInsetY} from "./inset.js";
23

34
// TODO Allow the interval to be specified as a string, e.g. “day” or “hour”?
45
// This will require the interval knowing the type of the associated scale to
@@ -17,25 +18,25 @@ function maybeIntervalValue(value, {interval} = {}) {
1718
return value;
1819
}
1920

20-
function maybeIntervalK(k, options = {}) {
21+
function maybeIntervalK(k, maybeInsetK, options = {}) {
2122
const {[k]: v, [`${k}1`]: v1, [`${k}2`]: v2} = options;
2223
const {value, interval} = maybeIntervalValue(v, options);
2324
if (interval == null) return options;
2425
let V1;
2526
const tv1 = data => V1 || (V1 = valueof(data, value).map(v => interval.floor(v)));
2627
const label = labelof(v);
27-
return {
28+
return maybeInsetK({
2829
...options,
2930
[k]: undefined,
3031
[`${k}1`]: v1 === undefined ? {transform: tv1, label} : v1,
3132
[`${k}2`]: v2 === undefined ? {transform: () => tv1().map(v => interval.offset(v)), label} : v2
32-
};
33+
});
3334
}
3435

3536
export function maybeIntervalX(options) {
36-
return maybeIntervalK("x", options);
37+
return maybeIntervalK("x", maybeInsetX, options);
3738
}
3839

3940
export function maybeIntervalY(options = {}) {
40-
return maybeIntervalK("y", options);
41+
return maybeIntervalK("y", maybeInsetY, options);
4142
}

test/output/aaplCloseRect.svg

Lines changed: 0 additions & 153 deletions
This file was deleted.

0 commit comments

Comments
 (0)