@@ -1802,14 +1802,11 @@ axes.drawOne = function(gd, ax, opts) {
1802
1802
// TODO: mirror labels, esp for subplots
1803
1803
1804
1804
seq . push ( function ( ) {
1805
- var labelFns = axes . makeLabelFns ( ax , mainLinePosition ) ;
1806
1805
return axes . drawLabels ( gd , ax , {
1807
1806
vals : vals ,
1808
1807
layer : mainAxLayer ,
1809
1808
transFn : transFn ,
1810
- labelXFn : labelFns . labelXFn ,
1811
- labelYFn : labelFns . labelYFn ,
1812
- labelAnchorFn : labelFns . labelAnchorFn ,
1809
+ labelFns : axes . makeLabelFns ( ax , mainLinePosition )
1813
1810
} ) ;
1814
1811
} ) ;
1815
1812
@@ -1821,8 +1818,6 @@ axes.drawOne = function(gd, ax, opts) {
1821
1818
seq . push ( function ( ) {
1822
1819
labelLength += getLabelLevelSpan ( ax , axId + 'tick' ) + pad ;
1823
1820
labelLength += ax . _tickAngles [ axId + 'tick' ] ? ax . tickfont . size * LINE_SPACING : 0 ;
1824
- var secondaryPosition = mainLinePosition + labelLength * sgn ;
1825
- var secondaryLabelFns = axes . makeLabelFns ( ax , secondaryPosition ) ;
1826
1821
1827
1822
return axes . drawLabels ( gd , ax , {
1828
1823
vals : getSecondaryLabelVals ( ax , vals ) ,
@@ -1831,9 +1826,7 @@ axes.drawOne = function(gd, ax, opts) {
1831
1826
repositionOnUpdate : true ,
1832
1827
secondary : true ,
1833
1828
transFn : transFn ,
1834
- labelXFn : secondaryLabelFns . labelXFn ,
1835
- labelYFn : secondaryLabelFns . labelYFn ,
1836
- labelAnchorFn : secondaryLabelFns . labelAnchorFn ,
1829
+ labelFns : axes . makeLabelFns ( ax , mainLinePosition + labelLength * sgn )
1837
1830
} ) ;
1838
1831
} ) ;
1839
1832
@@ -2178,66 +2171,79 @@ axes.makeTickPath = function(ax, shift, sgn, len) {
2178
2171
* @param {number } shift
2179
2172
* @param {number } angle [in degrees] ...
2180
2173
* @return {object }
2181
- * - {fn} labelXFn
2182
- * - {fn} labelYFn
2183
- * - {fn} labelAnchorFn
2184
- * - {number} labelStandoff
2185
- * - {number} labelShift
2174
+ * - {fn} xFn
2175
+ * - {fn} yFn
2176
+ * - {fn} anchorFn
2177
+ * - {fn} heightFn
2178
+ * - {number} labelStandoff (gap parallel to ticks)
2179
+ * - {number} labelShift (gap perpendicular to ticks)
2186
2180
*/
2187
2181
axes . makeLabelFns = function ( ax , shift , angle ) {
2188
2182
var axLetter = ax . _id . charAt ( 0 ) ;
2189
- var pad = ( ax . linewidth || 1 ) / 2 ;
2190
2183
var ticksOnOutsideLabels = ax . tickson !== 'boundaries' && ax . ticks === 'outside' ;
2191
2184
2192
- var labelStandoff = ticksOnOutsideLabels ? ax . ticklen : 0 ;
2185
+ var labelStandoff = 0 ;
2193
2186
var labelShift = 0 ;
2194
2187
2188
+ if ( ticksOnOutsideLabels ) {
2189
+ labelStandoff += ax . ticklen ;
2190
+ }
2195
2191
if ( angle && ax . ticks === 'outside' ) {
2196
2192
var rad = Lib . deg2rad ( angle ) ;
2197
2193
labelStandoff = ax . ticklen * Math . cos ( rad ) + 1 ;
2198
2194
labelShift = ax . ticklen * Math . sin ( rad ) ;
2199
2195
}
2200
-
2201
2196
if ( ax . showticklabels && ( ticksOnOutsideLabels || ax . showline ) ) {
2202
2197
labelStandoff += 0.2 * ax . tickfont . size ;
2203
2198
}
2199
+ labelStandoff += ( ax . linewidth || 1 ) / 2 ;
2204
2200
2205
- // Used in polar angular label x/y functions
2206
- // TODO generalize makeLabelFns so that it just work for angular axes
2207
2201
var out = {
2208
2202
labelStandoff : labelStandoff ,
2209
2203
labelShift : labelShift
2210
2204
} ;
2211
2205
2212
2206
var x0 , y0 , ff , flipIt ;
2207
+
2213
2208
if ( axLetter === 'x' ) {
2214
2209
flipIt = ax . side === 'bottom' ? 1 : - 1 ;
2215
2210
x0 = labelShift * flipIt ;
2216
- y0 = shift + ( labelStandoff + pad ) * flipIt ;
2211
+ y0 = shift + labelStandoff * flipIt ;
2217
2212
ff = ax . side === 'bottom' ? 1 : - 0.2 ;
2218
2213
2219
- out . labelXFn = function ( d ) { return d . dx + x0 ; } ;
2220
- out . labelYFn = function ( d ) { return d . dy + y0 + d . fontSize * ff ; } ;
2221
- out . labelAnchorFn = function ( a ) {
2214
+ out . xFn = function ( d ) { return d . dx + x0 ; } ;
2215
+ out . yFn = function ( d ) { return d . dy + y0 + d . fontSize * ff ; } ;
2216
+ out . anchorFn = function ( d , a ) {
2222
2217
if ( ! isNumeric ( a ) || a === 0 || a === 180 ) {
2223
2218
return 'middle' ;
2224
2219
}
2225
2220
return ( a * flipIt < 0 ) ? 'end' : 'start' ;
2226
2221
} ;
2222
+ out . heightFn = function ( d , a , h ) {
2223
+ return ( a < - 60 || a > 60 ) ? - 0.5 * h :
2224
+ ax . side === 'top' ? - h :
2225
+ 0 ;
2226
+ } ;
2227
2227
} else if ( axLetter === 'y' ) {
2228
2228
flipIt = ax . side === 'right' ? 1 : - 1 ;
2229
- x0 = labelStandoff + pad ;
2229
+ x0 = labelStandoff ;
2230
2230
y0 = - labelShift * flipIt ;
2231
2231
ff = Math . abs ( ax . tickangle ) === 90 ? 0.5 : 0 ;
2232
2232
2233
- out . labelXFn = function ( d ) { return d . dx + shift + ( x0 + d . fontSize * ff ) * flipIt ; } ;
2234
- out . labelYFn = function ( d ) { return d . dy + y0 + d . fontSize * MID_SHIFT ; } ;
2235
- out . labelAnchorFn = function ( a ) {
2233
+ out . xFn = function ( d ) { return d . dx + shift + ( x0 + d . fontSize * ff ) * flipIt ; } ;
2234
+ out . yFn = function ( d ) { return d . dy + y0 + d . fontSize * MID_SHIFT ; } ;
2235
+ out . anchorFn = function ( d , a ) {
2236
2236
if ( isNumeric ( a ) && Math . abs ( a ) === 90 ) {
2237
2237
return 'middle' ;
2238
2238
}
2239
2239
return ax . side === 'right' ? 'start' : 'end' ;
2240
2240
} ;
2241
+ out . heightFn = function ( d , a , h ) {
2242
+ a *= ax . side === 'left' ? 1 : - 1 ;
2243
+ return a < - 30 ? - h :
2244
+ a < 30 ? - 0.5 * h :
2245
+ 0 ;
2246
+ } ;
2241
2247
}
2242
2248
2243
2249
return out ;
@@ -2412,9 +2418,11 @@ axes.drawZeroLine = function(gd, ax, opts) {
2412
2418
* - {boolean} repositionOnUpdate (set to true to reposition update selection)
2413
2419
* - {boolean} secondary
2414
2420
* - {fn} transFn
2415
- * - {fn} labelXFn
2416
- * - {fn} labelYFn
2417
- * - {fn} labelAnchorFn
2421
+ * - {object} labelFns
2422
+ * + {fn} xFn
2423
+ * + {fn} yFn
2424
+ * + {fn} anchorFn
2425
+ * + {fn} heightFn
2418
2426
*/
2419
2427
axes . drawLabels = function ( gd , ax , opts ) {
2420
2428
opts = opts || { } ;
@@ -2423,9 +2431,7 @@ axes.drawLabels = function(gd, ax, opts) {
2423
2431
var axLetter = axId . charAt ( 0 ) ;
2424
2432
var cls = opts . cls || axId + 'tick' ;
2425
2433
var vals = opts . vals ;
2426
- var labelXFn = opts . labelXFn ;
2427
- var labelYFn = opts . labelYFn ;
2428
- var labelAnchorFn = opts . labelAnchorFn ;
2434
+ var labelFns = opts . labelFns ;
2429
2435
var tickAngle = opts . secondary ? 0 : ax . tickangle ;
2430
2436
var lastAngle = ( ax . _tickAngles || { } ) [ cls ] ;
2431
2437
@@ -2445,7 +2451,7 @@ axes.drawLabels = function(gd, ax, opts) {
2445
2451
var newPromise = gd . _promises . length ;
2446
2452
2447
2453
thisLabel
2448
- . call ( svgTextUtils . positionText , labelXFn ( d ) , labelYFn ( d ) )
2454
+ . call ( svgTextUtils . positionText , labelFns . xFn ( d ) , labelFns . yFn ( d ) )
2449
2455
. call ( Drawing . font , d . font , d . fontSize , d . fontColor )
2450
2456
. text ( d . text )
2451
2457
. call ( svgTextUtils . convertToTspans , gd ) ;
@@ -2469,47 +2475,26 @@ axes.drawLabels = function(gd, ax, opts) {
2469
2475
if ( opts . repositionOnUpdate ) {
2470
2476
tickLabels . each ( function ( d ) {
2471
2477
d3 . select ( this ) . select ( 'text' )
2472
- . call ( svgTextUtils . positionText , labelXFn ( d ) , labelYFn ( d ) ) ;
2478
+ . call ( svgTextUtils . positionText , labelFns . xFn ( d ) , labelFns . yFn ( d ) ) ;
2473
2479
} ) ;
2474
2480
}
2475
2481
2476
- // How much to shift a multi-line label to center it vertically.
2477
- function getAnchorHeight ( lineCount , lineHeight , angle ) {
2478
- var h = ( lineCount - 1 ) * lineHeight ;
2479
- if ( axLetter === 'x' ) {
2480
- if ( angle < - 60 || 60 < angle ) {
2481
- return - 0.5 * h ;
2482
- } else if ( ax . side === 'top' ) {
2483
- return - h ;
2484
- }
2485
- } else {
2486
- angle *= ax . side === 'left' ? 1 : - 1 ;
2487
- if ( angle < - 30 ) {
2488
- return - h ;
2489
- } else if ( angle < 30 ) {
2490
- return - 0.5 * h ;
2491
- }
2492
- }
2493
- return 0 ;
2494
- }
2495
-
2496
2482
function positionLabels ( s , angle ) {
2497
2483
s . each ( function ( d ) {
2498
2484
var thisLabel = d3 . select ( this ) ;
2499
2485
var mathjaxGroup = thisLabel . select ( '.text-math-group' ) ;
2500
- var anchor = labelAnchorFn ( angle , d ) ;
2486
+ var anchor = labelFns . anchorFn ( d , angle ) ;
2501
2487
2502
2488
var transform = opts . transFn . call ( thisLabel . node ( ) , d ) +
2503
2489
( ( isNumeric ( angle ) && + angle !== 0 ) ?
2504
- ( ' rotate(' + angle + ',' + labelXFn ( d ) + ',' +
2505
- ( labelYFn ( d ) - d . fontSize / 2 ) + ')' ) :
2490
+ ( ' rotate(' + angle + ',' + labelFns . xFn ( d ) + ',' +
2491
+ ( labelFns . yFn ( d ) - d . fontSize / 2 ) + ')' ) :
2506
2492
'' ) ;
2507
2493
2508
- var anchorHeight = getAnchorHeight (
2509
- svgTextUtils . lineCount ( thisLabel ) ,
2510
- LINE_SPACING * d . fontSize ,
2511
- isNumeric ( angle ) ? + angle : 0
2512
- ) ;
2494
+ // how much to shift a multi-line label to center it vertically.
2495
+ var nLines = svgTextUtils . lineCount ( thisLabel ) ;
2496
+ var lineHeight = LINE_SPACING * d . fontSize ;
2497
+ var anchorHeight = labelFns . heightFn ( d , isNumeric ( angle ) ? + angle : 0 , ( nLines - 1 ) * lineHeight ) ;
2513
2498
2514
2499
if ( anchorHeight ) {
2515
2500
transform += ' translate(0, ' + anchorHeight + ')' ;
0 commit comments