Skip to content

Commit dee5b37

Browse files
committed
maybeTuple
1 parent f8fd2e5 commit dee5b37

File tree

6 files changed

+21
-11
lines changed

6 files changed

+21
-11
lines changed

src/mark.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ export function maybeZero(x, x1, x2, x3 = identity) {
131131
return [x1, x2];
132132
}
133133

134+
// For marks that have x and y channels (e.g., cell, dot, line, text).
135+
export function maybeTuple(x, y) {
136+
return x === undefined && y === undefined ? [first, second] : [x, y];
137+
}
138+
134139
// A helper for extracting the z channel, if it is variable. Used by transforms
135140
// that require series, such as moving average and normalize.
136141
export function maybeZ({z, fill, stroke} = {}) {

src/marks/cell.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {identity, first, second} from "../mark.js";
1+
import {identity, maybeTuple} from "../mark.js";
22
import {AbstractBar} from "./bar.js";
33

44
export class Cell extends AbstractBar {
@@ -20,7 +20,8 @@ export class Cell extends AbstractBar {
2020
}
2121
}
2222

23-
export function cell(data, {x = first, y = second, ...options} = {}) {
23+
export function cell(data, {x, y, ...options} = {}) {
24+
([x, y] = maybeTuple(x, y));
2425
return new Cell(data, {...options, x, y});
2526
}
2627

src/marks/dot.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {ascending} from "d3";
22
import {create} from "d3";
33
import {filter, positive} from "../defined.js";
4-
import {Mark, identity, first, second, maybeColor, maybeNumber, title} from "../mark.js";
4+
import {Mark, identity, maybeColor, maybeNumber, maybeTuple, title} from "../mark.js";
55
import {Style, applyDirectStyles, applyIndirectStyles, applyTransform} from "../style.js";
66

77
export class Dot extends Mark {
@@ -68,7 +68,8 @@ export class Dot extends Mark {
6868
}
6969
}
7070

71-
export function dot(data, {x = first, y = second, ...options} = {}) {
71+
export function dot(data, {x, y, ...options} = {}) {
72+
([x, y] = maybeTuple(x, y));
7273
return new Dot(data, {...options, x, y});
7374
}
7475

src/marks/line.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {create} from "d3";
33
import {line as shapeLine} from "d3";
44
import {Curve} from "../curve.js";
55
import {defined} from "../defined.js";
6-
import {Mark, indexOf, identity, first, second, maybeColor, titleGroup} from "../mark.js";
6+
import {Mark, indexOf, identity, maybeColor, maybeTuple, titleGroup} from "../mark.js";
77
import {Style, applyDirectStyles, applyIndirectStyles, applyTransform} from "../style.js";
88

99
export class Line extends Mark {
@@ -66,7 +66,8 @@ export class Line extends Mark {
6666
}
6767
}
6868

69-
export function line(data, {x = first, y = second, ...options} = {}) {
69+
export function line(data, {x, y, ...options} = {}) {
70+
([x, y] = maybeTuple(x, y));
7071
return new Line(data, {...options, x, y});
7172
}
7273

src/marks/text.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {ascending} from "d3";
22
import {create} from "d3";
33
import {filter, nonempty} from "../defined.js";
4-
import {Mark, indexOf, identity, string, title, maybeColor, first, second} from "../mark.js";
4+
import {Mark, indexOf, identity, string, title, maybeColor, maybeTuple} from "../mark.js";
55
import {Style, applyDirectStyles, applyIndirectStyles, applyAttr, applyStyle, applyTransform} from "../style.js";
66

77
export class Text extends Mark {
@@ -72,7 +72,8 @@ export class Text extends Mark {
7272
}
7373
}
7474

75-
export function text(data, {x = first, y = second, ...options} = {}) {
75+
export function text(data, {x, y, ...options} = {}) {
76+
([x, y] = maybeTuple(x, y));
7677
return new Text(data, {...options, x, y});
7778
}
7879

src/transforms/group.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {group as grouper, sort, InternSet} from "d3";
22
import {defined} from "../defined.js";
3-
import {valueof, maybeColor, maybeTransform, maybeValue, maybeLazyChannel, lazyChannel, first, second, identity, take} from "../mark.js";
3+
import {valueof, maybeColor, maybeTransform, maybeValue, maybeLazyChannel, lazyChannel, first, identity, take, maybeTuple} from "../mark.js";
44

55
export function groupX({x, ...options} = {}) {
66
const [transform, X, y, z, fill, stroke] = group1(x, options);
@@ -84,8 +84,9 @@ function group1(x = identity, {domain, normalize, ...options} = {}) {
8484

8585
function group2(xv, yv, {domain, normalize, ...options} = {}) {
8686
const {z, fill, stroke} = options;
87-
const {value: x = first, domain: xdomain} = {domain, ...maybeValue(xv)};
88-
const {value: y = second, domain: ydomain} = {domain, ...maybeValue(yv)};
87+
let {value: x, domain: xdomain} = {domain, ...maybeValue(xv)};
88+
let {value: y, domain: ydomain} = {domain, ...maybeValue(yv)};
89+
([x, y] = maybeTuple(x, y));
8990
const k = normalize === true ? 100 : +normalize;
9091
const [X, setX] = lazyChannel(x);
9192
const [Y, setY] = lazyChannel(y);

0 commit comments

Comments
 (0)