@@ -15,6 +15,8 @@ var map1dArray = require('./map_1d_array');
15
15
var makepath = require ( './makepath' ) ;
16
16
var orientText = require ( './orient_text' ) ;
17
17
var svgTextUtils = require ( '../../lib/svg_text_utils' ) ;
18
+ var Lib = require ( '../../lib' ) ;
19
+ var alignmentConstants = require ( '../../constants/alignment' ) ;
18
20
19
21
module . exports = function plot ( gd , plotinfo , cdcarpet ) {
20
22
for ( var i = 0 ; i < cdcarpet . length ; i ++ ) {
@@ -58,10 +60,10 @@ function plotOne(gd, plotinfo, cd) {
58
60
drawGridLines ( xa , ya , boundaryLayer , aax , 'a-boundary' , aax . _boundarylines ) ;
59
61
drawGridLines ( xa , ya , boundaryLayer , bax , 'b-boundary' , bax . _boundarylines ) ;
60
62
61
- var maxAExtent = drawAxisLabels ( gd , xa , ya , trace , t , labelLayer , aax . _labels , 'a-label' ) ;
62
- var maxBExtent = drawAxisLabels ( gd , xa , ya , trace , t , labelLayer , bax . _labels , 'b-label' ) ;
63
+ var labelOrientationA = drawAxisLabels ( gd , xa , ya , trace , t , labelLayer , aax . _labels , 'a-label' ) ;
64
+ var labelOrientationB = drawAxisLabels ( gd , xa , ya , trace , t , labelLayer , bax . _labels , 'b-label' ) ;
63
65
64
- drawAxisTitles ( gd , labelLayer , trace , t , xa , ya , maxAExtent , maxBExtent ) ;
66
+ drawAxisTitles ( gd , labelLayer , trace , t , xa , ya , labelOrientationA , labelOrientationB ) ;
65
67
66
68
drawClipPath ( trace , t , clipLayer , xa , ya ) ;
67
69
}
@@ -131,8 +133,9 @@ function drawAxisLabels(gd, xaxis, yaxis, trace, t, layer, labels, labelClass) {
131
133
. classed ( labelClass , true ) ;
132
134
133
135
var maxExtent = 0 ;
136
+ var labelOrientation = { } ;
134
137
135
- labelJoin . each ( function ( label ) {
138
+ labelJoin . each ( function ( label , i ) {
136
139
// Most of the positioning is done in calc_labels. Only the parts that depend upon
137
140
// the screen space representation of the x and y axes are here:
138
141
var orientation ;
@@ -142,6 +145,11 @@ function drawAxisLabels(gd, xaxis, yaxis, trace, t, layer, labels, labelClass) {
142
145
var angle = ( label . axis . tickangle + 180.0 ) * Math . PI / 180.0 ;
143
146
orientation = orientText ( trace , xaxis , yaxis , label . xy , [ Math . cos ( angle ) , Math . sin ( angle ) ] ) ;
144
147
}
148
+
149
+ if ( ! i ) {
150
+ // TODO: offsetMultiplier? Not currently used anywhere...
151
+ labelOrientation = { angle : orientation . angle , flip : orientation . flip } ;
152
+ }
145
153
var direction = ( label . endAnchor ? - 1 : 1 ) * orientation . flip ;
146
154
147
155
var labelEl = d3 . select ( this )
@@ -169,29 +177,40 @@ function drawAxisLabels(gd, xaxis, yaxis, trace, t, layer, labels, labelClass) {
169
177
170
178
labelJoin . exit ( ) . remove ( ) ;
171
179
172
- return maxExtent ;
180
+ labelOrientation . maxExtent = maxExtent ;
181
+ return labelOrientation ;
173
182
}
174
183
175
- function drawAxisTitles ( gd , layer , trace , t , xa , ya , maxAExtent , maxBExtent ) {
184
+ function drawAxisTitles ( gd , layer , trace , t , xa , ya , labelOrientationA , labelOrientationB ) {
176
185
var a , b , xy , dxy ;
177
186
178
187
a = 0.5 * ( trace . a [ 0 ] + trace . a [ trace . a . length - 1 ] ) ;
179
188
b = trace . b [ 0 ] ;
180
189
xy = trace . ab2xy ( a , b , true ) ;
181
190
dxy = trace . dxyda_rough ( a , b ) ;
182
- drawAxisTitle ( gd , layer , trace , t , xy , dxy , trace . aaxis , xa , ya , maxAExtent , 'a-title' ) ;
191
+ if ( labelOrientationA . angle === undefined ) {
192
+ Lib . extendFlat ( labelOrientationA , orientText ( trace , xa , ya , xy , trace . dxydb_rough ( a , b ) ) ) ;
193
+ }
194
+ drawAxisTitle ( gd , layer , trace , t , xy , dxy , trace . aaxis , xa , ya , labelOrientationA , 'a-title' ) ;
183
195
184
196
a = trace . a [ 0 ] ;
185
197
b = 0.5 * ( trace . b [ 0 ] + trace . b [ trace . b . length - 1 ] ) ;
186
198
xy = trace . ab2xy ( a , b , true ) ;
187
199
dxy = trace . dxydb_rough ( a , b ) ;
188
- drawAxisTitle ( gd , layer , trace , t , xy , dxy , trace . baxis , xa , ya , maxBExtent , 'b-title' ) ;
200
+ if ( labelOrientationB . angle === undefined ) {
201
+ Lib . extendFlat ( labelOrientationB , orientText ( trace , xa , ya , xy , trace . dxyda_rough ( a , b ) ) ) ;
202
+ }
203
+ drawAxisTitle ( gd , layer , trace , t , xy , dxy , trace . baxis , xa , ya , labelOrientationB , 'b-title' ) ;
189
204
}
190
205
191
- function drawAxisTitle ( gd , layer , trace , t , xy , dxy , axis , xa , ya , offset , labelClass ) {
206
+ var lineSpacing = alignmentConstants . LINE_SPACING ;
207
+ var midShift = ( ( 1 - alignmentConstants . MID_SHIFT ) / lineSpacing ) + 1 ;
208
+
209
+ function drawAxisTitle ( gd , layer , trace , t , xy , dxy , axis , xa , ya , labelOrientation , labelClass ) {
192
210
var data = [ ] ;
193
211
if ( axis . title ) data . push ( axis . title ) ;
194
212
var titleJoin = layer . selectAll ( 'text.' + labelClass ) . data ( data ) ;
213
+ var offset = labelOrientation . maxExtent ;
195
214
196
215
titleJoin . enter ( ) . append ( 'text' )
197
216
. classed ( labelClass , true ) ;
@@ -205,14 +224,23 @@ function drawAxisTitle(gd, layer, trace, t, xy, dxy, axis, xa, ya, offset, label
205
224
}
206
225
207
226
// In addition to the size of the labels, add on some extra padding:
208
- offset += axis . titlefont . size + axis . titleoffset ;
227
+ var titleSize = axis . titlefont . size ;
228
+ offset += titleSize + axis . titleoffset ;
209
229
230
+ var labelNorm = labelOrientation . angle + ( labelOrientation . flip < 0 ? 180 : 0 ) ;
231
+ var angleDiff = ( labelNorm - orientation . angle + 450 ) % 360 ;
232
+ var reverseTitle = angleDiff > 90 && angleDiff < 270 ;
210
233
211
234
var el = d3 . select ( this ) ;
212
235
213
236
el . text ( axis . title || '' )
214
- . call ( svgTextUtils . convertToTspans , gd )
215
- . attr ( 'transform' ,
237
+ . call ( svgTextUtils . convertToTspans , gd ) ;
238
+
239
+ if ( reverseTitle ) {
240
+ offset = ( - svgTextUtils . lineCount ( el ) + midShift ) * lineSpacing * titleSize - offset ;
241
+ }
242
+
243
+ el . attr ( 'transform' ,
216
244
'translate(' + orientation . p [ 0 ] + ',' + orientation . p [ 1 ] + ') ' +
217
245
'rotate(' + orientation . angle + ') ' +
218
246
'translate(0,' + offset + ')'
0 commit comments