Skip to content

Commit eebd122

Browse files
committed
labelof facets
1 parent f9b7947 commit eebd122

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

src/facet.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {keyword, isObject, isTypedArray, range, slice, valueof} from "./options.js";
1+
import {keyword, isObject, isTypedArray, labelof, range, slice, valueof} from "./options.js";
22
import {warn} from "./warnings.js";
33

44
// facet filter, by mark
@@ -21,9 +21,18 @@ export function maybeFacet(facet, data) {
2121
// local facets can be defined as facet: {x: accessor, xFilter: "lte"}
2222
if (!isObject(facet)) throw new Error(`Unsupported facet ${facet}`);
2323
const {x, xFilter = "eq", y, yFilter = "eq"} = facet;
24+
let xv, yv;
25+
if (x !== undefined) {
26+
xv = valueof(data, x);
27+
if (xv != null) xv.label = labelof(x);
28+
}
29+
if (y !== undefined) {
30+
yv = valueof(data, y);
31+
if (yv != null) yv.label = labelof(y);
32+
}
2433
return {
25-
...(x !== undefined && {x: valueof(data, x)}),
26-
...(y !== undefined && {y: valueof(data, y)}),
34+
...(xv && {x: xv}),
35+
...(yv && {y: yv}),
2736
xFilter: maybeFacetFilter(xFilter, "x"),
2837
yFilter: maybeFacetFilter(yFilter, "y")
2938
};

test/output/penguinFacetAnnotated.svg

Lines changed: 2 additions & 2 deletions
Loading

test/output/penguinFacetAnnotatedX.svg

Lines changed: 2 additions & 2 deletions
Loading

test/plots/walmart-expansion-bar.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import * as d3 from "d3";
44
const parse = d3.utcParse("%m/%d/%Y");
55

66
export default async function () {
7-
const walmart = await d3.tsv("data/walmart.tsv", (d) => ({date: parse(d.date)}));
7+
const walmart = await d3.tsv("data/walmart.tsv", (d) => ({
8+
date: parse(d.date),
9+
year: parse(d.date).getUTCFullYear()
10+
}));
811
return Plot.plot({
912
width: 300,
10-
facet: {
11-
data: walmart,
12-
y: (d) => d.date.getUTCFullYear(),
13-
inset: 0
14-
},
13+
facet: {inset: 0, label: null},
1514
x: {percent: true},
1615
fy: {tickFormat: "d"},
1716
marks: [
@@ -20,8 +19,8 @@ export default async function () {
2019
Plot.groupZ(
2120
{x: "count"},
2221
{
23-
fill: (d) => d.date.getUTCFullYear(),
24-
facet: {y: (d) => d.date.getUTCFullYear(), yFilter: "lte"},
22+
fill: "year",
23+
facet: {y: "year", yFilter: "lte"},
2524
stroke: "white",
2625
offset: "expand"
2726
}

0 commit comments

Comments
 (0)