Skip to content

Warn users when facet data doesn't match mark data #1014

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

Merged
merged 13 commits into from
Jul 28, 2022
Merged
17 changes: 15 additions & 2 deletions src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {position, registry as scaleRegistry} from "./scales/index.js";
import {applyInlineStyles, maybeClassName, maybeClip, styles} from "./style.js";
import {basic, initializer} from "./transforms/basic.js";
import {maybeInterval} from "./transforms/interval.js";
import {consumeWarnings} from "./warnings.js";
import {consumeWarnings, warn} from "./warnings.js";

export function plot(options = {}) {
const {facet, style, caption, ariaLabel, ariaDescription} = options;
Expand Down Expand Up @@ -59,10 +59,11 @@ export function plot(options = {}) {
let facetChannels; // e.g. {fx: {value}, fy: {value}}
let facetsIndex; // nested array of facet indexes [[0, 1, 3, …], [2, 5, …], …]
let facetsExclude; // lazily-constructed opposite of facetsIndex
let facetData;
if (facet !== undefined) {
const {x, y} = facet;
if (x != null || y != null) {
const facetData = arrayify(facet.data);
facetData = arrayify(facet.data);
if (facetData == null) throw new Error("missing facet data");
facetChannels = {};
if (x != null) {
Expand Down Expand Up @@ -99,6 +100,18 @@ export function plot(options = {}) {
const {data, facets, channels} = mark.initialize(markFacets, facetChannels);
applyScaleTransforms(channels, options);
stateByMark.set(mark, {data, facets, channels});

// Warn for the common pitfall of wanting to facet mapped data.
if (
facetIndex?.length > 1 && // non-trivial faceting
mark.facet === "auto" && // no explicit mark facet option
mark.data !== facet.data && // mark not implicitly faceted (different data)
arrayify(mark.data)?.length === facetData.length // mark data seems parallel to facet data
) {
warn(
`Warning: the ${mark.ariaLabel} mark appears to use faceted data, but isn’t faceted. The mark data has the same length as the facet data and the mark facet option is "auto", but the mark data and facet data are distinct. If this mark should be faceted, set the mark facet option to true; otherwise, suppress this warning by setting the mark facet option to false.`
);
}
}

// Initalize the scales and axes.
Expand Down
200 changes: 200 additions & 0 deletions test/output/facetWarning.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions test/plots/facet-warning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";

export default async function () {
const anscombe = await d3.csv("data/anscombe.csv", d3.autoType);
return Plot.plot({
grid: true,
inset: 10,
width: 960,
height: 240,
facet: {
data: anscombe.filter((_, i) => i % 2),
x: "series"
},
marks: [
Plot.frame(),
Plot.dot(
anscombe.filter((_, i) => i % 2),
{x: "x", y: "y" /* , facet: true */}
)
]
});
}
1 change: 1 addition & 0 deletions test/plots/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export {default as empty} from "./empty.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";
export {default as facetWarning} from "./facet-warning.js";
export {default as faithfulDensity} from "./faithful-density.js";
export {default as faithfulDensity1d} from "./faithful-density-1d.js";
export {default as figcaption} from "./figcaption.js";
Expand Down