optional channels · Salebarn/plot@58cd739 · GitHub
Skip to content

Commit 58cd739

Browse files
committed
optional channels
1 parent 53edfbd commit 58cd739

14 files changed

Lines changed: 155 additions & 139 deletions

File tree

src/axes.js

Lines changed: 17 additions & 17 deletions

src/mark.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
export class Mark {
2-
constructor(data, channels) {
2+
constructor(data, channels = []) {
33
this.data = data;
4-
this.channels = channels = Object.fromEntries(Array.from(
5-
Object.entries(channels).filter(([, channel]) => channel),
6-
([name, channel]) => [name, Channel(data, channel)]
7-
));
4+
this.scaleChannels = [];
5+
this.channels = Object.fromEntries(channels
6+
.filter(channel => {
7+
const {name, value, optional} = channel;
8+
if (value === undefined) {
9+
if (optional) return false;
10+
throw new Error(`missing channel value: ${name}`);
11+
}
12+
return true;
13+
})
14+
.map(channel => {
15+
const {name} = channel;
16+
channel = Channel(data, channel);
17+
if (channel.scale) this.scaleChannels.push(channel);
18+
return [name, channel];
19+
})
20+
.filter(([name]) => name));
821
}
922
}
1023

11-
function Channel(data, {scale = null, type, value, label}) {
24+
function Channel(data, {scale, type, value, label}) {
1225
if (typeof value === "string") label = value, value = Array.from(data, Field(value));
1326
else if (typeof value === "function") value = Array.from(data, value);
1427
else if (typeof value.length !== "number") value = Array.from(value);

src/marks/area.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class Area extends Mark {
2222
) {
2323
super(
2424
data,
25-
{
26-
x1: {value: x1, scale: "x"},
27-
y1: {value: y1, scale: "y"},
28-
x2: x2 && {value: x2, scale: "x"},
29-
y2: y2 && {value: y2, scale: "y"}
30-
}
25+
[
26+
{name: "x1", value: x1, scale: "x"},
27+
{name: "y1", value: y1, scale: "y"},
28+
{name: "x2", value: x2, scale: "x", optional: true},
29+
{name: "y2", value: y2, scale: "y", optional: true}
30+
]
3131
);
3232
this.curve = Curve(curve);
3333
this.fill = fill;
@@ -60,12 +60,12 @@ class Area extends Mark {
6060

6161
export class AreaX extends Area {
6262
constructor(data, {x = identity, x1 = zero, x2 = x, y = indexOf} = {}, style) {
63-
super(data, {x1, x2, y1: y, y2: null}, style);
63+
super(data, {x1, x2, y1: y}, style);
6464
}
6565
}
6666

6767
export class AreaY extends Area {
6868
constructor(data, {x = indexOf, y = identity, y1 = zero, y2 = y} = {}, style) {
69-
super(data, {x1: x, x2: null, y1, y2}, style);
69+
super(data, {x1: x, y1, y2}, style);
7070
}
7171
}

src/marks/bar.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ export class BarX extends Bar {
6767
constructor(data, {x = identity, y = indexOf} = {}, style) {
6868
super(
6969
data,
70-
{
71-
x: {value: x, scale: "x"},
72-
y: {value: y, scale: "y", type: "band"},
73-
_: {value: [0], scale: "x"} // ensure the x-domain includes zero
74-
},
70+
[
71+
{name: "x", value: x, scale: "x"},
72+
{name: "y", value: y, scale: "y", type: "band"},
73+
{value: [0], scale: "x"} // ensure the x-domain includes zero
74+
],
7575
style
7676
);
7777
}
@@ -97,11 +97,11 @@ export class BarY extends Bar {
9797
constructor(data, {x = indexOf, y = identity} = {}, style) {
9898
super(
9999
data,
100-
{
101-
x: {value: x, scale: "x", type: "band"},
102-
y: {value: y, scale: "y"},
103-
_: {value: [0], scale: "y"} // ensure the y-domain includes zero
104-
},
100+
[
101+
{name: "x", value: x, scale: "x", type: "band"},
102+
{name: "y", value: y, scale: "y"},
103+
{value: [0], scale: "y"} // ensure the y-domain includes zero
104+
],
105105
style
106106
);
107107
}

src/marks/dot.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ export class DotXY extends Mark {
2727
) {
2828
super(
2929
data,
30-
{
31-
x: {value: x, scale: "x"},
32-
y: {value: y, scale: "y"},
33-
r: r && {value: r, scale: "r"},
34-
fill: fill && {value: fill, scale: "color"},
35-
stroke: stroke && {value: stroke, scale: "color"}
36-
}
30+
[
31+
{name: "x", value: x, scale: "x"},
32+
{name: "y", value: y, scale: "y"},
33+
{name: "r", value: r, scale: "r", optional: true},
34+
{name: "fill", value: fill, scale: "color", optional: true},
35+
{name: "stroke", value: stroke, scale: "color", optional: true}
36+
]
3737
);
3838
this.r = fixedR;
3939
this.fill = fixedFill;

src/marks/facet.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {group} from "d3-array";
22
import {create} from "d3-selection";
33
import {Mark, indexOf} from "../mark.js";
4-
import {Channels} from "../plot.js";
4+
import {ScaleChannels} from "../plot.js";
55
import {Scales, autoScaleRange} from "../scales.js";
66

77
export class FacetY extends Mark {
@@ -15,17 +15,17 @@ export class FacetY extends Mark {
1515
) {
1616
super(
1717
data,
18-
{
19-
x: x && {value: x, scale: "x"},
20-
y: {value: y, scale: "y", type: "band"}
21-
}
18+
[
19+
{name: "x", value: x, scale: "x", optional: true},
20+
{name: "y", value: y, scale: "y", type: "band"}
21+
]
2222
);
2323
this.options = options;
2424
}
2525
render(I, {y: {scale: y, domain}, ...scales}, dimensions) {
2626
const {data, options, channels: {y: {value: Y}}} = this;
2727
const {marks: submarks = []} = options;
28-
const subchannels = Channels(submarks);
28+
const subchannels = ScaleChannels(submarks);
2929
const subscales = {...Scales(subchannels, options.scales), ...scales};
3030
const subdimensions = {...dimensions, marginTop: 0, marginBottom: 0, height: y.bandwidth()};
3131
const G = group(I, i => Y[i]);

src/marks/line.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ class Line extends Mark {
3030
) {
3131
super(
3232
data,
33-
{
34-
x: {value: x, scale: "x"},
35-
y: {value: y, scale: "y"},
36-
z: z && {value: z}
37-
}
33+
[
34+
{name: "x", value: x, scale: "x"},
35+
{name: "y", value: y, scale: "y"},
36+
{name: "z", value: z, optional: true}
37+
]
3838
);
3939
this.curve = Curve(curve);
4040
this.fill = fill;

src/marks/rect.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ export class RectXY extends Mark {
2727
) {
2828
super(
2929
data,
30-
{
31-
x1: {value: x1, scale: "x"},
32-
y1: {value: y1, scale: "y"},
33-
x2: {value: x2, scale: "x"},
34-
y2: {value: y2, scale: "y"}
35-
}
30+
[
31+
{name: "x1", value: x1, scale: "x"},
32+
{name: "y1", value: y1, scale: "y"},
33+
{name: "x2", value: x2, scale: "x"},
34+
{name: "y2", value: y2, scale: "y"}
35+
]
3636
);
3737
this.fill = fill;
3838
this.fillOpacity = fillOpacity;

src/marks/rule.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ export class RuleX extends Mark {
1919
) {
2020
super(
2121
data,
22-
{
23-
x: {value: x, scale: "x"},
24-
y1: y1 && {value: y1, scale: "y"},
25-
y2: y2 && {value: y2, scale: "y"},
26-
stroke: stroke && {value: stroke, scale: "color"}
27-
}
22+
[
23+
{name: "x", value: x, scale: "x"},
24+
{name: "y1", value: y1, scale: "y", optional: true},
25+
{name: "y2", value: y2, scale: "y", optional: true},
26+
{name: "stroke", value: stroke, scale: "color", optional: true}
27+
]
2828
);
2929
this.stroke = fixedStroke;
3030
this.strokeWidth = strokeWidth;
@@ -83,12 +83,12 @@ export class RuleY extends Mark {
8383
) {
8484
super(
8585
data,
86-
{
87-
x1: x1 && {value: x1, scale: "x"},
88-
x2: x2 && {value: x2, scale: "x"},
89-
y: {value: y, scale: "y"},
90-
stroke: stroke && {value: stroke, scale: "color"}
91-
}
86+
[
87+
{name: "y", value: y, scale: "y"},
88+
{name: "x1", value: x1, scale: "x", optional: true},
89+
{name: "x2", value: x2, scale: "x", optional: true},
90+
{name: "stroke", value: stroke, scale: "color", optional: true}
91+
]
9292
);
9393
this.stroke = fixedStroke;
9494
this.strokeWidth = strokeWidth;
@@ -97,8 +97,8 @@ export class RuleY extends Mark {
9797
render(
9898
I,
9999
{
100-
x: {scale: x} = {},
101100
y: {scale: y},
101+
x: {scale: x} = {},
102102
color: {scale: color} = {}
103103
},
104104
{width, marginLeft, marginRight}
@@ -108,9 +108,9 @@ export class RuleY extends Mark {
108108
strokeWidth,
109109
strokeOpacity,
110110
channels: {
111+
y: {value: Y},
111112
x1: {value: X1} = {},
112113
x2: {value: X2} = {},
113-
y: {value: Y},
114114
stroke: {value: S} = {}
115115
}
116116
} = this;

src/plot.js

Lines changed: 3 additions & 6 deletions

0 commit comments

Comments
 (0)