Skip to content

Commit

Permalink
default insets for intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Sep 23, 2021
1 parent b8261f3 commit 7afd9cd
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 183 deletions.
30 changes: 9 additions & 21 deletions src/transforms/bin.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
import {bin as binner, extent, thresholdFreedmanDiaconis, thresholdScott, thresholdSturges, utcTickInterval} from "d3";
import {valueof, range, identity, maybeLazyChannel, maybeTuple, maybeColor, maybeValue, mid, labelof, isTemporal} from "../mark.js";
import {offset} from "../style.js";
import {basic} from "./basic.js";
import {maybeEvaluator, maybeGroup, maybeOutput, maybeOutputs, maybeReduce, maybeSort, maybeSubgroup, reduceCount, reduceIdentity} from "./group.js";
import {maybeInsetX, maybeInsetY} from "./inset.js";

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

// Group on {z, fill, stroke}, then optionally on x, then bin y.
export function binY(outputs = {x: "count"}, {inset, insetTop, insetBottom, ...options} = {}) {
let {x, y} = options;
y = maybeBinValue(y, options, identity);
([insetTop, insetBottom] = maybeInset(inset, insetTop, insetBottom));
return binn(null, y, x, null, outputs, {inset, insetTop, insetBottom, ...options});
export function binY(outputs = {x: "count"}, options = {}) {
const {x, y} = options;
return binn(null, maybeBinValue(y, options, identity), x, null, outputs, maybeInsetY(options));
}

// Group on {z, fill, stroke}, then bin on x and y.
export function bin(outputs = {fill: "count"}, {inset, insetTop, insetRight, insetBottom, insetLeft, ...options} = {}) {
export function bin(outputs = {fill: "count"}, options = {}) {
const {x, y} = maybeBinValueTuple(options);
([insetTop, insetBottom] = maybeInset(inset, insetTop, insetBottom));
([insetLeft, insetRight] = maybeInset(inset, insetLeft, insetRight));
return binn(x, y, null, null, outputs, {inset, insetTop, insetRight, insetBottom, insetLeft, ...options});
return binn(x, y, null, null, outputs, maybeInsetX(maybeInsetY(options)));
}

function binn(
Expand Down Expand Up @@ -252,9 +246,3 @@ function binfilter([{x0, x1}, set]) {
function binempty() {
return new Uint32Array(0);
}

function maybeInset(inset, inset1, inset2) {
return inset === undefined && inset1 === undefined && inset2 === undefined
? (offset ? [1, 0] : [0.5, 0.5])
: [inset1, inset2];
}
17 changes: 17 additions & 0 deletions src/transforms/inset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {offset} from "../style.js";

export function maybeInsetX({inset, insetLeft, insetRight, ...options} = {}) {
([insetLeft, insetRight] = maybeInset(inset, insetLeft, insetRight));
return {inset, insetLeft, insetRight, ...options};
}

export function maybeInsetY({inset, insetTop, insetBottom, ...options} = {}) {
([insetTop, insetBottom] = maybeInset(inset, insetTop, insetBottom));
return {inset, insetTop, insetBottom, ...options};
}

function maybeInset(inset, inset1, inset2) {
return inset === undefined && inset1 === undefined && inset2 === undefined
? (offset ? [1, 0] : [0.5, 0.5])
: [inset1, inset2];
}
11 changes: 6 additions & 5 deletions src/transforms/interval.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {labelof, maybeValue, valueof} from "../mark.js";
import {maybeInsetX, maybeInsetY} from "./inset.js";

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

function maybeIntervalK(k, options = {}) {
function maybeIntervalK(k, maybeInsetK, options = {}) {
const {[k]: v, [`${k}1`]: v1, [`${k}2`]: v2} = options;
const {value, interval} = maybeIntervalValue(v, options);
if (interval == null) return options;
let V1;
const tv1 = data => V1 || (V1 = valueof(data, value).map(v => interval.floor(v)));
const label = labelof(v);
return {
return maybeInsetK({
...options,
[k]: undefined,
[`${k}1`]: v1 === undefined ? {transform: tv1, label} : v1,
[`${k}2`]: v2 === undefined ? {transform: () => tv1().map(v => interval.offset(v)), label} : v2
};
});
}

export function maybeIntervalX(options) {
return maybeIntervalK("x", options);
return maybeIntervalK("x", maybeInsetX, options);
}

export function maybeIntervalY(options = {}) {
return maybeIntervalK("y", options);
return maybeIntervalK("y", maybeInsetY, options);
}
153 changes: 0 additions & 153 deletions test/output/aaplCloseRect.svg

This file was deleted.

Loading

0 comments on commit 7afd9cd

Please sign in to comment.