Skip to content

Commit cc8fec2

Browse files
mbostockFil
andauthored
Better one-dimensional cell defaults (#347)
* Update README * better one-dimensional cell defaults * fix tests * no default identity fill if stroke channel Co-authored-by: Philippe Rivière <fil@rezo.net>
1 parent 2265ae7 commit cc8fec2

File tree

4 files changed

+40
-38
lines changed

4 files changed

+40
-38
lines changed

src/marks/cell.js

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

44
export class Cell extends AbstractBar {
@@ -25,10 +25,12 @@ export function cell(data, {x, y, ...options} = {}) {
2525
return new Cell(data, {...options, x, y});
2626
}
2727

28-
export function cellX(data, {x = identity, ...options} = {}) {
29-
return new Cell(data, {...options, x});
28+
export function cellX(data, {x = indexOf, fill, stroke, ...options} = {}) {
29+
if (fill === undefined && maybeColor(stroke)[0] === undefined) fill = identity;
30+
return new Cell(data, {...options, x, fill, stroke});
3031
}
3132

32-
export function cellY(data, {y = identity, ...options} = {}) {
33-
return new Cell(data, {...options, y});
33+
export function cellY(data, {y = indexOf, fill, stroke, ...options} = {}) {
34+
if (fill === undefined && maybeColor(stroke)[0] === undefined) fill = identity;
35+
return new Cell(data, {...options, y, fill, stroke});
3436
}

test/marks/cell-test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,18 @@ tape("cellX() defaults x to identity and y to null", test => {
7373
const cell = Plot.cellX();
7474
test.strictEqual(cell.data, undefined);
7575
test.strictEqual(cell.transform, undefined);
76-
test.deepEqual(cell.channels.map(c => c.name), ["x"]);
77-
test.deepEqual(cell.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[1, 2, 3]]);
78-
test.deepEqual(cell.channels.map(c => c.scale), ["x"]);
76+
test.deepEqual(cell.channels.map(c => c.name), ["x", "fill"]);
77+
test.deepEqual(cell.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[ 0, 1, 2 ], [ 1, 2, 3 ]]);
78+
test.deepEqual(cell.channels.map(c => c.scale), ["x", "color"]);
7979
test.strictEqual(cell.channels.find(c => c.name === "x").type, "band");
8080
});
8181

8282
tape("cellY() defaults y to identity and x to null", test => {
8383
const cell = Plot.cellY();
8484
test.strictEqual(cell.data, undefined);
8585
test.strictEqual(cell.transform, undefined);
86-
test.deepEqual(cell.channels.map(c => c.name), ["y"]);
87-
test.deepEqual(cell.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[1, 2, 3]]);
88-
test.deepEqual(cell.channels.map(c => c.scale), ["y"]);
86+
test.deepEqual(cell.channels.map(c => c.name), ["y", "fill"]);
87+
test.deepEqual(cell.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[ 0, 1, 2 ], [ 1, 2, 3 ]]);
88+
test.deepEqual(cell.channels.map(c => c.scale), ["y", "color"]);
8989
test.strictEqual(cell.channels.find(c => c.name === "y").type, "band");
9090
});

test/output/highCardinalityOrdinal.svg

Lines changed: 26 additions & 26 deletions
Loading

test/plots/high-cardinality-ordinal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default async function() {
66
type: "ordinal"
77
},
88
marks: [
9-
Plot.cellX("ABCDEFGHIJKLMNOPQRSTUVWXYZ", {fill: d => d})
9+
Plot.cellX("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
1010
]
1111
});
1212
}

0 commit comments

Comments
 (0)