Skip to content

Commit a8ee323

Browse files
committed
inline & clean up
1 parent 63070d9 commit a8ee323

File tree

3 files changed

+59
-66
lines changed

3 files changed

+59
-66
lines changed

src/dimensions.js

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
import {extent, max} from "d3";
2-
import {createProjection, projectionAspectRatio} from "./projection.js";
3-
import {createScales, isOrdinalScale} from "./scales.js";
2+
import {projectionAspectRatio} from "./projection.js";
3+
import {isOrdinalScale} from "./scales.js";
44
import {offset} from "./style.js";
55
import {defaultWidth, monospaceWidth} from "./marks/text.js";
6-
import {createScaleFunctions, autoScaleRange, innerDimensions, outerDimensions} from "./scales.js";
6+
import {outerDimensions} from "./scales.js";
77

88
const marginMedium = 60;
99
const marginLarge = 90;
1010

1111
// When axes have "auto" margins, we might need to adjust the margins, after
1212
// seeing the actual tick labels. In that case we’ll compute the dimensions and
1313
// scales a second time.
14-
function autoMarginK(margin, {k: scale, labelAnchor, label}, options, mark, stateByMark, scales, dimensions, context) {
14+
export function autoMarginK(
15+
margin,
16+
{scale, labelAnchor, label},
17+
options,
18+
mark,
19+
stateByMark,
20+
scales,
21+
dimensions,
22+
context
23+
) {
1524
let {data, facets, channels} = stateByMark.get(mark);
1625
if (mark.initializer) ({channels} = mark.initializer(data, facets, {}, scales, dimensions, context));
17-
const width = mark.monospace ? (d) => (monospaceWidth(d) * 3) / 5 : defaultWidth;
18-
const actualLabel = label ?? scales[scale].label;
26+
const width = mark.monospace ? monospaceWidth : defaultWidth;
27+
const labelPenalty =
28+
(label ?? scales[scale].label ?? "") !== "" &&
29+
(labelAnchor === "center" || (labelAnchor == null && scales[scale].bandwidth));
1930
const l =
20-
max(channels.text.value, (t) => (t ? width(`${t}`) : NaN)) +
21-
(actualLabel != null &&
22-
actualLabel !== "" &&
23-
((labelAnchor == null && scales[scale].bandwidth) || labelAnchor === "center")
24-
? 100
25-
: 0);
31+
max(channels.text.value, (t) => (t ? width(`${t}`) : NaN)) * (mark.monospace ? 0.6 : 1) + (labelPenalty ? 100 : 0);
2632
const m = l >= 500 ? marginLarge : l >= 295 ? marginMedium : null;
2733
return m === null
2834
? options
@@ -31,46 +37,7 @@ function autoMarginK(margin, {k: scale, labelAnchor, label}, options, mark, stat
3137
: {[margin]: m, ...options};
3238
}
3339

34-
export function createDimensionsScales(channels, marks, stateByMark, options, context) {
35-
const scaleDescriptors = createScales(channels, options);
36-
const {dimensions, autoMargins} = createDimensions(scaleDescriptors, marks, options);
37-
autoScaleRange(scaleDescriptors, dimensions); // !! mutates scales ranges…
38-
const scales = createScaleFunctions(scaleDescriptors);
39-
const {fx, fy} = scales;
40-
const subdimensions = fx || fy ? innerDimensions(scaleDescriptors, dimensions) : dimensions;
41-
const superdimensions = fx || fy ? actualDimensions(scaleDescriptors, dimensions) : dimensions;
42-
context.projection = createProjection(options, subdimensions);
43-
44-
// Review the auto margins and create new scales if more space is needed.
45-
const originalOptions = options;
46-
for (const [margin, scale, mark] of autoMargins) {
47-
options = autoMarginK(
48-
margin,
49-
scale,
50-
options,
51-
mark,
52-
stateByMark,
53-
scales,
54-
mark.facet === "super" ? superdimensions : subdimensions,
55-
context
56-
);
57-
}
58-
if (options !== originalOptions) {
59-
const scaleDescriptors = createScales(channels, options);
60-
const {dimensions} = createDimensions(scaleDescriptors, marks, options);
61-
autoScaleRange(scaleDescriptors, dimensions);
62-
const scales = createScaleFunctions(scaleDescriptors);
63-
const {fx, fy} = scaleDescriptors;
64-
const subdimensions = fx || fy ? innerDimensions(scaleDescriptors, dimensions) : dimensions;
65-
const superdimensions = fx || fy ? actualDimensions(scaleDescriptors, dimensions) : dimensions;
66-
context.projection = createProjection(options, subdimensions);
67-
return {scaleDescriptors, scales, dimensions, subdimensions, superdimensions};
68-
}
69-
70-
return {scaleDescriptors, scales, dimensions, subdimensions, superdimensions};
71-
}
72-
73-
function createDimensions(scales, marks, options = {}) {
40+
export function createDimensions(scales, marks, options = {}) {
7441
// Compute the default margins: the maximum of the marks’ margins. While not
7542
// always used, they may be needed to compute the default height of the plot.
7643
let marginTopDefault = 0.5 - offset,

src/marks/axis.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ function axisKy(
100100
const autoMarginRight = margin === undefined &&
101101
marginRight === undefined &&
102102
anchor === "right" &&
103-
x == null && {k, labelAnchor, label};
103+
x == null && {scale: k, labelAnchor, label};
104104
const autoMarginLeft = margin === undefined &&
105105
marginLeft === undefined &&
106106
anchor === "left" &&
107-
x == null && {k, labelAnchor, label};
107+
x == null && {scale: k, labelAnchor, label};
108108
marginRight = margin === undefined ? (anchor === "right" ? 40 : 0) : margin;
109109
marginLeft = margin === undefined ? (anchor === "left" ? 40 : 0) : margin;
110110
return marks(

src/plot.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {creator, select} from "d3";
22
import {createChannel, inferChannelScale} from "./channel.js";
33
import {createContext} from "./context.js";
4-
import {createDimensionsScales} from "./dimensions.js";
4+
import {createDimensions, autoMarginK, actualDimensions} from "./dimensions.js";
55
import {createFacets, recreateFacets, facetExclude, facetGroups, facetTranslator, facetFilter} from "./facet.js";
66
import {pointer, pointerX, pointerY} from "./interactions/pointer.js";
77
import {createLegends, exposeLegends} from "./legends.js";
@@ -11,8 +11,9 @@ import {frame} from "./marks/frame.js";
1111
import {tip} from "./marks/tip.js";
1212
import {isColor, isIterable, isNone, isScaleOptions} from "./options.js";
1313
import {arrayify, map, yes, maybeIntervalTransform, subarray} from "./options.js";
14-
import {getGeometryChannels, hasProjection} from "./projection.js";
15-
import {createScales, createScaleFunctions, exposeScales} from "./scales.js";
14+
import {createProjection, getGeometryChannels, hasProjection} from "./projection.js";
15+
import {createScales, createScaleFunctions, autoScaleRange, exposeScales} from "./scales.js";
16+
import {innerDimensions} from "./scales.js";
1617
import {isPosition, registry as scaleRegistry} from "./scales/index.js";
1718
import {applyInlineStyles, maybeClassName} from "./style.js";
1819
import {initializer} from "./transforms/basic.js";
@@ -158,16 +159,41 @@ export function plot(options = {}) {
158159
return {...state, channels: {...state.channels, ...facetState?.channels}};
159160
};
160161

161-
// Initialize the dimensions and scales.
162+
// Initialize the dimensions and scales. Needs a double take when the left or
163+
// right margins are based on the y (and fy) actual tick labels.
162164
const channels = addScaleChannels(channelsByScale, stateByMark, options);
163-
const {scaleDescriptors, scales, dimensions, subdimensions, superdimensions} = createDimensionsScales(
164-
channels,
165-
marks,
166-
stateByMark,
167-
options,
168-
context
169-
);
170-
const {fx, fy} = scales;
165+
let scaleDescriptors = createScales(channels, options);
166+
let {dimensions, autoMargins} = createDimensions(scaleDescriptors, marks, options);
167+
autoScaleRange(scaleDescriptors, dimensions); // !! mutates scales ranges…
168+
let scales = createScaleFunctions(scaleDescriptors);
169+
let {fx, fy} = scales;
170+
let subdimensions = fx || fy ? innerDimensions(scaleDescriptors, dimensions) : dimensions;
171+
let superdimensions = fx || fy ? actualDimensions(scaleDescriptors, dimensions) : dimensions;
172+
context.projection = createProjection(options, subdimensions);
173+
174+
// Review the auto margins and create new scales if more space is needed.
175+
const originalOptions = options;
176+
for (const [margin, scale, mark] of autoMargins) {
177+
options = autoMarginK(
178+
margin,
179+
scale,
180+
options,
181+
mark,
182+
stateByMark,
183+
scales,
184+
mark.facet === "super" ? superdimensions : subdimensions,
185+
context
186+
);
187+
}
188+
if (options !== originalOptions) {
189+
scaleDescriptors = createScales(channels, options);
190+
dimensions = createDimensions(scaleDescriptors, marks, options).dimensions;
191+
autoScaleRange(scaleDescriptors, dimensions);
192+
({fx, fy} = scales = createScaleFunctions(scaleDescriptors));
193+
subdimensions = fx || fy ? innerDimensions(scaleDescriptors, dimensions) : dimensions;
194+
superdimensions = fx || fy ? actualDimensions(scaleDescriptors, dimensions) : dimensions;
195+
context.projection = createProjection(options, subdimensions);
196+
}
171197

172198
// Allows e.g. the pointer transform to support viewof.
173199
context.dispatchValue = (value) => {

0 commit comments

Comments
 (0)