Skip to content

empty facets #516

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 7 commits into from
Aug 20, 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
16 changes: 9 additions & 7 deletions src/axis.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {axisTop, axisBottom, axisRight, axisLeft, create, format, utcFormat} from "d3";
import {formatIsoDate} from "./format.js";
import {boolean, number, string, keyword, maybeKeyword, constant, isTemporal} from "./mark.js";
import {boolean, take, number, string, keyword, maybeKeyword, constant, isTemporal} from "./mark.js";

export class AxisX {
constructor({
Expand Down Expand Up @@ -67,7 +67,7 @@ export class AxisX {
.attr("font-family", null)
.call(!line ? g => g.select(".domain").remove() : () => {})
.call(!grid ? () => {}
: fy ? gridFacetX(fy, -ty)
: fy ? gridFacetX(index, fy, -ty)
: gridX(offsetSign * (marginBottom + marginTop - height)))
.call(!label ? () => {} : g => g.append("text")
.attr("fill", "currentColor")
Expand Down Expand Up @@ -148,7 +148,7 @@ export class AxisY {
.attr("font-family", null)
.call(!line ? g => g.select(".domain").remove() : () => {})
.call(!grid ? () => {}
: fx ? gridFacetY(fx, -tx)
: fx ? gridFacetY(index, fx, -tx)
: gridY(offsetSign * (marginLeft + marginRight - width)))
.call(!label ? () => {} : g => g.append("text")
.attr("fill", "currentColor")
Expand Down Expand Up @@ -182,22 +182,24 @@ function gridY(x2) {
.attr("x2", x2);
}

function gridFacetX(fy, ty) {
function gridFacetX(index, fy, ty) {
const dy = fy.bandwidth();
const domain = fy.domain();
return g => g.selectAll(".tick")
.append("path")
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.attr("d", fy.domain().map(v => `M0,${fy(v) + ty}v${dy}`).join(""));
.attr("d", (index ? take(domain, index) : domain).map(v => `M0,${fy(v) + ty}v${dy}`).join(""));
}

function gridFacetY(fx, tx) {
function gridFacetY(index, fx, tx) {
const dx = fx.bandwidth();
const domain = fx.domain();
return g => g.selectAll(".tick")
.append("path")
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.attr("d", fx.domain().map(v => `M${fx(v) + tx},0h${dx}`).join(""));
.attr("d", (index ? take(domain, index) : domain).map(v => `M${fx(v) + tx},0h${dx}`).join(""));
}

function createAxis(axis, scale, {ticks, tickSize, tickPadding, tickFormat}) {
Expand Down
38 changes: 22 additions & 16 deletions src/facet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {cross, difference, groups, InternMap} from "d3";
import {create} from "d3";
import {Mark, first, second, markify} from "./mark.js";
import {Mark, first, second, markify, where} from "./mark.js";
import {applyScales} from "./scales.js";
import {filterStyles} from "./style.js";

Expand All @@ -24,7 +24,6 @@ class Facet extends Mark {
this.marks = marks.flat(Infinity).map(markify);
// 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)
this.marksIndexByFacet = undefined; // map from facet key to array of mark indexes
}
initialize() {
Expand All @@ -34,7 +33,6 @@ class Facet extends Mark {
const facetsIndex = Array.from(facets, second);
const subchannels = [];
const marksChannels = this.marksChannels = [];
const marksIndex = this.marksIndex = new Array(this.marks.length);
const marksIndexByFacet = this.marksIndexByFacet = facetMap(channels);
for (const facetKey of facetsKeys) {
marksIndexByFacet.set(facetKey, new Array(this.marks.length));
Expand All @@ -57,12 +55,10 @@ class Facet extends Mark {
for (let j = 0; j < facetsKeys.length; ++j) {
marksIndexByFacet.get(facetsKeys[j])[i] = I[j];
}
marksIndex[i] = []; // implicit empty index for sparse facets
} else {
for (let j = 0; j < facetsKeys.length; ++j) {
marksIndexByFacet.get(facetsKeys[j])[i] = I;
}
marksIndex[i] = I;
}
}
for (const [, channel] of markChannels) {
Expand All @@ -73,44 +69,54 @@ class Facet extends Mark {
return {index, channels: [...channels, ...subchannels]};
}
render(I, scales, channels, dimensions, axes) {
const {marks, marksChannels, marksIndex, marksIndexByFacet} = this;
const {marks, marksChannels, marksIndexByFacet} = this;
const {fx, fy} = scales;
const fyDomain = fy && fy.domain();
const fxDomain = fx && fx.domain();
const fyMargins = fy && {marginTop: 0, marginBottom: 0, height: fy.bandwidth()};
const fxMargins = fx && {marginRight: 0, marginLeft: 0, width: fx.bandwidth()};
const subdimensions = {...dimensions, ...fxMargins, ...fyMargins};
const marksValues = marksChannels.map(channels => applyScales(channels, scales));
return create("svg:g")
.call(g => {
if (fy && axes.y) {
const domain = fy.domain();
const axis1 = axes.y, axis2 = nolabel(axis1);
const j = axis1.labelAnchor === "bottom" ? domain.length - 1 : axis1.labelAnchor === "center" ? domain.length >> 1 : 0;
const j = axis1.labelAnchor === "bottom" ? fyDomain.length - 1 : axis1.labelAnchor === "center" ? fyDomain.length >> 1 : 0;
const fyDimensions = {...dimensions, ...fyMargins};
g.selectAll()
.data(domain)
.data(fyDomain)
.join("g")
.attr("transform", ky => `translate(0,${fy(ky)})`)
.append((_, i) => (i === j ? axis1 : axis2).render(null, scales, null, fyDimensions));
.append((ky, i) => (i === j ? axis1 : axis2).render(
fx && where(fxDomain, kx => marksIndexByFacet.has([kx, ky])),
scales,
null,
fyDimensions
));
}
if (fx && axes.x) {
const domain = fx.domain();
const axis1 = axes.x, axis2 = nolabel(axis1);
const j = axis1.labelAnchor === "right" ? domain.length - 1 : axis1.labelAnchor === "center" ? domain.length >> 1 : 0;
const j = axis1.labelAnchor === "right" ? fxDomain.length - 1 : axis1.labelAnchor === "center" ? fxDomain.length >> 1 : 0;
const {marginLeft, marginRight} = dimensions;
const fxDimensions = {...dimensions, ...fxMargins, labelMarginLeft: marginLeft, labelMarginRight: marginRight};
g.selectAll()
.data(domain)
.data(fxDomain)
.join("g")
.attr("transform", kx => `translate(${fx(kx)},0)`)
.append((_, i) => (i === j ? axis1 : axis2).render(null, scales, null, fxDimensions));
.append((kx, i) => (i === j ? axis1 : axis2).render(
fy && where(fyDomain, ky => marksIndexByFacet.has([kx, ky])),
scales,
null,
fxDimensions
));
}
})
.call(g => g.selectAll()
.data(facetKeys(scales))
.data(facetKeys(scales).filter(marksIndexByFacet.has, marksIndexByFacet))
.join("g")
.attr("transform", facetTranslate(fx, fy))
.each(function(key) {
const marksFacetIndex = marksIndexByFacet.get(key) || marksIndex;
const marksFacetIndex = marksIndexByFacet.get(key);
for (let i = 0; i < marks.length; ++i) {
const values = marksValues[i];
const index = filterStyles(marksFacetIndex[i], values);
Expand Down
5 changes: 5 additions & 0 deletions src/mark.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ export function range(data) {
return Uint32Array.from(data, indexOf);
}

// Returns a filtered range of data given the test function.
export function where(data, test) {
return range(data).filter(i => test(data[i], i, data));
}

// Returns an array [values[index[0]], values[index[1]], …].
export function take(values, index) {
return Array.from(index, i => values[i]);
Expand Down
19 changes: 7 additions & 12 deletions test/output/penguinCulmen.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading