Skip to content

Commit 66142e2

Browse files
authored
inline facets and axes (#799)
* clean internals * inline faceting * flatten facet axes * consolidate mark state; require channel names * local variable instead of selection.call * comments * enter-append instead of join * aria-label=facet
1 parent 2cb0639 commit 66142e2

35 files changed

+17263
-17407
lines changed

src/axis.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ export class AxisX {
4141
render(
4242
index,
4343
{[this.name]: x, fy},
44-
channels,
4544
{
4645
width,
4746
height,
4847
marginTop,
4948
marginRight,
5049
marginBottom,
5150
marginLeft,
51+
offsetLeft = 0,
5252
facetMarginTop,
5353
facetMarginBottom,
5454
labelMarginLeft = 0,
@@ -71,7 +71,7 @@ export class AxisX {
7171
const ty = offsetSign * offset + (axis === "top" ? marginTop : height - marginBottom);
7272
return create("svg:g")
7373
.call(applyAria, this)
74-
.attr("transform", `translate(0,${ty})`)
74+
.attr("transform", `translate(${offsetLeft},${ty})`)
7575
.call(createAxis(axis === "top" ? axisTop : axisBottom, x, this))
7676
.call(maybeTickRotate, tickRotate)
7777
.attr("font-size", null)
@@ -134,14 +134,14 @@ export class AxisY {
134134
render(
135135
index,
136136
{[this.name]: y, fx},
137-
channels,
138137
{
139138
width,
140139
height,
141140
marginTop,
142141
marginRight,
143142
marginBottom,
144143
marginLeft,
144+
offsetTop = 0,
145145
facetMarginLeft,
146146
facetMarginRight
147147
}
@@ -162,7 +162,7 @@ export class AxisY {
162162
const tx = offsetSign * offset + (axis === "right" ? width - marginRight : marginLeft);
163163
return create("svg:g")
164164
.call(applyAria, this)
165-
.attr("transform", `translate(${tx},0)`)
165+
.attr("transform", `translate(${tx},${offsetTop})`)
166166
.call(createAxis(axis === "right" ? axisRight : axisLeft, y, this))
167167
.call(maybeTickRotate, tickRotate)
168168
.attr("font-size", null)

src/legends/ramp.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ export function legendRamp(color, {
105105
x = applyRange(scaleLinear().domain([-1, range.length - 1]), [marginLeft, width - marginRight]);
106106

107107
svg.append("g")
108-
.selectAll("rect")
108+
.selectAll()
109109
.data(range)
110-
.join("rect")
110+
.enter()
111+
.append("rect")
111112
.attr("x", (d, i) => x(i - 1))
112113
.attr("y", marginTop)
113114
.attr("width", (d, i) => x(i) - x(i - 1))
@@ -123,9 +124,10 @@ export function legendRamp(color, {
123124
x = applyRange(scaleBand().domain(domain), [marginLeft, width - marginRight]);
124125

125126
svg.append("g")
126-
.selectAll("rect")
127+
.selectAll()
127128
.data(domain)
128-
.join("rect")
129+
.enter()
130+
.append("rect")
129131
.attr("x", x)
130132
.attr("y", marginTop)
131133
.attr("width", Math.max(0, x.bandwidth() - 1))

src/legends/swatches.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ function legendItems(scale, {
117117
.style("columns", columns)
118118
.selectAll()
119119
.data(scale.domain)
120-
.join("div")
120+
.enter()
121+
.append("div")
121122
.attr("class", `${className}-swatch`)
122123
.call(swatch, scale)
123124
.call(item => item.append("div")
@@ -142,7 +143,8 @@ function legendItems(scale, {
142143
swatches
143144
.selectAll()
144145
.data(scale.domain)
145-
.join("span")
146+
.enter()
147+
.append("span")
146148
.attr("class", `${className}-swatch`)
147149
.call(swatch, scale)
148150
.append(function() {

src/marks/area.js

Lines changed: 5 additions & 2 deletions
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;
@@ -42,7 +44,8 @@ export class Area extends Mark {
4244
.call(applyTransform, x, y, dx, dy)
4345
.call(g => g.selectAll()
4446
.data(groupIndex(I, [X1, Y1, X2, Y2], this, channels))
45-
.join("path")
47+
.enter()
48+
.append("path")
4649
.call(applyDirectStyles, this)
4750
.call(applyGroupedChannelStyles, this, channels)
4851
.attr("d", shapeArea()

src/marks/arrow.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ export class Arrow extends Mark {
6969
.call(applyTransform, x, y, offset + dx, offset + dy)
7070
.call(g => g.selectAll()
7171
.data(index)
72-
.join("path")
72+
.enter()
73+
.append("path")
7374
.call(applyDirectStyles, this)
7475
.attr("d", i => {
7576
// The start ⟨x1,y1⟩ and end ⟨x2,y2⟩ points may be inset, and the

src/marks/bar.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export class AbstractBar extends Mark {
2525
.call(this._transform, scales, dx, dy)
2626
.call(g => g.selectAll()
2727
.data(index)
28-
.join("rect")
28+
.enter()
29+
.append("rect")
2930
.call(applyDirectStyles, this)
3031
.attr("x", this._x(scales, channels, dimensions))
3132
.attr("width", this._width(scales, channels, dimensions))

src/marks/dot.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export class Dot extends Mark {
5858
.call(applyTransform, x, y, offset + dx, offset + dy)
5959
.call(g => g.selectAll()
6060
.data(index)
61-
.join(circle ? "circle" : "path")
61+
.enter()
62+
.append(circle ? "circle" : "path")
6263
.call(applyDirectStyles, this)
6364
.call(circle
6465
? selection => {

src/marks/image.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export class Image extends Mark {
6868
.call(applyTransform, x, y, offset + dx, offset + dy)
6969
.call(g => g.selectAll()
7070
.data(index)
71-
.join("image")
71+
.enter()
72+
.append("image")
7273
.call(applyDirectStyles, this)
7374
.attr("x", W && X ? i => X[i] - W[i] / 2 : W ? i => cx - W[i] / 2 : X ? i => X[i] - this.width / 2 : cx - this.width / 2)
7475
.attr("y", H && Y ? i => Y[i] - H[i] / 2 : H ? i => cy - H[i] / 2 : Y ? i => Y[i] - this.height / 2 : cy - this.height / 2)

src/marks/line.js

Lines changed: 5 additions & 2 deletions
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;
@@ -42,7 +44,8 @@ export class Line extends Mark {
4244
.call(applyTransform, x, y, offset + dx, offset + dy)
4345
.call(g => g.selectAll()
4446
.data(groupIndex(I, [X, Y], this, channels))
45-
.join("path")
47+
.enter()
48+
.append("path")
4649
.call(applyDirectStyles, this)
4750
.call(applyGroupedChannelStyles, this, channels)
4851
.call(applyGroupedMarkers, this, channels)

src/marks/link.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export class Link extends Mark {
3636
.call(applyTransform, x, y, offset + dx, offset + dy)
3737
.call(g => g.selectAll()
3838
.data(index)
39-
.join("path")
39+
.enter()
40+
.append("path")
4041
.call(applyDirectStyles, this)
4142
.attr("d", i => {
4243
const p = path();

src/marks/rect.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export class Rect extends Mark {
5353
.call(applyTransform, x, y, dx, dy)
5454
.call(g => g.selectAll()
5555
.data(index)
56-
.join("rect")
56+
.enter()
57+
.append("rect")
5758
.call(applyDirectStyles, this)
5859
.attr("x", X1 && X2 && !isCollapsed(x) ? i => Math.min(X1[i], X2[i]) + insetLeft : marginLeft + insetLeft)
5960
.attr("y", Y1 && Y2 && !isCollapsed(y) ? i => Math.min(Y1[i], Y2[i]) + insetTop : marginTop + insetTop)

src/marks/rule.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ export class RuleX extends Mark {
4141
return create("svg:g")
4242
.call(applyIndirectStyles, this, dimensions)
4343
.call(applyTransform, X && x, null, offset, 0)
44-
.call(g => g.selectAll("line")
44+
.call(g => g.selectAll()
4545
.data(index)
46-
.join("line")
46+
.enter()
47+
.append("line")
4748
.call(applyDirectStyles, this)
4849
.attr("x1", X ? i => X[i] : (marginLeft + width - marginRight) / 2)
4950
.attr("x2", X ? i => X[i] : (marginLeft + width - marginRight) / 2)
@@ -84,9 +85,10 @@ export class RuleY extends Mark {
8485
return create("svg:g")
8586
.call(applyIndirectStyles, this, dimensions)
8687
.call(applyTransform, null, Y && y, dx, offset + dy)
87-
.call(g => g.selectAll("line")
88+
.call(g => g.selectAll()
8889
.data(index)
89-
.join("line")
90+
.enter()
91+
.append("line")
9092
.call(applyDirectStyles, this)
9193
.attr("x1", X1 && !isCollapsed(x) ? i => X1[i] + insetLeft : marginLeft + insetLeft)
9294
.attr("x2", X2 && !isCollapsed(x) ? (x.bandwidth ? i => X2[i] + x.bandwidth() - insetRight : i => X2[i] - insetRight) : width - marginRight - insetRight)

src/marks/text.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export class Text extends Mark {
6868
.call(applyTransform, x, y, offset + dx, offset + dy)
6969
.call(g => g.selectAll()
7070
.data(index)
71-
.join("text")
71+
.enter()
72+
.append("text")
7273
.call(applyDirectStyles, this)
7374
.call(applyMultilineText, this, T)
7475
.attr("transform", R ? (X && Y ? i => `translate(${X[i]},${Y[i]}) rotate(${R[i]})`

src/marks/tick.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ class AbstractTick extends Mark {
1818
return create("svg:g")
1919
.call(applyIndirectStyles, this, dimensions)
2020
.call(this._transform, scales, dx, dy)
21-
.call(g => g.selectAll("line")
21+
.call(g => g.selectAll()
2222
.data(index)
23-
.join("line")
23+
.enter()
24+
.append("line")
2425
.call(applyDirectStyles, this)
2526
.attr("x1", this._x1(scales, channels, dimensions))
2627
.attr("x2", this._x2(scales, channels, dimensions))

src/marks/vector.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export class Vector extends Mark {
4848
.call(applyTransform, x, y, offset + dx, offset + dy)
4949
.call(g => g.selectAll()
5050
.data(index)
51-
.join("path")
51+
.enter()
52+
.append("path")
5253
.call(applyDirectStyles, this)
5354
.attr("d", i => {
5455
const l = fl(i), a = fr(i) * radians;

0 commit comments

Comments
 (0)