@@ -23,11 +23,8 @@ export function autoAxisTicks(axes, dimensions) {
2323// Mutates axes.{x,y}.label!
2424// Mutates axes.{x,y}.labelAnchor!
2525// Mutates axes.{x,y}.labelOffset!
26- export function autoAxisLabels ( encodings , scales , axes , dimensions ) {
26+ export function autoAxisLabels ( channels , scales , axes , dimensions ) {
2727 if ( axes . x ) {
28- if ( axes . x . label === undefined ) {
29- axes . x . label = inferLabel ( encodings . get ( "x" ) , scales . x , "x" ) ;
30- }
3128 if ( axes . x . labelAnchor === undefined ) {
3229 axes . x . labelAnchor = scales . x . type === "ordinal" ? "center"
3330 : scales . x . invert ? "left"
@@ -37,11 +34,11 @@ export function autoAxisLabels(encodings, scales, axes, dimensions) {
3734 const { marginTop, marginBottom} = dimensions ;
3835 axes . x . labelOffset = axes . x . anchor === "top" ? marginTop : marginBottom ;
3936 }
37+ if ( axes . x . label === undefined ) {
38+ axes . x . label = inferLabel ( channels . get ( "x" ) , scales . x , axes . x , "x" ) ;
39+ }
4040 }
4141 if ( axes . y ) {
42- if ( axes . y . label === undefined ) {
43- axes . y . label = inferLabel ( encodings . get ( "y" ) , scales . y , "y" ) ;
44- }
4542 if ( axes . y . labelAnchor === undefined ) {
4643 axes . y . labelAnchor = scales . y . type === "ordinal" ? "center"
4744 : axes . x && axes . x . anchor === "top" ? "bottom"
@@ -51,26 +48,29 @@ export function autoAxisLabels(encodings, scales, axes, dimensions) {
5148 const { marginRight, marginLeft} = dimensions ;
5249 axes . y . labelOffset = axes . y . anchor === "left" ? marginLeft : marginRight ;
5350 }
51+ if ( axes . y . label === undefined ) {
52+ axes . y . label = inferLabel ( channels . get ( "y" ) , scales . y , axes . y , "y" ) ;
53+ }
5454 }
5555}
5656
57- // Encodings can have labels; if all the encodings for a given scale are
57+ // Channels can have labels; if all the channels for a given scale are
5858// consistently labeled (i.e., have the same value if not undefined), and the
59- // corresponding axis doesn’t already have an explicit label, then the
60- // encodings’ label is promoted to the corresponding axis. TODO The arrows
61- // should be disabled if the label anchor is center: the arrows will point the
62- // wrong way with the rotated label.
63- function inferLabel ( encodings = [ ] , scale , key ) {
59+ // corresponding axis doesn’t already have an explicit label, then the channels’
60+ // label is promoted to the corresponding axis.
61+ function inferLabel ( channels = [ ] , scale , axis , key ) {
6462 let candidate ;
65- for ( const { label} of encodings ) {
63+ for ( const { label} of channels ) {
6664 if ( candidate === undefined ) candidate = label ;
6765 else if ( candidate !== label ) return ;
6866 }
6967 if ( candidate !== undefined ) {
7068 const { invert} = scale ;
71- const prefix = key === "y" ? ( invert ? "↓ " : "↑ " ) : key === "x" && invert ? "← " : "" ;
72- const suffix = key === "x" && ! invert ? " →" : "" ;
73- candidate = `${ prefix } ${ candidate } ${ suffix } ` ;
69+ if ( axis . labelAnchor !== "center" ) {
70+ const prefix = key === "y" ? ( invert ? "↓ " : "↑ " ) : key === "x" && invert ? "← " : "" ;
71+ const suffix = key === "x" && ! invert ? " →" : "" ;
72+ candidate = `${ prefix } ${ candidate } ${ suffix } ` ;
73+ }
7474 }
7575 return candidate ;
7676}
0 commit comments