diff --git a/src/facet.js b/src/facet.js index aa6f3df7ba..85dd63457d 100644 --- a/src/facet.js +++ b/src/facet.js @@ -18,7 +18,7 @@ class Facet extends Mark { ], transform ); - this.marks = marks; + this.marks = marks.filter(d => !!d); // The following fields are set by initialize: this.marksChannels = undefined; // array of mark channels this.marksIndex = undefined; // array of mark indexes (for non-faceted marks) diff --git a/src/plot.js b/src/plot.js index cffd705753..2ddccc7e86 100644 --- a/src/plot.js +++ b/src/plot.js @@ -34,6 +34,7 @@ export function plot(options = {}) { // Initialize the marks’ channels, indexing them by mark and scale as needed. // Also apply any scale transforms. for (const mark of marks) { + if (!mark) continue; if (markChannels.has(mark)) throw new Error("duplicate mark"); const named = Object.create(null); const {index, channels} = mark.initialize(); @@ -93,6 +94,7 @@ export function plot(options = {}) { .text(`.plot text { white-space: pre; }`); for (const mark of marks) { + if (!mark) continue; const channels = markChannels.get(mark); const index = markIndex.get(mark); const node = mark.render(index, scales, channels, dimensions, axes); diff --git a/test/marks/empty-test.js b/test/marks/empty-test.js new file mode 100644 index 0000000000..35591442aa --- /dev/null +++ b/test/marks/empty-test.js @@ -0,0 +1,23 @@ +import * as Plot from "@observablehq/plot"; +import {JSDOM} from "jsdom"; +import tape from "tape-await"; + +tape("empty marks are ignored", test => { + global.document = new JSDOM("").window.document; + test.equal( + Plot.plot({ marks: [undefined, 0, null, Plot.dotX([0])] }).innerHTML, + Plot.plot({ marks: [Plot.dotX([0])] }).innerHTML + ); + const data = [1,2,3]; + test.equal( + Plot.plot({ + facet: { data, y: d => d }, + marks: [ false, Plot.dotX(data) ] + }).innerHTML, + Plot.plot({ + facet: { data, y: d => d }, + marks: [ Plot.dotX(data) ] + }).innerHTML + ); + delete global.document; +});