Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better one-dimensional cell defaults #347

Merged
merged 4 commits into from
May 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ The following *facet* options are supported:
* facet.**marginLeft** -
* facet.**grid** -

TODO Describe how data is faceted according to strict equality (`===`): if a mark’s data is strictly equal to the facet data, the mark is faceted; otherwise, the mark is repeated for each facet. You can disable faceting for a specific mark by giving it a shallow copy of the data.

## Marks

Marks visualize data as geometric shapes such as bars, dots, and lines. An single mark can generate multiple shapes: for example, passing a [Plot.barY](#plotbarydata-options) to [Plot.plot](#plotplotoptions) will produce a bar for each element in the associated data. Multiple marks can be layered into plots.
Expand Down
12 changes: 7 additions & 5 deletions src/marks/cell.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {identity, maybeTuple} from "../mark.js";
import {identity, indexOf, maybeColor, maybeTuple} from "../mark.js";
import {AbstractBar} from "./bar.js";

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

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

export function cellY(data, {y = identity, ...options} = {}) {
return new Cell(data, {...options, y});
export function cellY(data, {y = indexOf, fill, stroke, ...options} = {}) {
if (fill === undefined && maybeColor(stroke)[0] === undefined) fill = identity;
return new Cell(data, {...options, y, fill, stroke});
}
12 changes: 6 additions & 6 deletions test/marks/cell-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ tape("cellX() defaults x to identity and y to null", test => {
const cell = Plot.cellX();
test.strictEqual(cell.data, undefined);
test.strictEqual(cell.transform, undefined);
test.deepEqual(cell.channels.map(c => c.name), ["x"]);
test.deepEqual(cell.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[1, 2, 3]]);
test.deepEqual(cell.channels.map(c => c.scale), ["x"]);
test.deepEqual(cell.channels.map(c => c.name), ["x", "fill"]);
test.deepEqual(cell.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[ 0, 1, 2 ], [ 1, 2, 3 ]]);
test.deepEqual(cell.channels.map(c => c.scale), ["x", "color"]);
test.strictEqual(cell.channels.find(c => c.name === "x").type, "band");
});

tape("cellY() defaults y to identity and x to null", test => {
const cell = Plot.cellY();
test.strictEqual(cell.data, undefined);
test.strictEqual(cell.transform, undefined);
test.deepEqual(cell.channels.map(c => c.name), ["y"]);
test.deepEqual(cell.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[1, 2, 3]]);
test.deepEqual(cell.channels.map(c => c.scale), ["y"]);
test.deepEqual(cell.channels.map(c => c.name), ["y", "fill"]);
test.deepEqual(cell.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[ 0, 1, 2 ], [ 1, 2, 3 ]]);
test.deepEqual(cell.channels.map(c => c.scale), ["y", "color"]);
test.strictEqual(cell.channels.find(c => c.name === "y").type, "band");
});
52 changes: 26 additions & 26 deletions test/output/highCardinalityOrdinal.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plots/high-cardinality-ordinal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default async function() {
type: "ordinal"
},
marks: [
Plot.cellX("ABCDEFGHIJKLMNOPQRSTUVWXYZ", {fill: d => d})
Plot.cellX("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
]
});
}