Skip to content

Commit 9428166

Browse files
committed
clean internals
1 parent 2cb0639 commit 9428166

File tree

4 files changed

+40
-44
lines changed

4 files changed

+40
-44
lines changed

src/axis.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export class AxisX {
4141
render(
4242
index,
4343
{[this.name]: x, fy},
44-
channels,
4544
{
4645
width,
4746
height,
@@ -134,7 +133,6 @@ export class AxisY {
134133
render(
135134
index,
136135
{[this.name]: y, fx},
137-
channels,
138136
{
139137
width,
140138
height,

src/marks/area.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {maybeIdentityX, maybeIdentityY} from "../transforms/identity.js";
88
import {maybeStackX, maybeStackY} from "../transforms/stack.js";
99

1010
const defaults = {
11-
filter: null,
1211
ariaLabel: "area",
1312
strokeWidth: 1,
1413
strokeLinecap: "round",
@@ -34,6 +33,9 @@ export class Area extends Mark {
3433
this.z = z;
3534
this.curve = Curve(curve, tension);
3635
}
36+
filter(index) {
37+
return index;
38+
}
3739
render(I, {x, y}, channels, dimensions) {
3840
const {x1: X1, y1: Y1, x2: X2 = X1, y2: Y2 = Y1} = channels;
3941
const {dx, dy} = this;

src/marks/line.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {maybeDenseIntervalX, maybeDenseIntervalY} from "../transforms/bin.js";
77
import {applyGroupedMarkers, markers} from "./marker.js";
88

99
const defaults = {
10-
filter: null,
1110
ariaLabel: "line",
1211
fill: "none",
1312
stroke: "currentColor",
@@ -34,6 +33,9 @@ export class Line extends Mark {
3433
this.curve = Curve(curve, tension);
3534
markers(this, options);
3635
}
36+
filter(index) {
37+
return index;
38+
}
3739
render(I, {x, y}, channels, dimensions) {
3840
const {x: X, y: Y} = channels;
3941
const {dx, dy} = this;

src/plot.js

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ export function plot(options = {}) {
6161
autoScaleLabels(scaleChannels, scaleDescriptors, axes, dimensions, options);
6262
autoAxisTicks(scaleDescriptors, axes);
6363

64-
// When faceting, render axes for fx and fy instead of x and y.
65-
const x = facet !== undefined && scales.fx ? "fx" : "x";
66-
const y = facet !== undefined && scales.fy ? "fy" : "y";
67-
if (axes[x]) marks.unshift(axes[x]);
68-
if (axes[y]) marks.unshift(axes[y]);
69-
7064
const {width, height} = dimensions;
7165

7266
const svg = create("svg")
@@ -96,11 +90,17 @@ export function plot(options = {}) {
9690
.call(applyInlineStyles, style)
9791
.node();
9892

93+
// When faceting, render axes for fx and fy instead of x and y.
94+
const axisX = axes[facet !== undefined && scales.fx ? "fx" : "x"];
95+
const axisY = axes[facet !== undefined && scales.fy ? "fy" : "y"];
96+
if (axisY) svg.appendChild(axisY.render(null, scales, dimensions));
97+
if (axisX) svg.appendChild(axisX.render(null, scales, dimensions));
98+
99+
// Render marks.
99100
for (const mark of marks) {
100-
const channels = markChannels.get(mark) ?? [];
101+
const channels = markChannels.get(mark);
101102
const values = applyScales(channels, scales);
102-
let index = markIndex.get(mark);
103-
if (mark.filter != null) index = mark.filter(index, channels, values);
103+
const index = mark.filter(markIndex.get(mark), channels, values);
104104
const node = mark.render(index, scales, values, dimensions, axes);
105105
if (node != null) svg.appendChild(node);
106106
}
@@ -138,16 +138,6 @@ export function plot(options = {}) {
138138
return figure;
139139
}
140140

141-
function defaultFilter(index, channels, values) {
142-
for (const [name, {filter = defined}] of channels) {
143-
if (name !== undefined && filter !== null) {
144-
const value = values[name];
145-
index = index.filter(i => filter(value[i]));
146-
}
147-
}
148-
return index;
149-
}
150-
151141
export class Mark {
152142
constructor(data, channels = [], options = {}, defaults) {
153143
const {facet = "auto", sort, dx, dy, clip} = options;
@@ -156,7 +146,6 @@ export class Mark {
156146
this.sort = isOptions(sort) ? sort : null;
157147
this.facet = facet == null || facet === false ? null : keyword(facet === true ? "include" : facet, "facet", ["auto", "include", "exclude"]);
158148
const {transform} = basic(options);
159-
this.filter = defaults?.filter === undefined ? defaultFilter : defaults.filter;
160149
this.transform = transform;
161150
if (defaults !== undefined) channels = styles(this, options, channels, defaults);
162151
this.channels = channels.filter(channel => {
@@ -193,6 +182,15 @@ export class Mark {
193182
if (this.sort != null) channelSort(channels, facetChannels, data, this.sort);
194183
return {index, channels};
195184
}
185+
filter(index, channels, values) {
186+
for (const [name, {filter = defined}] of channels) {
187+
if (name !== undefined && filter !== null) {
188+
const value = values[name];
189+
index = index.filter(i => filter(value[i]));
190+
}
191+
}
192+
return index;
193+
}
196194
plot({marks = [], ...options} = {}) {
197195
return plot({...options, marks: [...marks, this]});
198196
}
@@ -240,16 +238,14 @@ class Facet extends Mark {
240238
this.marksIndexByFacet = undefined; // map from facet key to array of mark indexes
241239
}
242240
initialize() {
243-
const {index, channels} = super.initialize();
244-
const facets = index === undefined ? [] : facetGroups(index, channels);
241+
const {index, channels: facetChannels} = super.initialize();
242+
const facets = index === undefined ? [] : facetGroups(index, facetChannels);
245243
const facetsKeys = Array.from(facets, first);
246244
const facetsIndex = Array.from(facets, second);
247-
const subchannels = [];
245+
const channels = facetChannels.slice();
248246
const marksChannels = this.marksChannels = [];
249-
const marksIndexByFacet = this.marksIndexByFacet = facetMap(channels);
250-
for (const facetKey of facetsKeys) {
251-
marksIndexByFacet.set(facetKey, new Array(this.marks.length));
252-
}
247+
const marksIndexByFacet = this.marksIndexByFacet = facetMap(facetChannels);
248+
for (const key of facetsKeys) marksIndexByFacet.set(key, new Array(this.marks.length));
253249
let facetsExclude;
254250
for (let i = 0; i < this.marks.length; ++i) {
255251
const mark = this.marks[i];
@@ -258,30 +254,31 @@ class Facet extends Mark {
258254
: facet === "include" ? facetsIndex
259255
: facet === "exclude" ? facetsExclude || (facetsExclude = facetsIndex.map(f => Uint32Array.from(difference(index, f))))
260256
: undefined;
261-
const {index: I, channels: markChannels} = mark.initialize(markFacets, channels);
257+
const {index: markIndex, channels: markChannels} = mark.initialize(markFacets, facetChannels);
262258
// If an index is returned by mark.initialize, its structure depends on
263259
// whether or not faceting has been applied: it is a flat index ([0, 1, 2,
264260
// …]) when not faceted, and a nested index ([[0, 1, …], [2, 3, …], …])
265261
// when faceted.
266-
if (I !== undefined) {
262+
if (markIndex !== undefined) {
267263
if (markFacets) {
268264
for (let j = 0; j < facetsKeys.length; ++j) {
269-
marksIndexByFacet.get(facetsKeys[j])[i] = I[j];
265+
marksIndexByFacet.get(facetsKeys[j])[i] = markIndex[j];
270266
}
271267
} else {
272268
for (let j = 0; j < facetsKeys.length; ++j) {
273-
marksIndexByFacet.get(facetsKeys[j])[i] = I;
269+
marksIndexByFacet.get(facetsKeys[j])[i] = markIndex;
274270
}
275271
}
276272
}
277-
for (const [, channel] of markChannels) {
278-
subchannels.push([, channel]);
279-
}
273+
for (const [, channel] of markChannels) channels.push([, channel]); // anonymize channels
280274
marksChannels.push(markChannels);
281275
}
282-
return {index, channels: [...channels, ...subchannels]};
276+
return {index, channels};
277+
}
278+
filter(index) {
279+
return index;
283280
}
284-
render(I, scales, _, dimensions, axes) {
281+
render(index, scales, values, dimensions, axes) {
285282
const {marks, marksChannels, marksIndexByFacet} = this;
286283
const {fx, fy} = scales;
287284
const fyDomain = fy && fy.domain();
@@ -303,7 +300,6 @@ class Facet extends Mark {
303300
.append((ky, i) => (i === j ? axis1 : axis2).render(
304301
fx && where(fxDomain, kx => marksIndexByFacet.has([kx, ky])),
305302
scales,
306-
null,
307303
fyDimensions
308304
));
309305
}
@@ -319,7 +315,6 @@ class Facet extends Mark {
319315
.append((kx, i) => (i === j ? axis1 : axis2).render(
320316
fy && where(fyDomain, ky => marksIndexByFacet.has([kx, ky])),
321317
scales,
322-
null,
323318
fxDimensions
324319
));
325320
}
@@ -333,8 +328,7 @@ class Facet extends Mark {
333328
for (let i = 0; i < marks.length; ++i) {
334329
const mark = marks[i];
335330
const values = marksValues[i];
336-
let index = marksFacetIndex[i];
337-
if (mark.filter != null) index = mark.filter(index, marksChannels[i], values);
331+
const index = mark.filter(marksFacetIndex[i], marksChannels[i], values);
338332
const node = mark.render(index, scales, values, subdimensions);
339333
if (node != null) this.appendChild(node);
340334
}

0 commit comments

Comments
 (0)