Skip to content

Commit 6b3a578

Browse files
committed
default filter
1 parent 1a2de86 commit 6b3a578

File tree

5 files changed

+21
-29
lines changed

5 files changed

+21
-29
lines changed

src/axis.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import {boolean, take, number, string, keyword, maybeKeyword, constant, isTempor
33
import {formatIsoDate} from "./format.js";
44
import {radians} from "./math.js";
55
import {applyAttr, impliedString} from "./style.js";
6-
import {Decoration} from "./decoration.js";
76

8-
export class AxisX extends Decoration {
7+
export class AxisX {
98
constructor({
109
name = "x",
1110
axis,
@@ -23,7 +22,6 @@ export class AxisX extends Decoration {
2322
ariaLabel,
2423
ariaDescription
2524
} = {}) {
26-
super();
2725
this.name = name;
2826
this.axis = keyword(axis, "axis", ["top", "bottom"]);
2927
this.ticks = ticks;
@@ -99,7 +97,7 @@ export class AxisX extends Decoration {
9997
}
10098
}
10199

102-
export class AxisY extends Decoration {
100+
export class AxisY {
103101
constructor({
104102
name = "y",
105103
axis,
@@ -117,7 +115,6 @@ export class AxisY extends Decoration {
117115
ariaLabel,
118116
ariaDescription
119117
} = {}) {
120-
super();
121118
this.name = name;
122119
this.axis = keyword(axis, "axis", ["left", "right"]);
123120
this.ticks = ticks;

src/decoration.js

-13
This file was deleted.

src/marks/area.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {maybeIdentityX, maybeIdentityY} from "../transforms/identity.js";
77
import {maybeStackX, maybeStackY} from "../transforms/stack.js";
88

99
const defaults = {
10+
filter: null,
1011
ariaLabel: "area",
1112
strokeWidth: 1,
1213
strokeMiterlimit: 1
@@ -29,9 +30,6 @@ export class Area extends Mark {
2930
);
3031
this.curve = Curve(curve, tension);
3132
}
32-
filter(I) {
33-
return I;
34-
}
3533
render(I, {x, y}, channels, dimensions) {
3634
const {x1: X1, y1: Y1, x2: X2 = X1, y2: Y2 = Y1} = channels;
3735
const {dx, dy} = this;

src/marks/line.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {applyDirectStyles, applyIndirectStyles, applyTransform, applyGroupedChan
66
import {applyGroupedMarkers, markers} from "./marker.js";
77

88
const defaults = {
9+
filter: null,
910
ariaLabel: "line",
1011
fill: "none",
1112
stroke: "currentColor",
@@ -29,9 +30,6 @@ export class Line extends Mark {
2930
this.curve = Curve(curve, tension);
3031
markers(this, options);
3132
}
32-
filter(I) {
33-
return I;
34-
}
3533
render(I, {x, y}, channels, dimensions) {
3634
const {x: X, y: Y} = channels;
3735
const {dx, dy} = this;

src/plot.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {create, cross, difference, groups, InternMap, select} from "d3";
22
import {Axes, autoAxisTicks, autoScaleLabels} from "./axes.js";
33
import {Channel, channelSort} from "./channel.js";
4-
import {Decoration} from "./decoration.js";
4+
import {defined} from "./defined.js";
55
import {Dimensions} from "./dimensions.js";
66
import {Legends, exposeLegends} from "./legends.js";
77
import {arrayify, isOptions, keyword, range, first, second, where} from "./options.js";
@@ -99,7 +99,8 @@ export function plot(options = {}) {
9999
for (const mark of marks) {
100100
const channels = markChannels.get(mark) ?? [];
101101
const values = applyScales(channels, scales);
102-
const index = mark.filter(markIndex.get(mark), channels, values);
102+
let index = markIndex.get(mark);
103+
if (mark.filter != null) index = mark.filter(index, channels, values);
103104
const node = mark.render(index, scales, values, dimensions, axes);
104105
if (node != null) svg.appendChild(node);
105106
}
@@ -136,15 +137,25 @@ export function plot(options = {}) {
136137
return figure;
137138
}
138139

139-
export class Mark extends Decoration {
140+
function defaultFilter(index, channels, values) {
141+
for (const [name, {filter = defined}] of channels) {
142+
if (name !== undefined && filter !== null) {
143+
const value = values[name];
144+
index = index.filter(i => filter(value[i]));
145+
}
146+
}
147+
return index;
148+
}
149+
150+
export class Mark {
140151
constructor(data, channels = [], options = {}, defaults) {
141-
super();
142152
const {facet = "auto", sort, dx, dy, clip} = options;
143153
const names = new Set();
144154
this.data = data;
145155
this.sort = isOptions(sort) ? sort : null;
146156
this.facet = facet == null || facet === false ? null : keyword(facet === true ? "include" : facet, "facet", ["auto", "include", "exclude"]);
147157
const {transform} = basic(options);
158+
this.filter = defaults?.filter === undefined ? defaultFilter : defaults.filter;
148159
this.transform = transform;
149160
if (defaults !== undefined) channels = styles(this, options, channels, defaults);
150161
this.channels = channels.filter(channel => {
@@ -321,7 +332,8 @@ class Facet extends Mark {
321332
for (let i = 0; i < marks.length; ++i) {
322333
const mark = marks[i];
323334
const values = marksValues[i];
324-
const index = mark.filter(marksFacetIndex[i], marksChannels[i], values);
335+
let index = marksFacetIndex[i];
336+
if (mark.filter != null) mark.filter(index, marksChannels[i], values);
325337
const node = mark.render(index, scales, values, subdimensions);
326338
if (node != null) this.appendChild(node);
327339
}

0 commit comments

Comments
 (0)