Skip to content

Commit 9924a6b

Browse files
committed
mark.channels is an object
1 parent 9049e81 commit 9924a6b

File tree

15 files changed

+159
-160
lines changed

15 files changed

+159
-160
lines changed

src/channel.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export function Channel(data, {scale, type, value, filter, hint}) {
1515
};
1616
}
1717

18-
export function channelObject(channelDescriptors, data) {
18+
export function Channels(channelDescriptors, data) {
1919
const channels = {};
20-
for (const channel of channelDescriptors) {
21-
channels[channel.name] = Channel(data, channel);
20+
for (const name in channelDescriptors) {
21+
channels[name] = Channel(data, channelDescriptors[name]);
2222
}
2323
return channels;
2424
}

src/marks/dot.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ export class Dot extends Mark {
4242
// appropriate default symbols based on whether the dots are filled or
4343
// stroked, and for the symbol legend to match the appearance of the dots.
4444
const {channels} = this;
45-
const symbolChannel = channels.find(({scale}) => scale === "symbol");
45+
const {symbol: symbolChannel} = channels;
4646
if (symbolChannel) {
47-
const fillChannel = channels.find(({name}) => name === "fill");
48-
const strokeChannel = channels.find(({name}) => name === "stroke");
47+
const {fill: fillChannel, stroke: strokeChannel} = channels;
4948
symbolChannel.hint = {
5049
fill: fillChannel ? (fillChannel.value === symbolChannel.value ? "color" : "currentColor") : this.fill,
5150
stroke: strokeChannel ? (strokeChannel.value === symbolChannel.value ? "color" : "currentColor") : this.stroke

src/plot.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {cross, difference, groups, InternMap, select} from "d3";
22
import {Axes, autoAxisTicks, autoScaleLabels} from "./axes.js";
3-
import {Channel, channelObject, channelDomain, valueObject} from "./channel.js";
3+
import {Channel, Channels, channelDomain, valueObject} from "./channel.js";
44
import {Context, create} from "./context.js";
55
import {defined} from "./defined.js";
66
import {Dimensions} from "./dimensions.js";
@@ -266,7 +266,7 @@ export class Mark {
266266
this.facet = facet == null || facet === false ? null : keyword(facet === true ? "include" : facet, "facet", ["auto", "include", "exclude"]);
267267
if (extraChannels !== undefined) channels = [...channels, ...extraChannels.filter(e => !channels.some(c => c.name === e.name))];
268268
if (defaults !== undefined) channels = [...channels, ...styles(this, options, defaults)];
269-
this.channels = channels.filter(channel => {
269+
this.channels = Object.fromEntries(channels.filter(channel => {
270270
const {name, value, optional} = channel;
271271
if (value == null) {
272272
if (optional) return false;
@@ -278,7 +278,7 @@ export class Mark {
278278
if (names.has(key)) throw new Error(`duplicate channel: ${key}`);
279279
names.add(key);
280280
return true;
281-
});
281+
}).map(channel => [channel.name, channel]));
282282
this.dx = +dx || 0;
283283
this.dy = +dy || 0;
284284
this.clip = maybeClip(clip);
@@ -287,7 +287,7 @@ export class Mark {
287287
let data = arrayify(this.data);
288288
if (facets === undefined && data != null) facets = [range(data)];
289289
if (this.transform != null) ({facets, data} = this.transform(data, facets)), data = arrayify(data);
290-
const channels = channelObject(this.channels, data);
290+
const channels = Channels(this.channels, data);
291291
if (this.sort != null) channelDomain(channels, facetChannels, data, this.sort);
292292
return {data, facets, channels};
293293
}

test/marks/area-test.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ it("area(data, options) has the expected defaults", () => {
66
const area = Plot.area(undefined, {x1: "0", y1: "1"});
77
assert.strictEqual(area.data, undefined);
88
// assert.strictEqual(area.transform, undefined);
9-
assert.deepStrictEqual(area.channels.map(c => c.name), ["x1", "y1"]);
10-
assert.deepStrictEqual(area.channels.map(c => c.value), ["0", "1"]);
11-
assert.deepStrictEqual(area.channels.map(c => c.scale), ["x", "y"]);
9+
assert.deepStrictEqual(Object.keys(area.channels), ["x1", "y1"]);
10+
assert.deepStrictEqual(Object.values(area.channels).map(c => c.value), ["0", "1"]);
11+
assert.deepStrictEqual(Object.values(area.channels).map(c => c.scale), ["x", "y"]);
1212
assert.strictEqual(area.curve, curveLinear);
1313
assert.strictEqual(area.fill, undefined);
1414
assert.strictEqual(area.fillOpacity, undefined);
@@ -26,28 +26,28 @@ it("area(data, options) has the expected defaults", () => {
2626

2727
it("area(data, {x1, y1, y2}) specifies an optional y2 channel", () => {
2828
const area = Plot.area(undefined, {x1: "0", y1: "1", y2: "2"});
29-
const y2 = area.channels.find(c => c.name === "y2");
29+
const {y2} = area.channels;
3030
assert.strictEqual(y2.value, "2");
3131
assert.strictEqual(y2.scale, "y");
3232
});
3333

3434
it("area(data, {x1, x2, y1}) specifies an optional x2 channel", () => {
3535
const area = Plot.area(undefined, {x1: "0", x2: "1", y1: "2"});
36-
const x2 = area.channels.find(c => c.name === "x2");
36+
const {x2} = area.channels;
3737
assert.strictEqual(x2.value, "1");
3838
assert.strictEqual(x2.scale, "x");
3939
});
4040

4141
it("area(data, {z}) specifies an optional z channel", () => {
4242
const area = Plot.area(undefined, {x1: "0", y1: "1", z: "2"});
43-
const z = area.channels.find(c => c.name === "z");
43+
const {z} = area.channels;
4444
assert.strictEqual(z.value, "2");
4545
assert.strictEqual(z.scale, undefined);
4646
});
4747

4848
it("area(data, {title}) specifies an optional title channel", () => {
4949
const area = Plot.area(undefined, {x1: "0", y1: "1", title: "2"});
50-
const title = area.channels.find(c => c.name === "title");
50+
const {title} = area.channels;
5151
assert.strictEqual(title.value, "2");
5252
assert.strictEqual(title.scale, undefined);
5353
});
@@ -65,14 +65,14 @@ it("area(data, {fill}) allows fill to be null", () => {
6565
it("area(data, {fill}) allows fill to be a variable color", () => {
6666
const area = Plot.area(undefined, {x1: "0", y1: "1", fill: "x"});
6767
assert.strictEqual(area.fill, undefined);
68-
const fill = area.channels.find(c => c.name === "fill");
68+
const {fill} = area.channels;
6969
assert.strictEqual(fill.value, "x");
7070
assert.strictEqual(fill.scale, "color");
7171
});
7272

7373
it("area(data, {fill}) implies a default z channel if fill is variable", () => {
7474
const area = Plot.area(undefined, {x1: "0", y1: "1", fill: "2", stroke: "3"}); // fill takes priority
75-
const z = area.channels.find(c => c.name === "z");
75+
const {z} = area.channels;
7676
assert.strictEqual(z.value, "2");
7777
assert.strictEqual(z.scale, undefined);
7878
});
@@ -90,14 +90,14 @@ it("area(data, {stroke}) allows stroke to be null", () => {
9090
it("area(data, {stroke}) allows stroke to be a variable color", () => {
9191
const area = Plot.area(undefined, {x1: "0", y1: "1", stroke: "x"});
9292
assert.strictEqual(area.stroke, undefined);
93-
const stroke = area.channels.find(c => c.name === "stroke");
93+
const {stroke} = area.channels;
9494
assert.strictEqual(stroke.value, "x");
9595
assert.strictEqual(stroke.scale, "color");
9696
});
9797

9898
it("area(data, {stroke}) implies a default z channel if stroke is variable", () => {
9999
const area = Plot.area(undefined, {x1: "0", y1: "1", stroke: "2"});
100-
const z = area.channels.find(c => c.name === "z");
100+
const {z} = area.channels;
101101
assert.strictEqual(z.value, "2");
102102
assert.strictEqual(z.scale, undefined);
103103
});
@@ -109,26 +109,26 @@ it("area(data, {curve}) specifies a named curve or function", () => {
109109

110110
it("areaX(data, {x, y}) defaults x1 to zero, x2 to x, and y1 to y", () => {
111111
const area = Plot.areaX(undefined, {x: "0", y: "1"});
112-
const x1 = area.channels.find(c => c.name === "x1");
112+
const {x1} = area.channels;
113113
// assert.strictEqual(x1.value, 0);
114114
assert.strictEqual(x1.scale, "x");
115-
const x2 = area.channels.find(c => c.name === "x2");
115+
const {x2} = area.channels;
116116
assert.strictEqual(x2.value.label, "0");
117117
assert.strictEqual(x2.scale, "x");
118-
const y1 = area.channels.find(c => c.name === "y1");
118+
const {y1} = area.channels;
119119
assert.strictEqual(y1.value, "1");
120120
assert.strictEqual(y1.scale, "y");
121121
});
122122

123123
it("areaY(data, {x, y}) defaults x1 to x, y1 to zero, and y2 to y", () => {
124124
const area = Plot.areaY(undefined, {x: "0", y: "1"});
125-
const x1 = area.channels.find(c => c.name === "x1");
125+
const {x1} = area.channels;
126126
assert.strictEqual(x1.value, "0");
127127
assert.strictEqual(x1.scale, "x");
128-
const y1 = area.channels.find(c => c.name === "y1");
128+
const {y1} = area.channels;
129129
// assert.strictEqual(y1.value, 0);
130130
assert.strictEqual(y1.scale, "y");
131-
const y2 = area.channels.find(c => c.name === "y2");
131+
const {y2} = area.channels;
132132
assert.strictEqual(y2.value.label, "1");
133133
assert.strictEqual(y2.scale, "y");
134134
});

test/marks/bar-test.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ it("barX() has the expected defaults", () => {
55
const bar = Plot.barX();
66
assert.strictEqual(bar.data, undefined);
77
// assert.strictEqual(bar.transform, undefined);
8-
assert.deepStrictEqual(bar.channels.map(c => c.name), ["x1", "x2", "y"]);
9-
// assert.deepStrictEqual(bar.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[0, 0, 0], [1, 2, 3]]);
10-
assert.deepStrictEqual(bar.channels.map(c => c.scale), ["x", "x", "y"]);
8+
assert.deepStrictEqual(Object.keys(bar.channels), ["x1", "x2", "y"]);
9+
// assert.deepStrictEqual(Object.values(bar.channels).map(c => Plot.valueof([1, 2, 3], c.value)), [[0, 0, 0], [1, 2, 3]]);
10+
assert.deepStrictEqual(Object.values(bar.channels).map(c => c.scale), ["x", "x", "y"]);
1111
assert.strictEqual(bar.fill, undefined);
1212
assert.strictEqual(bar.fillOpacity, undefined);
1313
assert.strictEqual(bar.stroke, undefined);
@@ -28,15 +28,15 @@ it("barX() has the expected defaults", () => {
2828

2929
it("barX(data, {y}) uses a band scale", () => {
3030
const bar = Plot.barX(undefined, {y: "x"});
31-
assert.deepStrictEqual(bar.channels.map(c => c.name), ["x1", "x2", "y"]);
32-
assert.deepStrictEqual(bar.channels.map(c => c.scale), ["x", "x", "y"]);
33-
assert.strictEqual(bar.channels.find(c => c.name === "y").type, "band");
34-
assert.strictEqual(bar.channels.find(c => c.name === "y").value.label, "x");
31+
assert.deepStrictEqual(Object.keys(bar.channels), ["x1", "x2", "y"]);
32+
assert.deepStrictEqual(Object.values(bar.channels).map(c => c.scale), ["x", "x", "y"]);
33+
assert.strictEqual(bar.channels.y.type, "band");
34+
assert.strictEqual(bar.channels.y.value.label, "x");
3535
});
3636

3737
it("barX(data, {title}) specifies an optional title channel", () => {
3838
const bar = Plot.barX(undefined, {title: "x"});
39-
const title = bar.channels.find(c => c.name === "title");
39+
const {title} = bar.channels;
4040
assert.strictEqual(title.value, "x");
4141
assert.strictEqual(title.scale, undefined);
4242
});
@@ -54,7 +54,7 @@ it("barX(data, {fill}) allows fill to be null", () => {
5454
it("barX(data, {fill}) allows fill to be a variable color", () => {
5555
const bar = Plot.barX(undefined, {fill: "x"});
5656
assert.strictEqual(bar.fill, undefined);
57-
const fill = bar.channels.find(c => c.name === "fill");
57+
const {fill} = bar.channels;
5858
assert.strictEqual(fill.value, "x");
5959
assert.strictEqual(fill.scale, "color");
6060
});
@@ -72,20 +72,20 @@ it("barX(data, {stroke}) allows stroke to be null", () => {
7272
it("barX(data, {stroke}) allows stroke to be a variable color", () => {
7373
const bar = Plot.barX(undefined, {stroke: "x"});
7474
assert.strictEqual(bar.stroke, undefined);
75-
const stroke = bar.channels.find(c => c.name === "stroke");
75+
const {stroke} = bar.channels;
7676
assert.strictEqual(stroke.value, "x");
7777
assert.strictEqual(stroke.scale, "color");
7878
});
7979

8080
it("barX(data, {x, y}) defaults x1 to zero and x2 to x", () => {
8181
const bar = Plot.barX(undefined, {x: "0", y: "1"});
82-
const x1 = bar.channels.find(c => c.name === "x1");
82+
const {x1} = bar.channels;
8383
// assert.strictEqual(x1.value, 0);
8484
assert.strictEqual(x1.scale, "x");
85-
const x2 = bar.channels.find(c => c.name === "x2");
85+
const {x2} = bar.channels;
8686
assert.strictEqual(x2.value.label, "0");
8787
assert.strictEqual(x2.scale, "x");
88-
const y = bar.channels.find(c => c.name === "y");
88+
const {y} = bar.channels;
8989
assert.strictEqual(y.value.label, "1");
9090
assert.strictEqual(y.scale, "y");
9191
});
@@ -99,9 +99,9 @@ it("barY() has the expected defaults", () => {
9999
const bar = Plot.barY();
100100
assert.strictEqual(bar.data, undefined);
101101
// assert.strictEqual(bar.transform, undefined);
102-
assert.deepStrictEqual(bar.channels.map(c => c.name), ["y1", "y2", "x"]);
103-
// assert.deepStrictEqual(bar.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[0, 0, 0], [1, 2, 3]]);
104-
assert.deepStrictEqual(bar.channels.map(c => c.scale), ["y", "y", "x"]);
102+
assert.deepStrictEqual(Object.keys(bar.channels), ["y1", "y2", "x"]);
103+
// assert.deepStrictEqual(Object.values(bar.channels).map(c => Plot.valueof([1, 2, 3], c.value)), [[0, 0, 0], [1, 2, 3]]);
104+
assert.deepStrictEqual(Object.values(bar.channels).map(c => c.scale), ["y", "y", "x"]);
105105
assert.strictEqual(bar.fill, undefined);
106106
assert.strictEqual(bar.fillOpacity, undefined);
107107
assert.strictEqual(bar.stroke, undefined);
@@ -122,15 +122,15 @@ it("barY() has the expected defaults", () => {
122122

123123
it("barY(data, {x}) uses a band scale", () => {
124124
const bar = Plot.barY(undefined, {x: "y"});
125-
assert.deepStrictEqual(bar.channels.map(c => c.name), ["y1", "y2", "x"]);
126-
assert.deepStrictEqual(bar.channels.map(c => c.scale), ["y", "y", "x"]);
127-
assert.strictEqual(bar.channels.find(c => c.name === "x").type, "band");
128-
assert.strictEqual(bar.channels.find(c => c.name === "x").value.label, "y");
125+
assert.deepStrictEqual(Object.keys(bar.channels), ["y1", "y2", "x"]);
126+
assert.deepStrictEqual(Object.values(bar.channels).map(c => c.scale), ["y", "y", "x"]);
127+
assert.strictEqual(bar.channels.x.type, "band");
128+
assert.strictEqual(bar.channels.x.value.label, "y");
129129
});
130130

131131
it("barY(data, {title}) specifies an optional title channel", () => {
132132
const bar = Plot.barY(undefined, {title: "x"});
133-
const title = bar.channels.find(c => c.name === "title");
133+
const {title} = bar.channels;
134134
assert.strictEqual(title.value, "x");
135135
assert.strictEqual(title.scale, undefined);
136136
});
@@ -148,7 +148,7 @@ it("barY(data, {fill}) allows fill to be null", () => {
148148
it("barY(data, {fill}) allows fill to be a variable color", () => {
149149
const bar = Plot.barY(undefined, {fill: "x"});
150150
assert.strictEqual(bar.fill, undefined);
151-
const fill = bar.channels.find(c => c.name === "fill");
151+
const {fill} = bar.channels;
152152
assert.strictEqual(fill.value, "x");
153153
assert.strictEqual(fill.scale, "color");
154154
});
@@ -166,20 +166,20 @@ it("barY(data, {stroke}) allows stroke to be null", () => {
166166
it("barY(data, {stroke}) allows stroke to be a variable color", () => {
167167
const bar = Plot.barY(undefined, {stroke: "x"});
168168
assert.strictEqual(bar.stroke, undefined);
169-
const stroke = bar.channels.find(c => c.name === "stroke");
169+
const {stroke} = bar.channels;
170170
assert.strictEqual(stroke.value, "x");
171171
assert.strictEqual(stroke.scale, "color");
172172
});
173173

174174
it("barY(data, {x, y}) defaults y1 to zero and y2 to y", () => {
175175
const bar = Plot.barY(undefined, {x: "0", y: "1"});
176-
const x = bar.channels.find(c => c.name === "x");
176+
const {x} = bar.channels;
177177
assert.strictEqual(x.value.label, "0");
178178
assert.strictEqual(x.scale, "x");
179-
const y1 = bar.channels.find(c => c.name === "y1");
179+
const {y1} = bar.channels;
180180
// assert.strictEqual(y1.value, 0);
181181
assert.strictEqual(y1.scale, "y");
182-
const y2 = bar.channels.find(c => c.name === "y2");
182+
const {y2} = bar.channels;
183183
assert.strictEqual(y2.value.label, "1");
184184
assert.strictEqual(y2.scale, "y");
185185
});

test/marks/cell-test.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ it("cell() has the expected defaults", () => {
55
const cell = Plot.cell();
66
assert.strictEqual(cell.data, undefined);
77
assert.strictEqual(cell.transform, undefined);
8-
assert.deepStrictEqual(cell.channels.map(c => c.name), ["x", "y"]);
9-
assert.deepStrictEqual(cell.channels.map(c => Plot.valueof([[1, 2], [3, 4]], c.value)), [[1, 3], [2, 4]]);
10-
assert.deepStrictEqual(cell.channels.map(c => c.scale), ["x", "y"]);
11-
assert.strictEqual(cell.channels.find(c => c.name === "x").type, "band");
12-
assert.strictEqual(cell.channels.find(c => c.name === "y").type, "band");
8+
assert.deepStrictEqual(Object.keys(cell.channels), ["x", "y"]);
9+
assert.deepStrictEqual(Object.values(cell.channels).map(c => Plot.valueof([[1, 2], [3, 4]], c.value)), [[1, 3], [2, 4]]);
10+
assert.deepStrictEqual(Object.values(cell.channels).map(c => c.scale), ["x", "y"]);
11+
assert.strictEqual(cell.channels.x.type, "band");
12+
assert.strictEqual(cell.channels.y.type, "band");
1313
assert.strictEqual(cell.fill, undefined);
1414
assert.strictEqual(cell.fillOpacity, undefined);
1515
assert.strictEqual(cell.stroke, undefined);
@@ -30,7 +30,7 @@ it("cell() has the expected defaults", () => {
3030

3131
it("cell(data, {title}) specifies an optional title channel", () => {
3232
const cell = Plot.cell(undefined, {title: "x"});
33-
const title = cell.channels.find(c => c.name === "title");
33+
const {title} = cell.channels;
3434
assert.strictEqual(title.value, "x");
3535
assert.strictEqual(title.scale, undefined);
3636
});
@@ -48,7 +48,7 @@ it("cell(data, {fill}) allows fill to be null", () => {
4848
it("cell(data, {fill}) allows fill to be a variable color", () => {
4949
const cell = Plot.cell(undefined, {fill: "x"});
5050
assert.strictEqual(cell.fill, undefined);
51-
const fill = cell.channels.find(c => c.name === "fill");
51+
const {fill} = cell.channels;
5252
assert.strictEqual(fill.value, "x");
5353
assert.strictEqual(fill.scale, "color");
5454
});
@@ -66,7 +66,7 @@ it("cell(data, {stroke}) allows stroke to be null", () => {
6666
it("cell(data, {stroke}) allows stroke to be a variable color", () => {
6767
const cell = Plot.cell(undefined, {stroke: "x"});
6868
assert.strictEqual(cell.stroke, undefined);
69-
const stroke = cell.channels.find(c => c.name === "stroke");
69+
const {stroke} = cell.channels;
7070
assert.strictEqual(stroke.value, "x");
7171
assert.strictEqual(stroke.scale, "color");
7272
});
@@ -75,18 +75,18 @@ it("cellX() defaults x to identity and y to null", () => {
7575
const cell = Plot.cellX();
7676
assert.strictEqual(cell.data, undefined);
7777
assert.strictEqual(cell.transform, undefined);
78-
assert.deepStrictEqual(cell.channels.map(c => c.name), ["x", "fill"]);
79-
assert.deepStrictEqual(cell.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[ 0, 1, 2 ], [ 1, 2, 3 ]]);
80-
assert.deepStrictEqual(cell.channels.map(c => c.scale), ["x", "color"]);
81-
assert.strictEqual(cell.channels.find(c => c.name === "x").type, "band");
78+
assert.deepStrictEqual(Object.keys(cell.channels), ["x", "fill"]);
79+
assert.deepStrictEqual(Object.values(cell.channels).map(c => Plot.valueof([1, 2, 3], c.value)), [[ 0, 1, 2 ], [ 1, 2, 3 ]]);
80+
assert.deepStrictEqual(Object.values(cell.channels).map(c => c.scale), ["x", "color"]);
81+
assert.strictEqual(cell.channels.x.type, "band");
8282
});
8383

8484
it("cellY() defaults y to identity and x to null", () => {
8585
const cell = Plot.cellY();
8686
assert.strictEqual(cell.data, undefined);
8787
assert.strictEqual(cell.transform, undefined);
88-
assert.deepStrictEqual(cell.channels.map(c => c.name), ["y", "fill"]);
89-
assert.deepStrictEqual(cell.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[ 0, 1, 2 ], [ 1, 2, 3 ]]);
90-
assert.deepStrictEqual(cell.channels.map(c => c.scale), ["y", "color"]);
91-
assert.strictEqual(cell.channels.find(c => c.name === "y").type, "band");
88+
assert.deepStrictEqual(Object.keys(cell.channels), ["y", "fill"]);
89+
assert.deepStrictEqual(Object.values(cell.channels).map(c => Plot.valueof([1, 2, 3], c.value)), [[ 0, 1, 2 ], [ 1, 2, 3 ]]);
90+
assert.deepStrictEqual(Object.values(cell.channels).map(c => c.scale), ["y", "color"]);
91+
assert.strictEqual(cell.channels.y.type, "band");
9292
});

0 commit comments

Comments
 (0)