|
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, isFirst, 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, scaleLock, type, value, filter, hint}) { |
| 9 | + let V = valueof(data, value); |
| 10 | + |
| 11 | + // For color and symbol scales, if a scale is not explicitly specified when |
| 12 | + // the channel is declared, ignore the channel’s default scale if the channel |
| 13 | + // values are literal. In the case of symbols, we must further promote symbol |
| 14 | + // names (e.g., "plus") to a symbol instance (symbolPlus). TODO Instead of a |
| 15 | + // scaleLock property, use an "auto" scale? |
| 16 | + const is = scaleLock || !V ? null : scale === "color" ? isColor : scale === "symbol" ? isSymbol : null; |
| 17 | + if (is && isFirst(V, is) && isEvery(V, is)) { |
| 18 | + if (is === isSymbol) V = map(V, maybeSymbol); |
| 19 | + scale = undefined; |
| 20 | + } |
| 21 | + |
8 | 22 | return { |
9 | 23 | scale, |
10 | 24 | type, |
11 | | - value: valueof(data, value), |
| 25 | + value: V, |
12 | 26 | label: labelof(value), |
13 | 27 | filter, |
14 | 28 | hint |
15 | 29 | }; |
16 | 30 | } |
17 | 31 |
|
18 | | -export function Channels(descriptors, data) { |
19 | | - return Object.fromEntries(Object.entries(descriptors).map(([name, channel]) => [name, Channel(data, channel)])); |
| 32 | +export function Channels(channels, data) { |
| 33 | + return Object.fromEntries(Object.entries(channels).map(([name, channel]) => [name, Channel(data, channel)])); |
20 | 34 | } |
21 | 35 |
|
22 | 36 | // TODO Use Float64Array for scales with numeric ranges, e.g. position? |
23 | 37 | export function valueObject(channels, scales) { |
24 | 38 | return Object.fromEntries( |
25 | 39 | Object.entries(channels).map(([name, {scale: scaleName, value}]) => { |
26 | 40 | let scale; |
27 | | - if (scaleName !== undefined) { |
28 | | - scale = scales[scaleName]; |
29 | | - } |
| 41 | + if (scaleName != null) scale = scales[scaleName]; |
30 | 42 | return [name, scale === undefined ? value : map(value, scale)]; |
31 | 43 | }) |
32 | 44 | ); |
|
0 commit comments