Skip to content

Commit

Permalink
[] is an empty facet (observablehq#1216)
Browse files Browse the repository at this point in the history
* [] is an empty facet

closes observablehq#1212
alternative to observablehq#1215

* consistent filtering

Co-authored-by: Mike Bostock <[email protected]>
  • Loading branch information
2 people authored and chaichontat committed Jan 14, 2024
1 parent d5be54e commit 721df8e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ export function plot(options = {}) {
if (facets) {
const fi = facetPosition.get(key);
facet = facets[fi] ?? facets[0];
if (!facet) continue;
facet = mark.filter(facet, channels, values);
if (!facet.length) continue;
facet.fi = fi;
}
const node = mark.render(facet, scales, values, subdimensions, context);
Expand All @@ -317,8 +317,8 @@ export function plot(options = {}) {
let facet = null;
if (facets) {
facet = facets[0];
if (!facet) continue;
facet = mark.filter(facet, channels, values);
if (!facet.length) continue;
}
const node = mark.render(facet, scales, values, dimensions, context);
if (node != null) svg.appendChild(node);
Expand Down Expand Up @@ -656,8 +656,8 @@ function maybeMarkFacet(mark, topFacetState, options) {
// Facet filter, by mark; for now only the "eq" filter is provided.
function filterFacets(facets, {channels: {fx, fy}, groups}) {
return fx && fy
? facets.map(({x, y}) => groups.get(x)?.get(y))
? facets.map(({x, y}) => groups.get(x)?.get(y) ?? [])
: fx
? facets.map(({x}) => groups.get(x))
: facets.map(({y}) => groups.get(y));
? facets.map(({x}) => groups.get(x) ?? [])
: facets.map(({y}) => groups.get(y) ?? []);
}
4 changes: 1 addition & 3 deletions test/output/athletesSample.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions test/output/athletesSportWeight.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions test/output/emptyFacet.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: 0 additions & 2 deletions test/output/projectionFitAntarctica.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion test/output/shorthandBoxX.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/plots/empty-facet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as Plot from "@observablehq/plot";

export default async function () {
const data = [
{PERIOD: 1, VALUE: 3, TYPE: "c"},
{PERIOD: 2, VALUE: 4, TYPE: "c"}
];
return Plot.plot({
facet: {data, x: "TYPE"},
fx: {domain: ["a", "b"]},
marks: [Plot.barY(data, {x: "PERIOD", y: "VALUE"})]
});
}
1 change: 1 addition & 0 deletions test/plots/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export {default as downloads} from "./downloads.js";
export {default as downloadsOrdinal} from "./downloads-ordinal.js";
export {default as driving} from "./driving.js";
export {default as empty} from "./empty.js";
export {default as emptyFacet} from "./empty-facet.js";
export {default as emptyLegend} from "./empty-legend.js";
export {default as emptyX} from "./empty-x.js";
export {default as energyProduction} from "./energy-production.js";
Expand Down

0 comments on commit 721df8e

Please sign in to comment.