Skip to content

Commit bca32ba

Browse files
committed
infer channel scales
1 parent 1852e30 commit bca32ba

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/plot.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export function plot(options = {}) {
7676
: mark.facet === "exclude" ? facetsExclude || (facetsExclude = facetsIndex.map(f => Uint32Array.from(difference(facetIndex, f))))
7777
: undefined;
7878
const {facets, channels} = mark.initialize(markFacets, facetChannels);
79-
stateByMark.set(mark, {facets, channels: applyScaleTransforms(channels, options)});
79+
applyScaleTransforms(channels, options);
80+
stateByMark.set(mark, {facets, channels});
8081
}
8182

8283
// Initalize the scales and axes.
@@ -96,7 +97,9 @@ export function plot(options = {}) {
9697
const {facets, channels} = mark.reinitialize(state.facets, state.channels, scales);
9798
if (facets !== undefined) state.facets = facets;
9899
if (channels !== undefined) {
99-
Object.assign(state.channels, applyScaleTransforms(channels, options));
100+
inferChannelScale(channels, mark);
101+
applyScaleTransforms(channels, options);
102+
Object.assign(state.channels, channels);
100103
for (const {scale} of Object.values(channels)) if (scale != null) newByScale.add(scale);
101104
}
102105
}
@@ -324,6 +327,25 @@ function applyScaleTransforms(channels, options) {
324327
return channels;
325328
}
326329

330+
// An initializer may generate channels without knowing how the downstream mark
331+
// will use them. Marks are typically responsible associated scales with
332+
// channels, but here we assume common behavior across marks.
333+
function inferChannelScale(channels) {
334+
for (const name in channels) {
335+
const channel = channels[name];
336+
let {scale} = channel;
337+
if (scale === true) {
338+
switch (name) {
339+
case "fill": case "stroke": scale = "color"; break;
340+
case "fillOpacity": case "strokeOpacity": case "opacity": scale = "opacity"; break;
341+
case "r": case "length": case "symbol": scale = name; break;
342+
default: scale = null;
343+
}
344+
channel.scale = scale;
345+
}
346+
}
347+
}
348+
327349
function addScaleChannels(channelsByScale, stateByMark, filter = yes) {
328350
for (const {channels} of stateByMark.values()) {
329351
for (const name in channels) {

src/transforms/hexbin.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,9 @@ function hexbinn(outputs, {radius = 10, ...options}) {
4949
channels: {
5050
x: {value: BX},
5151
y: {value: BY},
52-
...Object.fromEntries(outputs.map(({name, output}) => [name, {scale: scaleof(name), radius: name === "r" ? radius : undefined, value: output.transform()}]))
52+
...Object.fromEntries(outputs.map(({name, output}) => [name, {scale: true, radius: name === "r" ? radius : undefined, value: output.transform()}]))
5353
}
5454
};
5555
}
5656
};
5757
}
58-
59-
function scaleof(name) {
60-
switch (name) {
61-
case "fill": case "stroke": return "color";
62-
case "fillOpacity": case "strokeOpacity": case "opacity": return "opacity";
63-
case "r": case "length": case "symbol": return name;
64-
}
65-
}

0 commit comments

Comments
 (0)