Skip to content

Commit 5957c86

Browse files
committed
rebase
1 parent 45d0c97 commit 5957c86

File tree

15 files changed

+58
-59
lines changed

15 files changed

+58
-59
lines changed

src/axes.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import {format, utcFormat} from "d3";
22
import {formatIsoDate} from "./format.js";
33
import {constant, isTemporal, string} from "./options.js";
4-
import {isOrdinalScale} from "./scales.js";
5-
6-
export function inferFontVariant(scale) {
7-
return isOrdinalScale(scale) && scale.interval === undefined ? undefined : "tabular-nums";
8-
}
94

105
// D3 doesn’t provide a tick format for ordinal scales; we want shorthand when
116
// an ordinal domain is numbers or dates, and we want null to mean the empty

src/channel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function createChannels(channels, data) {
2727
export function valueObject(channels, scales) {
2828
const values = Object.fromEntries(
2929
Object.entries(channels).map(([name, {scale: scaleName, value}]) => {
30-
const scale = scaleName == null ? null : scales[scaleName];
30+
const scale = scaleName == null ? null : scales[scaleName]?.apply;
3131
return [name, scale == null ? value : map(value, scale)];
3232
})
3333
);

src/facet.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ export function facetGroups(data, {fx, fy}) {
5555
}
5656

5757
export function facetTranslate(fx, fy, {marginTop, marginLeft}) {
58-
return fx && fy
59-
? ({x, y}) => `translate(${fx(x) - marginLeft},${fy(y) - marginTop})`
60-
: fx
61-
? ({x}) => `translate(${fx(x) - marginLeft},0)`
62-
: ({y}) => `translate(0,${fy(y) - marginTop})`;
58+
return fx?.apply && fy?.apply
59+
? ({x, y}) => `translate(${fx.apply(x) - marginLeft},${fy.apply(y) - marginTop})`
60+
: fx?.apply
61+
? ({x}) => `translate(${fx.apply(x) - marginLeft},0)`
62+
: ({y}) => `translate(0,${fy.apply(y) - marginTop})`;
6363
}
6464

6565
// Returns an index that for each facet lists all the elements present in other

src/legends/ramp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {quantize, interpolateNumber, piecewise, format, scaleBand, scaleLinear, axisBottom} from "d3";
2-
import {inferFontVariant} from "../axes.js";
2+
import {inferFontVariant} from "../marks/axis.js";
33
import {createContext, create} from "../context.js";
44
import {map} from "../options.js";
55
import {interpolatePiecewise} from "../scales/quantitative.js";

src/legends/swatches.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {pathRound as path} from "d3";
2-
import {inferFontVariant, maybeAutoTickFormat} from "../axes.js";
2+
import {inferFontVariant} from "../marks/axis.js";
3+
import {maybeAutoTickFormat} from "../axes.js";
34
import {createContext, create} from "../context.js";
45
import {isNoneish, maybeColorChannel, maybeNumberChannel} from "../options.js";
56
import {isOrdinalScale, isThresholdScale} from "../scales.js";

src/marks/axis.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {marks} from "../mark.js";
44
import {radians} from "../math.js";
55
import {range, valueof, arrayify, constant, keyword, identity, number} from "../options.js";
66
import {isNoneish, isIterable, isTemporal, maybeRangeInterval, orderof} from "../options.js";
7-
import {isTemporalScale} from "../scales.js";
7+
import {isOrdinalScale, isTemporalScale} from "../scales.js";
88
import {offset} from "../style.js";
99
import {initializer} from "../transforms/basic.js";
1010
import {ruleX, ruleY} from "./rule.js";
@@ -136,7 +136,7 @@ function axisKy(
136136
initializer: function (data, facets, channels, scales, dimensions) {
137137
const scale = scales[k];
138138
const {marginTop, marginRight, marginBottom, marginLeft} = (k === "y" && dimensions.inset) || dimensions;
139-
const cla = labelAnchor ?? (scale.bandwidth ? "center" : "top");
139+
const cla = labelAnchor ?? (scale.bandwidth === undefined ? "top" : "center");
140140
const clo = labelOffset ?? (anchor === "right" ? marginRight : marginLeft) - 3;
141141
if (cla === "center") {
142142
this.textAnchor = undefined; // middle
@@ -245,7 +245,7 @@ function axisKx(
245245
initializer: function (data, facets, channels, scales, dimensions) {
246246
const scale = scales[k];
247247
const {marginTop, marginRight, marginBottom, marginLeft} = (k === "x" && dimensions.inset) || dimensions;
248-
const cla = labelAnchor ?? (scale.bandwidth ? "center" : "right");
248+
const cla = labelAnchor ?? (scale.bandwidth === undefined ? "right" : "center");
249249
const clo = labelOffset ?? (anchor === "top" ? marginTop : marginBottom) - 3;
250250
if (cla === "center") {
251251
this.frameAnchor = anchor;
@@ -507,26 +507,26 @@ function axisMark(mark, k, ariaLabel, data, options, initialize) {
507507
if (data == null) {
508508
if (isIterable(ticks)) {
509509
data = arrayify(ticks);
510-
} else if (scale.ticks) {
510+
} else if (scale._tickFunction) {
511511
if (ticks !== undefined) {
512-
data = scale.ticks(ticks);
512+
data = scale._tickFunction(ticks);
513513
} else {
514514
interval = maybeRangeInterval(interval === undefined ? scale.interval : interval, scale.type);
515515
if (interval !== undefined) {
516516
// For time scales, we could pass the interval directly to
517517
// scale.ticks because it’s supported by d3.utcTicks; but
518518
// quantitative scales and d3.ticks do not support numeric
519519
// intervals for scale.ticks, so we compute them here.
520-
const [min, max] = extent(scale.domain());
520+
const [min, max] = extent(scale.domain);
521521
data = interval.range(min, interval.offset(interval.floor(max))); // inclusive max
522522
} else {
523-
const [min, max] = extent(scale.range());
523+
const [min, max] = extent(scale.range);
524524
ticks = (max - min) / (tickSpacing === undefined ? (k === "x" ? 80 : 35) : tickSpacing);
525-
data = scale.ticks(ticks);
525+
data = scale._tickFunction(ticks);
526526
}
527527
}
528528
} else {
529-
data = scale.domain();
529+
data = scale.domain;
530530
}
531531
if (k === "y" || k === "x") {
532532
facets = [range(data)];
@@ -563,12 +563,12 @@ function inferTextChannel(scale, ticks, tickFormat) {
563563
// domain (or ticks) are numbers or dates (say because we’re applying a time
564564
// interval to the ordinal scale), we want Plot’s default formatter.
565565
function inferTickFormat(scale, ticks, tickFormat) {
566-
return scale.tickFormat
567-
? scale.tickFormat(isIterable(ticks) ? null : ticks, tickFormat)
566+
return scale._tickFormat
567+
? scale._tickFormat(isIterable(ticks) ? null : ticks, tickFormat)
568568
: tickFormat === undefined
569569
? formatDefault
570570
: typeof tickFormat === "string"
571-
? (isTemporal(scale.domain()) ? utcFormat : format)(tickFormat)
571+
? (isTemporal(scale.domain) ? utcFormat : format)(tickFormat)
572572
: constant(tickFormat);
573573
}
574574

@@ -600,24 +600,18 @@ const shapeTickRight = {
600600
}
601601
};
602602

603-
// TODO Unify this with the other inferFontVariant; here we only have a scale
604-
// function rather than a scale descriptor.
605-
function inferFontVariant(scale) {
606-
return scale.bandwidth && scale.interval === undefined ? undefined : "tabular-nums";
607-
}
608-
609603
// Determines whether the scale points in the “positive” (right or down) or
610604
// “negative” (left or up) direction; if the scale order cannot be determined,
611605
// returns NaN; used to assign an appropriate label arrow.
612606
function inferScaleOrder(scale) {
613-
return Math.sign(orderof(scale.domain())) * Math.sign(orderof(scale.range()));
607+
return Math.sign(orderof(scale.domain)) * Math.sign(orderof(scale.range));
614608
}
615609

616610
// Takes the scale label, and if this is not an ordinal scale and the label was
617611
// inferred from an associated channel, adds an orientation-appropriate arrow.
618612
function inferAxisLabel(key, scale, labelAnchor) {
619-
const label = scale.label;
620-
if (scale.bandwidth || !label?.inferred) return label;
613+
const label = scale._label;
614+
if (scale.bandwidth !== undefined || !label?.inferred) return label;
621615
const order = inferScaleOrder(scale);
622616
return order
623617
? key === "x" || labelAnchor === "center"
@@ -627,3 +621,7 @@ function inferAxisLabel(key, scale, labelAnchor) {
627621
: `${order < 0 ? "↑ " : "↓ "}${label}`
628622
: label;
629623
}
624+
625+
export function inferFontVariant(scale) {
626+
return isOrdinalScale(scale) && scale.interval === undefined ? undefined : "tabular-nums";
627+
}

src/marks/bar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ export class AbstractBar extends Mark {
5757
}
5858
_width({x}, {x: X}, {marginRight, marginLeft, width}) {
5959
const {insetLeft, insetRight} = this;
60-
const bandwidth = X && x ? x.bandwidth() : width - marginRight - marginLeft;
60+
const bandwidth = X && x ? x.bandwidth : width - marginRight - marginLeft;
6161
return Math.max(0, bandwidth - insetLeft - insetRight);
6262
}
6363
_height({y}, {y: Y}, {marginTop, marginBottom, height}) {
6464
const {insetTop, insetBottom} = this;
65-
const bandwidth = Y && y ? y.bandwidth() : height - marginTop - marginBottom;
65+
const bandwidth = Y && y ? y.bandwidth : height - marginTop - marginBottom;
6666
return Math.max(0, bandwidth - insetTop - insetBottom);
6767
}
6868
}

src/marks/geo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function scaleProjection({x: X, y: Y}) {
6060
Y ??= (y) => y;
6161
return geoTransform({
6262
point(x, y) {
63-
this.stream.point(X(x), Y(y));
63+
this.stream.point(X.apply(x), Y.apply(y));
6464
}
6565
});
6666
}

src/marks/raster.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class Raster extends AbstractRaster {
9898
return super.scale(channels, scales, context);
9999
}
100100
render(index, scales, values, dimensions, context) {
101-
const color = scales[values.channels.fill?.scale] ?? ((x) => x);
101+
const color = scales[values.channels.fill?.scale]?.apply ?? ((x) => x);
102102
const {x: X, y: Y} = values;
103103
const {document} = context;
104104
const [x1, y1, x2, y2] = renderBounds(values, dimensions, context);

src/marks/tick.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class TickX extends AbstractTick {
6363
}
6464
_y2({y}, {y: Y}, {height, marginBottom}) {
6565
const {insetBottom} = this;
66-
return Y && y ? (i) => Y[i] + y.bandwidth() - insetBottom : height - marginBottom - insetBottom;
66+
return Y && y ? (i) => Y[i] + y.bandwidth - insetBottom : height - marginBottom - insetBottom;
6767
}
6868
}
6969

@@ -90,7 +90,7 @@ export class TickY extends AbstractTick {
9090
}
9191
_x2({x}, {x: X}, {width, marginRight}) {
9292
const {insetRight} = this;
93-
return X && x ? (i) => X[i] + x.bandwidth() - insetRight : width - marginRight - insetRight;
93+
return X && x ? (i) => X[i] + x.bandwidth - insetRight : width - marginRight - insetRight;
9494
}
9595
_y1(scales, {y: Y}) {
9696
return (i) => Y[i];

src/plot.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ export function plot(options = {}) {
133133

134134
// Initalize the scales and dimensions.
135135
const scaleDescriptors = createScales(addScaleChannels(channelsByScale, stateByMark), options);
136-
const scales = createScaleFunctions(scaleDescriptors);
137136
const dimensions = createDimensions(scaleDescriptors, marks, options);
138137

139138
autoScaleRange(scaleDescriptors, dimensions);
140139

140+
const scales = createScaleFunctions(scaleDescriptors);
141141
const {fx, fy} = scales;
142142
const subdimensions = fx || fy ? innerDimensions(scaleDescriptors, dimensions) : dimensions;
143143
const superdimensions = fx || fy ? actualDimensions(scales, dimensions) : dimensions;
@@ -235,7 +235,7 @@ export function plot(options = {}) {
235235

236236
// Render facets.
237237
if (facets !== undefined) {
238-
const facetDomains = {x: fx?.domain(), y: fy?.domain()};
238+
const facetDomains = {x: fx?.domain, y: fy?.domain};
239239

240240
// Sort the facets to match the fx and fy domains; this is needed because
241241
// the facets were constructed prior to the fx and fy scales.
@@ -638,9 +638,9 @@ function actualDimensions({fx, fy}, dimensions) {
638638
}
639639

640640
function outerRange(scale) {
641-
const domain = scale.domain();
642-
let x1 = scale(domain[0]);
643-
let x2 = scale(domain[domain.length - 1]);
641+
const {domain} = scale;
642+
let x1 = scale.apply(domain[0]);
643+
let x2 = scale.apply(domain[domain.length - 1]);
644644
if (x2 < x1) [x1, x2] = [x2, x1];
645-
return [x1, x2 + scale.bandwidth()];
645+
return [x1, x2 + scale.bandwidth];
646646
}

src/scales.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,17 @@ export function createScaleFunctions(scales) {
101101
return Object.fromEntries(
102102
Object.entries(scales)
103103
.filter(([, {scale}]) => scale) // drop identity scales
104-
.map(([name, {scale, type, interval, label}]) => {
105-
scale.type = type; // for axis
106-
if (interval != null) scale.interval = interval; // for axis
107-
if (label != null) scale.label = label; // for axis
108-
return [name, scale];
109-
})
104+
.map(([name, scale]) => [
105+
name,
106+
{
107+
...exposeScale(scale),
108+
// for axis
109+
_label: scale.label,
110+
_tickFormat: scale.scale.tickFormat,
111+
_tickFunction: scale.scale.ticks,
112+
...(scale.type === "identity" && {range: slice(scale.range)})
113+
}
114+
])
110115
);
111116
}
112117

@@ -472,10 +477,10 @@ export function isDivergingScale({type}) {
472477
// dimension (whereas a dot will simply be drawn in the center).
473478
export function isCollapsed(scale) {
474479
if (scale === undefined) return true; // treat missing scale as collapsed
475-
const domain = scale.domain();
476-
const value = scale(domain[0]);
480+
const {domain} = scale;
481+
const value = scale.apply(domain[0]);
477482
for (let i = 1, n = domain.length; i < n; ++i) {
478-
if (scale(domain[i]) - value) {
483+
if (scale.apply(domain[i]) - value) {
479484
return false;
480485
}
481486
}

src/style.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ export function applyStyle(selection, name, value) {
401401
export function applyTransform(selection, mark, {x, y}, tx = offset, ty = offset) {
402402
tx += mark.dx;
403403
ty += mark.dy;
404-
if (x?.bandwidth) tx += x.bandwidth() / 2;
405-
if (y?.bandwidth) ty += y.bandwidth() / 2;
404+
if (x?.bandwidth !== undefined) tx += x.bandwidth / 2;
405+
if (y?.bandwidth !== undefined) ty += y.bandwidth / 2;
406406
if (tx || ty) selection.attr("transform", `translate(${tx},${ty})`);
407407
}
408408

src/transforms/dodge.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function dodge(y, x, anchor, padding, options) {
7070
if (!channels[x]) throw new Error(`missing channel: ${x}`);
7171
({[x]: X} = applyPosition(channels, scales, context));
7272
const r = R ? undefined : this.r !== undefined ? this.r : options.r !== undefined ? number(options.r) : 3;
73-
if (R) R = valueof(R.value, scales[R.scale] || identity, Float64Array);
73+
if (R) R = valueof(R.value, scales[R.scale]?.apply || identity, Float64Array);
7474
let [ky, ty] = anchor(dimensions);
7575
const compare = ky ? compareAscending : compareSymmetric;
7676
const Y = new Float64Array(X.length);

test/transforms/remap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function remap(outputs = {}, options) {
1111
if (!channel) throw new Error(`missing channel: ${name}`);
1212
const V = Array.from(channel.value);
1313
const n = V.length;
14-
const scale = scales[channel.scale];
14+
const scale = scales[channel.scale]?.apply;
1515
if (scale) for (let i = 0; i < n; ++i) V[i] = scale(V[i]);
1616
for (let i = 0; i < n; ++i) V[i] = map(V[i], i, V);
1717
return [name, {value: V}];

0 commit comments

Comments
 (0)