Skip to content

Commit 06608eb

Browse files
tophtuckermbostock
andauthored
Remove autoImpl and return more from autoSpec (#1504)
* Remove autoImpl and return more from autoSpec * object to dry identifier/name conversion --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent 28669c4 commit 06608eb

File tree

2 files changed

+106
-16
lines changed

2 files changed

+106
-16
lines changed

src/marks/auto.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ import {rectX, rectY} from "./rect.js";
1313
import {ruleX, ruleY} from "./rule.js";
1414

1515
export function autoSpec(data, options) {
16-
const {x, y, fx, fy, color, size, mark} = autoImpl(data, options);
17-
return {x, y, fx, fy, color, size, mark};
18-
}
19-
20-
function autoImpl(data, options) {
2116
options = normalizeOptions(options);
2217

2318
// Greedily materialize columns for type inference; we’ll need them anyway to
@@ -202,27 +197,27 @@ function autoImpl(data, options) {
202197
reduce: sizeReduce ?? null
203198
},
204199
mark,
205-
markImpl,
200+
markImpl: implNames[markImpl],
206201
markOptions,
207-
transformImpl,
202+
transformImpl: implNames[transformImpl],
208203
transformOptions,
209204
colorMode
210205
};
211206
}
212207

213208
export function auto(data, options) {
209+
const spec = autoSpec(data, options);
214210
const {
215211
fx,
216212
fy,
217213
x: {zero: xZero},
218214
y: {zero: yZero},
219-
markImpl,
220215
markOptions,
221-
transformImpl,
222216
transformOptions,
223217
colorMode
224-
} = autoImpl(data, options);
225-
218+
} = spec;
219+
const markImpl = impls[spec.markImpl];
220+
const transformImpl = impls[spec.transformImpl];
226221
// In the case of filled marks (particularly bars and areas) the frame and
227222
// rules should come after the mark; in the case of stroked marks
228223
// (particularly dots and lines) they should come before the mark.
@@ -333,3 +328,33 @@ function isReducer(reduce) {
333328
function isHighCardinality(value) {
334329
return value ? new InternSet(value).size > value.length >> 1 : false;
335330
}
331+
332+
const impls = {
333+
dot,
334+
line,
335+
lineX,
336+
lineY,
337+
areaX,
338+
areaY,
339+
ruleX,
340+
ruleY,
341+
barX,
342+
barY,
343+
rectX,
344+
rectY,
345+
cell,
346+
bin,
347+
binX,
348+
binY,
349+
group,
350+
groupX,
351+
groupY
352+
};
353+
354+
// Instead of returning the mark or transform implementation directly, we return
355+
// the implementation name to facilitate code compilation (“eject to explicit
356+
// marks”). An implementation-to-name mapping needs to live somewhere for
357+
// compilation, and by having it in Plot we can more easily introduce a new mark
358+
// or transform implementation in Plot.auto without having to synchronize a
359+
// downstream change in the compiler.
360+
const implNames = Object.fromEntries(Object.entries(impls).map(([name, impl]) => [impl, name]));

test/marks/auto-test.js

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,20 @@ it("Plot.autoSpec makes a histogram from a quantitative dimension", () => {
1010
y: {value: null, reduce: "count", zero: true},
1111
color: {value: null, reduce: null},
1212
size: {value: null, reduce: null},
13-
mark: "bar"
13+
mark: "bar",
14+
markImpl: "rectY",
15+
markOptions: {
16+
fx: undefined,
17+
fy: undefined,
18+
x: {value: Object.assign([1, 1, 38], {label: "value"})},
19+
y: undefined,
20+
fill: undefined,
21+
z: undefined,
22+
r: undefined
23+
},
24+
transformImpl: "binX",
25+
transformOptions: {fill: undefined, r: undefined, y: "count"},
26+
colorMode: "fill"
1427
});
1528
});
1629

@@ -23,7 +36,20 @@ it("Plot.autoSpec makes a bar chart from an ordinal dimension", () => {
2336
y: {value: null, reduce: "count", zero: true},
2437
color: {value: null, reduce: null, color: "blue"},
2538
size: {value: null, reduce: null},
26-
mark: "bar"
39+
mark: "bar",
40+
markImpl: "barY",
41+
markOptions: {
42+
fx: undefined,
43+
fy: undefined,
44+
x: Object.assign(["duck", "duck", "goose"], {label: "value"}),
45+
y: undefined,
46+
fill: "blue",
47+
z: undefined,
48+
r: undefined
49+
},
50+
transformImpl: "groupX",
51+
transformOptions: {fill: undefined, r: undefined, y: "count"},
52+
colorMode: "fill"
2753
});
2854
});
2955

@@ -40,7 +66,20 @@ it("Plot.autoSpec makes a line from a monotonic dimension", () => {
4066
y: {value: "value", reduce: null, zero: false},
4167
color: {value: null, reduce: null},
4268
size: {value: null, reduce: null},
43-
mark: "line"
69+
mark: "line",
70+
markImpl: "line",
71+
markOptions: {
72+
fx: undefined,
73+
fy: undefined,
74+
x: Object.assign([1, 2, 3], {label: "date"}),
75+
y: Object.assign([1, 1, 38], {label: "value"}),
76+
stroke: undefined,
77+
z: undefined,
78+
r: undefined
79+
},
80+
transformImpl: undefined,
81+
transformOptions: {stroke: undefined, r: undefined},
82+
colorMode: "stroke"
4483
});
4584
});
4685

@@ -57,7 +96,20 @@ it("Plot.autoSpec makes a dot plot from two quantitative dimensions", () => {
5796
y: {value: "y", reduce: null, zero: false},
5897
color: {value: null, reduce: null},
5998
size: {value: null, reduce: null},
60-
mark: "dot"
99+
mark: "dot",
100+
markImpl: "dot",
101+
markOptions: {
102+
fx: undefined,
103+
fy: undefined,
104+
x: Object.assign([0, 2, 1], {label: "x"}),
105+
y: Object.assign([0, 3, 2], {label: "y"}),
106+
stroke: undefined,
107+
z: undefined,
108+
r: undefined
109+
},
110+
transformImpl: undefined,
111+
transformOptions: {stroke: undefined, r: undefined},
112+
colorMode: "stroke"
61113
});
62114
});
63115

@@ -77,6 +129,19 @@ it("Plot.autoSpec makes a faceted heatmap", () => {
77129
y: {value: "y", reduce: null, zero: false},
78130
color: {value: null, reduce: "count"},
79131
size: {value: null, reduce: null},
80-
mark: "bar"
132+
mark: "bar",
133+
markImpl: "rectY",
134+
markOptions: {
135+
fx: undefined,
136+
fy: "f",
137+
x: {value: Object.assign([0, 2, 1, 4, 2, 4], {label: "x"})},
138+
y: {value: Object.assign([0, 3, 2, 1, 6, 2], {label: "y"})},
139+
fill: undefined,
140+
z: undefined,
141+
r: undefined
142+
},
143+
transformImpl: "bin",
144+
transformOptions: {fill: "count", r: undefined},
145+
colorMode: "fill"
81146
});
82147
});

0 commit comments

Comments
 (0)