Skip to content

Commit 42f5d9f

Browse files
authored
better implicit bar/rect position (#1552)
1 parent 47f06e6 commit 42f5d9f

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

src/marks/bar.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import {create} from "../context.js";
22
import {Mark} from "../mark.js";
3-
import {identity, indexOf, number} from "../options.js";
3+
import {hasXY, identity, indexOf, number} from "../options.js";
44
import {isCollapsed} from "../scales.js";
5-
import {
6-
applyDirectStyles,
7-
applyIndirectStyles,
8-
applyTransform,
9-
impliedString,
10-
applyAttr,
11-
applyChannelStyles
12-
} from "../style.js";
5+
import {applyAttr, applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyTransform} from "../style.js";
6+
import {impliedString} from "../style.js";
137
import {maybeIdentityX, maybeIdentityY} from "../transforms/identity.js";
148
import {maybeIntervalX, maybeIntervalY} from "../transforms/interval.js";
159
import {maybeStackX, maybeStackY} from "../transforms/stack.js";
@@ -129,10 +123,12 @@ export class BarY extends AbstractBar {
129123
}
130124
}
131125

132-
export function barX(data, options = {y: indexOf, x2: identity}) {
126+
export function barX(data, options = {}) {
127+
if (!hasXY(options)) options = {...options, y: indexOf, x2: identity};
133128
return new BarX(data, maybeStackX(maybeIntervalX(maybeIdentityX(options))));
134129
}
135130

136-
export function barY(data, options = {x: indexOf, y2: identity}) {
131+
export function barY(data, options = {}) {
132+
if (!hasXY(options)) options = {...options, x: indexOf, y2: identity};
137133
return new BarY(data, maybeStackY(maybeIntervalY(maybeIdentityY(options))));
138134
}

src/marks/rect.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import {create} from "../context.js";
22
import {Mark} from "../mark.js";
3-
import {identity, indexOf, number} from "../options.js";
3+
import {hasXY, identity, indexOf, number} from "../options.js";
44
import {isCollapsed} from "../scales.js";
5-
import {
6-
applyDirectStyles,
7-
applyIndirectStyles,
8-
applyTransform,
9-
impliedString,
10-
applyAttr,
11-
applyChannelStyles
12-
} from "../style.js";
5+
import {applyAttr, applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyTransform} from "../style.js";
6+
import {impliedString} from "../style.js";
137
import {maybeIdentityX, maybeIdentityY} from "../transforms/identity.js";
148
import {maybeTrivialIntervalX, maybeTrivialIntervalY} from "../transforms/interval.js";
159
import {maybeStackX, maybeStackY} from "../transforms/stack.js";
@@ -103,10 +97,12 @@ export function rect(data, options) {
10397
return new Rect(data, maybeTrivialIntervalX(maybeTrivialIntervalY(options)));
10498
}
10599

106-
export function rectX(data, options = {y: indexOf, interval: 1, x2: identity}) {
100+
export function rectX(data, options = {}) {
101+
if (!hasXY(options)) options = {...options, y: indexOf, x2: identity, interval: 1};
107102
return new Rect(data, maybeStackX(maybeTrivialIntervalY(maybeIdentityX(options))));
108103
}
109104

110-
export function rectY(data, options = {x: indexOf, interval: 1, y2: identity}) {
105+
export function rectY(data, options = {}) {
106+
if (!hasXY(options)) options = {...options, x: indexOf, y2: identity, interval: 1};
111107
return new Rect(data, maybeStackY(maybeTrivialIntervalX(maybeIdentityY(options))));
112108
}

src/options.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,21 @@ export function slice(values, type = Array) {
139139
return values instanceof type ? values.slice() : type.from(values);
140140
}
141141

142+
// Returns true if any of x, x1, or x2 is not (strictly) undefined.
143+
export function hasX({x, x1, x2}) {
144+
return x !== undefined || x1 !== undefined || x2 !== undefined;
145+
}
146+
147+
// Returns true if any of y, y1, or y2 is not (strictly) undefined.
148+
export function hasY({y, y1, y2}) {
149+
return y !== undefined || y1 !== undefined || y2 !== undefined;
150+
}
151+
152+
// Returns true if has x or y, or if interval is not (strictly) undefined.
153+
export function hasXY(options) {
154+
return hasX(options) || hasY(options) || options.interval !== undefined;
155+
}
156+
142157
// Disambiguates an options object (e.g., {y: "x2"}) from a primitive value.
143158
export function isObject(option) {
144159
return option?.toString === objectToString;

src/transforms/identity.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import {identity} from "../options.js";
1+
import {hasX, hasY, identity} from "../options.js";
22

33
export function maybeIdentityX(options = {}) {
4-
const {x, x1, x2} = options;
5-
return x1 === undefined && x2 === undefined && x === undefined ? {...options, x: identity} : options;
4+
return hasX(options) ? options : {...options, x: identity};
65
}
76

87
export function maybeIdentityY(options = {}) {
9-
const {y, y1, y2} = options;
10-
return y1 === undefined && y2 === undefined && y === undefined ? {...options, y: identity} : options;
8+
return hasY(options) ? options : {...options, y: identity};
119
}

0 commit comments

Comments
 (0)