|
1 | 1 | import {ascending, descending, rollup, sort} from "d3"; |
2 | | -import {first, isIterable, labelof, map, maybeValue, range, valueof} from "./options.js"; |
| 2 | +import {first, isColor, isEvery, isIterable, labelof, map, maybeValue, range, valueof} from "./options.js"; |
3 | 3 | import {registry} from "./scales/index.js"; |
| 4 | +import {isSymbol, maybeSymbol} from "./symbols.js"; |
4 | 5 | import {maybeReduce} from "./transforms/group.js"; |
5 | 6 |
|
6 | 7 | // TODO Type coercion? |
7 | | -export function Channel(data, {scale, type, value, filter, hint}) { |
| 8 | +export function Channel(data, {scale, type, value, filter, hint}, name) { |
| 9 | + let V = valueof(data, value); |
| 10 | + |
| 11 | + // If the channel uses the "auto" scale, infer the scale from the channel name |
| 12 | + // and the provided values. For color and symbol channels, no scale is applied |
| 13 | + // if the values are literal; however for symbols, we must promote symbol |
| 14 | + // names (e.g., "plus") to symbol implementations (symbolPlus). |
| 15 | + if (scale === "auto") { |
| 16 | + scale = inferChannelScale(name); |
| 17 | + if (V) { |
| 18 | + if (scale === "color") { |
| 19 | + if (isEvery(V, isColor)) { |
| 20 | + scale = undefined; |
| 21 | + } |
| 22 | + } else if (scale === "symbol") { |
| 23 | + if (isEvery(V, isSymbol)) { |
| 24 | + V = map(V, maybeSymbol); |
| 25 | + scale = undefined; |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
8 | 31 | return { |
9 | 32 | scale, |
10 | 33 | type, |
11 | | - value: valueof(data, value), |
| 34 | + value: V, |
12 | 35 | label: labelof(value), |
13 | 36 | filter, |
14 | 37 | hint |
15 | 38 | }; |
16 | 39 | } |
17 | 40 |
|
18 | | -export function Channels(descriptors, data) { |
19 | | - return Object.fromEntries(Object.entries(descriptors).map(([name, channel]) => [name, Channel(data, channel)])); |
| 41 | +export function Channels(channels, data) { |
| 42 | + return Object.fromEntries(Object.entries(channels).map(([name, channel]) => [name, Channel(data, channel, name)])); |
20 | 43 | } |
21 | 44 |
|
22 | 45 | // TODO Use Float64Array for scales with numeric ranges, e.g. position? |
23 | 46 | export function valueObject(channels, scales) { |
24 | 47 | return Object.fromEntries( |
25 | 48 | Object.entries(channels).map(([name, {scale: scaleName, value}]) => { |
26 | 49 | let scale; |
27 | | - if (scaleName !== undefined) { |
28 | | - scale = scales[scaleName]; |
29 | | - } |
| 50 | + if (scaleName != null) scale = scales[scaleName]; |
30 | 51 | return [name, scale === undefined ? value : map(value, scale)]; |
31 | 52 | }) |
32 | 53 | ); |
33 | 54 | } |
34 | 55 |
|
| 56 | +// Returns the default scale for the channel with the given name. |
| 57 | +export function inferChannelScale(name) { |
| 58 | + switch (name) { |
| 59 | + case "fill": |
| 60 | + case "stroke": |
| 61 | + return "color"; |
| 62 | + case "fillOpacity": |
| 63 | + case "strokeOpacity": |
| 64 | + return "opacity"; |
| 65 | + } |
| 66 | + return registry.has(name) ? name : null; |
| 67 | +} |
| 68 | + |
35 | 69 | // Note: mutates channel.domain! This is set to a function so that it is lazily |
36 | 70 | // computed; i.e., if the scale’s domain is set explicitly, that takes priority |
37 | 71 | // over the sort option, and we don’t need to do additional work. |
|
0 commit comments