1
1
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" ;
4
4
import { offset } from "./style.js" ;
5
5
import { defaultWidth , monospaceWidth } from "./marks/text.js" ;
6
- import { createScaleFunctions , autoScaleRange , innerDimensions , outerDimensions } from "./scales.js" ;
6
+ import { outerDimensions } from "./scales.js" ;
7
7
8
8
const marginMedium = 60 ;
9
9
const marginLarge = 90 ;
10
10
11
11
// When axes have "auto" margins, we might need to adjust the margins, after
12
12
// seeing the actual tick labels. In that case we’ll compute the dimensions and
13
13
// 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
+ ) {
15
24
let { data, facets, channels} = stateByMark . get ( mark ) ;
16
25
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 ) ) ;
19
30
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 ) ;
26
32
const m = l >= 500 ? marginLarge : l >= 295 ? marginMedium : null ;
27
33
return m === null
28
34
? options
@@ -31,46 +37,7 @@ function autoMarginK(margin, {k: scale, labelAnchor, label}, options, mark, stat
31
37
: { [ margin ] : m , ...options } ;
32
38
}
33
39
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 = { } ) {
74
41
// Compute the default margins: the maximum of the marks’ margins. While not
75
42
// always used, they may be needed to compute the default height of the plot.
76
43
let marginTopDefault = 0.5 - offset ,
0 commit comments