Skip to content

Commit 4169fcf

Browse files
committed
simplify: always compute scales
1 parent 295942e commit 4169fcf

File tree

3 files changed

+37
-42
lines changed

3 files changed

+37
-42
lines changed

src/plot.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ function autoHeight({y, fy, fx}) {
143143

144144
// Wrap the plot in a figure with a caption, if desired.
145145
function wrap(svg, scaleDescriptors, {caption} = {}) {
146-
const scales = (key) => exposeScales(scaleDescriptors, key);
146+
const scales = exposeScales(scaleDescriptors);
147147
const legends = [];
148148
for (let key in scaleDescriptors) {
149149
const {legend} = scaleDescriptors[key];
150150
if (typeof legend === "function") {
151-
const l = legend(scales(key));
151+
const l = legend(scales[key]);
152152
if (l instanceof Node) legends.push(l);
153153
}
154154
}

src/scales.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,11 @@ function asOrdinalType(key) {
114114
}
115115

116116
// prepare scales for exposure through the plot's scales() function
117-
export function exposeScales(scaleDescriptors, key) {
118-
if (key === undefined) {
119-
return Object.fromEntries(
120-
Object.entries(scaleDescriptors)
121-
.map(([key, descriptor]) => [key, exposeScale(descriptor)])
122-
);
123-
}
124-
if (key in scaleDescriptors) {
125-
return exposeScale(scaleDescriptors[key]);
126-
}
117+
export function exposeScales(scaleDescriptors) {
118+
return Object.fromEntries(
119+
Object.entries(scaleDescriptors)
120+
.map(([key, descriptor]) => [key, exposeScale(descriptor)])
121+
);
127122
}
128123

129124
function exposeScale({scale, label}) {

test/scales/scales-test.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import {JSDOM} from "jsdom";
44
const {window} = new JSDOM("");
55
global.document = window.document;
66

7-
tape("plot(…).scales() exposes the plot’s scales", test => {
7+
tape("plot(…).scales exposes the plot’s scales", test => {
88
const plot = Plot.dot([1, 2], {x: d => d, y: d => d}).plot();
9-
test.equal(typeof plot.scales, "function");
10-
const scales = plot.scales();
9+
test.equal(typeof plot.scales, "object");
10+
const scales = plot.scales;
1111
test.equal(Object.entries(scales).length, 2);
1212
test.assert("x" in scales);
1313
test.assert("y" in scales);
1414
});
1515

16-
tape("plot(…).scales('x') exposes the plot’s x scale", test => {
17-
const x = Plot.dot([1, 2], {x: d => d}).plot().scales('x');
16+
tape("plot(…).scales.x exposes the plot’s x scale", test => {
17+
const x = Plot.dot([1, 2], {x: d => d}).plot().scales.x;
1818
test.deepEqual(x.domain, [1, 2]);
1919
test.deepEqual(x.range, [20, 620]);
2020
test.equal(typeof x.interpolate, "function");
@@ -23,10 +23,10 @@ tape("plot(…).scales('x') exposes the plot’s x scale", test => {
2323
test.equal(typeof x.scale, "function");
2424
});
2525

26-
tape("plot(…).scales('y') exposes the plot’s y scale", test => {
27-
const y0 = Plot.dot([1, 2], {x: d => d}).plot().scales('y');
26+
tape("plot(…).scales.y exposes the plot’s y scale", test => {
27+
const y0 = Plot.dot([1, 2], {x: d => d}).plot().scales.y;
2828
test.equal(y0, undefined);
29-
const y = Plot.dot([1, 2], {y: d => d}).plot().scales('y');
29+
const y = Plot.dot([1, 2], {y: d => d}).plot().scales.y;
3030
test.deepEqual(y.domain, [1, 2]);
3131
test.deepEqual(y.range, [380, 20]);
3232
test.equal(typeof y.interpolate, "function");
@@ -35,11 +35,11 @@ tape("plot(…).scales('y') exposes the plot’s y scale", test => {
3535
test.equal(typeof y.scale, "function");
3636
});
3737

38-
tape("plot(…).scales('fx') exposes the plot’s fx scale", test => {
39-
const fx0 = Plot.dot([1, 2], {x: d => d}).plot().scales('fx');
38+
tape("plot(…).scales.fx exposes the plot’s fx scale", test => {
39+
const fx0 = Plot.dot([1, 2], {x: d => d}).plot().scales.fx;
4040
test.equal(fx0, undefined);
4141
const data = [1, 2];
42-
const fx = Plot.dot(data, {y: d => d}).plot({facet: {data, x: data}}).scales('fx');
42+
const fx = Plot.dot(data, {y: d => d}).plot({facet: {data, x: data}}).scales.fx;
4343
test.deepEqual(fx.domain, [1, 2]);
4444
test.deepEqual(fx.range, [40, 620]);
4545
test.equal(typeof fx.interpolate, "undefined");
@@ -48,11 +48,11 @@ tape("plot(…).scales('fx') exposes the plot’s fx scale", test => {
4848
test.equal(typeof fx.scale, "function");
4949
});
5050

51-
tape("plot(…).scales('fy') exposes the plot’s fy scale", test => {
52-
const fy0 = Plot.dot([1, 2], {x: d => d}).plot().scales('fy');
51+
tape("plot(…).scales.fy exposes the plot’s fy scale", test => {
52+
const fy0 = Plot.dot([1, 2], {x: d => d}).plot().scales.fy;
5353
test.equal(fy0, undefined);
5454
const data = [1, 2];
55-
const fy = Plot.dot(data, {y: d => d}).plot({facet: {data, y: data}}).scales('fy');
55+
const fy = Plot.dot(data, {y: d => d}).plot({facet: {data, y: data}}).scales.fy;
5656
test.deepEqual(fy.domain, [1, 2]);
5757
test.deepEqual(fy.range, [20, 380]);
5858
test.equal(typeof fy.interpolate, "undefined");
@@ -61,11 +61,11 @@ tape("plot(…).scales('fy') exposes the plot’s fy scale", test => {
6161
test.equal(typeof fy.scale, "function");
6262
});
6363

64-
tape("plot(…).scales('color') exposes a continuous color scale", test => {
65-
const color0 = Plot.dot([1, 2], {x: d => d}).plot().scales('color');
64+
tape("plot(…).scales.color exposes a continuous color scale", test => {
65+
const color0 = Plot.dot([1, 2], {x: d => d}).plot().scales.color;
6666
test.equal(color0, undefined);
6767
const data = [1, 2, 3, 4, 5];
68-
const color = Plot.dot(data, {y: d => d, fill: d => d}).plot().scales('color');
68+
const color = Plot.dot(data, {y: d => d, fill: d => d}).plot().scales.color;
6969
test.deepEqual(color.domain, [1, 5]);
7070
test.deepEqual(color.range, [0, 1]);
7171
test.equal(typeof color.interpolate, "function");
@@ -74,9 +74,9 @@ tape("plot(…).scales('color') exposes a continuous color scale", test => {
7474
test.equal(typeof color.scale, "function");
7575
});
7676

77-
tape("plot(…).scales('color') exposes an ordinal color scale", test => {
77+
tape("plot(…).scales.color exposes an ordinal color scale", test => {
7878
const data = ["a", "b", "c", "d"];
79-
const color = Plot.dot(data, {y: d => d, fill: d => d}).plot().scales('color');
79+
const color = Plot.dot(data, {y: d => d, fill: d => d}).plot().scales.color;
8080
test.deepEqual(color.domain, data);
8181
test.deepEqual(color.range, ['#4e79a7', '#f28e2c', '#e15759', '#76b7b2', '#59a14f', '#edc949', '#af7aa1', '#ff9da7', '#9c755f', '#bab0ab']);
8282
test.equal(typeof color.interpolate, "undefined");
@@ -85,11 +85,11 @@ tape("plot(…).scales('color') exposes an ordinal color scale", test => {
8585
test.equal(typeof color.scale, "function");
8686
});
8787

88-
tape("plot(…).scales('r') exposes a radius scale", test => {
89-
const r0 = Plot.dot([1, 2], {x: d => d}).plot().scales('r');
88+
tape("plot(…).scales.r exposes a radius scale", test => {
89+
const r0 = Plot.dot([1, 2], {x: d => d}).plot().scales.r;
9090
test.equal(r0, undefined);
9191
const data = [1, 2, 3, 4, 9];
92-
const r = Plot.dot(data, {r: d => d}).plot().scales('r');
92+
const r = Plot.dot(data, {r: d => d}).plot().scales.r;
9393
test.deepEqual(r.domain, [0, 9]);
9494
test.deepEqual(r.range, [0, Math.sqrt(40.5)]);
9595
test.equal(typeof r.interpolate, "function");
@@ -98,11 +98,11 @@ tape("plot(…).scales('r') exposes a radius scale", test => {
9898
test.equal(typeof r.scale, "function");
9999
});
100100

101-
tape("plot(…).scales('opacity') exposes a linear scale", test => {
102-
const opacity0 = Plot.dot([1, 2], {x: d => d}).plot().scales('opacity');
101+
tape("plot(…).scales.opacity exposes a linear scale", test => {
102+
const opacity0 = Plot.dot([1, 2], {x: d => d}).plot().scales.opacity;
103103
test.equal(opacity0, undefined);
104104
const data = [1, 2, 3, 4, 9];
105-
const opacity = Plot.dot(data, {fillOpacity: d => d}).plot().scales('opacity');
105+
const opacity = Plot.dot(data, {fillOpacity: d => d}).plot().scales.opacity;
106106
test.deepEqual(opacity.domain, [0, 9]);
107107
test.deepEqual(opacity.range, [0, 1]);
108108
test.equal(typeof opacity.interpolate, "function");
@@ -133,19 +133,19 @@ tape("plot(…).scales expose label", test => {
133133
});
134134

135135
tape("plot(…).scales expose color label", test => {
136-
const x = Plot.dot([{x: 1}, {x: 2}, {x: 3}], {fill: "x"}).plot().scales("color");
136+
const x = Plot.dot([{x: 1}, {x: 2}, {x: 3}], {fill: "x"}).plot().scales.color;
137137
test.equal(x.label, "x");
138-
const y = Plot.dot([{x: 1}, {x: 2}, {x: 3}], {fill: "x"}).plot({color: {label: "y"}}).scales("color");
138+
const y = Plot.dot([{x: 1}, {x: 2}, {x: 3}], {fill: "x"}).plot({color: {label: "y"}}).scales.color;
139139
test.equal(y.label, "y");
140140
});
141141

142142
tape("plot(…).scales expose radius label", test => {
143-
const x = Plot.dot([{x: 1}, {x: 2}, {x: 3}], {r: "x"}).plot().scales("r");
143+
const x = Plot.dot([{x: 1}, {x: 2}, {x: 3}], {r: "x"}).plot().scales.r;
144144
test.equal(x.label, "x");
145-
const r = Plot.dot([{x: 1}, {x: 2}, {x: 3}], {r: "x"}).plot({color: {label: "radius"}}).scales("color");
145+
const r = Plot.dot([{x: 1}, {x: 2}, {x: 3}], {r: "x"}).plot({r: {label: "radius"}}).scales.r;
146146
test.equal(r.label, "radius");
147147
});
148148

149149
function scaleOpt(x) {
150-
return Plot.dot([{x: 1}, {x: 2}, {x: 3}], {x: "x"}).plot({x}).scales("x");
150+
return Plot.dot([{x: 1}, {x: 2}, {x: 3}], {x: "x"}).plot({x}).scales.x;
151151
}

0 commit comments

Comments
 (0)