Skip to content

Commit 0003e4f

Browse files
committed
shorthand extra channels
1 parent 74e8423 commit 0003e4f

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

src/channel.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export type ChannelName =
6161
* An object literal of channel definitions. This is also used to represent
6262
* materialized channel states after mark initialization.
6363
*/
64-
export type Channels = Record<string, Channel>;
64+
export type Channels = {[key in ChannelName]?: Channel};
6565

6666
/**
6767
* A channel definition. This is also used to represent the materialized channel

src/mark.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {ChannelDomainSort, Channels, ChannelValue, ChannelValues, ChannelValueSpec} from "./channel.js";
1+
import type {Channel, ChannelDomainSort, ChannelValue, ChannelValues, ChannelValueSpec} from "./channel.js";
22
import type {Context} from "./context.js";
33
import type {Dimensions} from "./dimensions.js";
44
import type {plot} from "./plot.js";
@@ -448,7 +448,7 @@ export interface MarkOptions {
448448
* An object defining additional custom channels. This meta option may be used
449449
* by an **initializer** to declare extra channels.
450450
*/
451-
channels?: Channels;
451+
channels?: Record<string, Channel | ChannelValue>;
452452
}
453453

454454
/** The abstract base class for Mark implementations. */

src/mark.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {channelDomain, createChannels, valueObject} from "./channel.js";
22
import {defined} from "./defined.js";
33
import {maybeFacetAnchor} from "./facet.js";
4+
import {maybeValues} from "./options.js";
45
import {arrayify, isDomainSort, isOptions, keyword, maybeNamed, range, singleton} from "./options.js";
56
import {project} from "./projection.js";
67
import {maybeClip, styles} from "./style.js";
@@ -38,7 +39,7 @@ export class Mark {
3839
}
3940
this.facetAnchor = maybeFacetAnchor(facetAnchor);
4041
channels = maybeNamed(channels);
41-
if (extraChannels !== undefined) channels = {...maybeNamed(extraChannels), ...channels};
42+
if (extraChannels !== undefined) channels = {...maybeValues(maybeNamed(extraChannels)), ...channels};
4243
if (defaults !== undefined) channels = {...styles(this, options, defaults), ...channels};
4344
this.channels = Object.fromEntries(
4445
Object.entries(channels)

src/options.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,15 @@ export function maybeValue(value) {
328328
return value === undefined || isOptions(value) ? value : {value};
329329
}
330330

331+
// Like maybeValue, but for an object for values.
332+
export function maybeValues(channels) {
333+
return Object.fromEntries(
334+
Object.entries(channels).map(([name, channel]) => {
335+
return [name, maybeValue(channel)];
336+
})
337+
);
338+
}
339+
331340
// Coerces the given channel values (if any) to numbers. This is useful when
332341
// values will be interpolated into other code, such as an SVG transform, and
333342
// where we don’t wish to allow unexpected behavior for weird input.

test/plots/tip.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ export async function tipDotFacets() {
8080
y: "height",
8181
fx: "sex",
8282
fy: "date_of_birth",
83-
channels: {
84-
name: {value: "name"},
85-
sport: {value: "sport"}
86-
}
83+
channels: {name: "name", sport: "sport"}
8784
})
8885
)
8986
]
@@ -107,7 +104,7 @@ export async function tipGeoCentroid() {
107104
marks: [
108105
Plot.geo(countymesh),
109106
Plot.geo(counties, {...pointer, stroke: "red", strokeWidth: 2}),
110-
Plot.tip(counties.features, {...pointer, channels: {name: {value: (d) => d.properties.name}}})
107+
Plot.tip(counties.features, {...pointer, channels: {name: (d) => d.properties.name}})
111108
]
112109
});
113110
}

0 commit comments

Comments
 (0)