Skip to content

Commit

Permalink
filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Dec 3, 2020
1 parent 836c236 commit b51205b
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 39 deletions.
7 changes: 7 additions & 0 deletions src/defined.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ export function ascendingDefined(a, b) {
export function nonempty(x) {
return x != null && (x + "") !== "";
}

export function filter(index, ...channels) {
for (const c of channels) {
if (c) index = index.filter(i => defined(c[i]));
}
return index;
}
10 changes: 1 addition & 9 deletions src/mark.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Mark {
initialize(data) {
if (data !== undefined) data = this.transform(data);
return {
index: data === undefined ? undefined : Array.from(data, indexOf),
index: data === undefined ? undefined : Uint32Array.from(data, indexOf),
channels: this.channels.map(channel => {
const {name} = channel;
return [name == null ? undefined : name + "", Channel(data, channel)];
Expand Down Expand Up @@ -68,11 +68,3 @@ export function maybeColor(value) {
export function maybeNumber(value) {
return typeof value === "number" ? [undefined, value] : [value, undefined];
}

export function applyAttr(selection, name, value) {
if (value != null) selection.attr(name, value);
}

export function applyStyle(selection, name, value) {
if (value != null) selection.style(name, value);
}
6 changes: 2 additions & 4 deletions src/marks/bar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ascending} from "d3-array";
import {create} from "d3-selection";
import {defined} from "../defined.js";
import {filter} from "../defined.js";
import {Mark, number, identity, indexOf, first, second, maybeColor} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles} from "../style.js";

Expand Down Expand Up @@ -41,9 +41,7 @@ class AbstractBar extends Mark {
render(I, scales, channels) {
const {color} = scales;
const {x: X, y: Y, z: Z, fill: F, stroke: S} = channels;
let index = I.filter(i => defined(X[i]) && defined(Y[i]));
if (F) index = index.filter(i => defined(F[i]));
if (S) index = index.filter(i => defined(S[i]));
const index = filter(I, X, Y, F, S);
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
return create("svg:g")
.call(applyIndirectStyles, this)
Expand Down
7 changes: 2 additions & 5 deletions src/marks/dot.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ascending} from "d3-array";
import {create} from "d3-selection";
import {defined, nonempty} from "../defined.js";
import {filter, nonempty} from "../defined.js";
import {Mark, indexOf, identity, first, second, maybeColor, maybeNumber} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles} from "../style.js";

Expand Down Expand Up @@ -48,10 +48,7 @@ export class Dot extends Mark {
{x, y, r, color},
{x: X, y: Y, z: Z, r: R, title: L, fill: F, stroke: S}
) {
let index = I.filter(i => defined(X[i]) && defined(Y[i]));
if (R) index = index.filter(i => defined(R[i]));
if (F) index = index.filter(i => defined(F[i]));
if (S) index = index.filter(i => defined(S[i]));
const index = filter(I, X, Y, R, F, S);
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
return create("svg:g")
.call(applyIndirectStyles, this)
Expand Down
5 changes: 2 additions & 3 deletions src/marks/link.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ascending} from "d3-array";
import {create} from "d3-selection";
import {defined} from "../defined.js";
import {filter} from "../defined.js";
import {Mark, maybeColor} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles} from "../style.js";

Expand Down Expand Up @@ -38,8 +38,7 @@ export class Link extends Mark {
{x, y, color},
{x1: X1, y1: Y1, x2: X2, y2: Y2, z: Z, stroke: S}
) {
let index = I.filter(i => defined(X1[i]) && defined(Y1[i]) && defined(X2[i]) && defined(Y2[i]));
if (S) index = index.filter(i => defined(S[i]));
const index = filter(I, X1, Y1, X2, Y2, S);
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
return create("svg:g")
.call(applyIndirectStyles, this)
Expand Down
6 changes: 2 additions & 4 deletions src/marks/rect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ascending} from "d3-array";
import {create} from "d3-selection";
import {zero} from "../mark.js";
import {defined} from "../defined.js";
import {filter} from "../defined.js";
import {Mark, number, maybeColor} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles} from "../style.js";

Expand Down Expand Up @@ -50,9 +50,7 @@ export class Rect extends Mark {
{x, y, color},
{x1: X1, y1: Y1, x2: X2, y2: Y2, z: Z, fill: F, stroke: S}
) {
let index = I.filter(i => defined(X1[i]) && defined(Y1[i]) && defined(X2[i]) && defined(Y2[i]));
if (F) index = index.filter(i => defined(F[i]));
if (S) index = index.filter(i => defined(S[i]));
const index = filter(I, X1, Y2, X2, Y2, F, S);
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
return create("svg:g")
.call(applyIndirectStyles, this)
Expand Down
11 changes: 3 additions & 8 deletions src/marks/rule.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ascending} from "d3-array";
import {create} from "d3-selection";
import {defined} from "../defined.js";
import {filter} from "../defined.js";
import {Mark, identity, maybeColor} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles} from "../style.js";

Expand Down Expand Up @@ -37,10 +37,7 @@ export class RuleX extends Mark {
{x: X, y1: Y1, y2: Y2, z: Z, stroke: S},
{marginTop, height, marginBottom}
) {
let index = I.filter(i => defined(X[i]));
if (Y1) index = index.filter(i => defined(Y1[i]));
if (Y2) index = index.filter(i => defined(Y2[i]));
if (S) index = index.filter(i => defined(S[i]));
const index = filter(I, X, Y1, Y2, S);
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
return create("svg:g")
.call(applyIndirectStyles, this)
Expand Down Expand Up @@ -90,9 +87,7 @@ export class RuleY extends Mark {
{y: Y, x1: X1, x2: X2, z: Z, stroke: S},
{width, marginLeft, marginRight}
) {
let index = I.filter(i => defined(Y[i]));
if (X1) index = index.filter(i => defined(X1[i]));
if (X2) index = index.filter(i => defined(X2[i]));
const index = filter(I, Y, X1, X2);
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
return create("svg:g")
.call(applyIndirectStyles, this)
Expand Down
5 changes: 2 additions & 3 deletions src/marks/text.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ascending} from "d3-array";
import {create} from "d3-selection";
import {defined, nonempty} from "../defined.js";
import {filter, nonempty} from "../defined.js";
import {Mark, indexOf, identity, string, maybeColor} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles, applyAttr} from "../style.js";

Expand Down Expand Up @@ -47,8 +47,7 @@ export class Text extends Mark {
{x, y, color},
{x: X, y: Y, z: Z, text: T, title: L, fill: F}
) {
let index = I.filter(i => defined(X[i]) && defined(Y[i]) && nonempty(T[i]));
if (F) index = index.filter(i => defined(F[i]));
const index = filter(I, X, Y, F).filter(i => nonempty(T[i]));
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
return create("svg:g")
.call(applyIndirectStyles, this)
Expand Down
5 changes: 2 additions & 3 deletions src/marks/tick.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ascending} from "d3-array";
import {create} from "d3-selection";
import {defined} from "../defined.js";
import {filter} from "../defined.js";
import {Mark, identity, indexOf, maybeColor} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles} from "../style.js";

Expand Down Expand Up @@ -30,8 +30,7 @@ class AbstractTick extends Mark {
render(I, scales, channels) {
const {color} = scales;
const {x: X, y: Y, z: Z, stroke: S} = channels;
let index = I.filter(i => defined(X[i]) && defined(Y[i]));
if (S) index = index.filter(i => defined(S[i]));
const index = filter(I, X, Y, S);
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
return create("svg:g")
.call(applyIndirectStyles, this)
Expand Down

0 comments on commit b51205b

Please sign in to comment.