@@ -15,6 +15,7 @@ var getSubplotCalcData = require('../../plots/get_data').getSubplotCalcData;
15
15
var xmlnsNamespaces = require ( '../../constants/xmlns_namespaces' ) ;
16
16
var d3 = require ( 'd3' ) ;
17
17
var Drawing = require ( '../../components/drawing' ) ;
18
+ var svgTextUtils = require ( '../../lib/svg_text_utils' ) ;
18
19
19
20
var Mapbox = require ( './mapbox' ) ;
20
21
@@ -170,21 +171,41 @@ exports.toSVG = function(gd) {
170
171
'font-family' : 'Arial' ,
171
172
'color' : 'rgba(0, 0, 0, 0.75)' ,
172
173
'text-anchor' : 'end' ,
173
- 'data-unformatted' : attributions ,
174
- x : size . l + size . w * domain . x [ 1 ] - 3 ,
175
- y : size . t + size . h * ( 1 - domain . y [ 0 ] ) - 4
174
+ 'data-unformatted' : attributions
176
175
} ) ;
177
176
178
177
var bBox = Drawing . bBox ( attributionText . node ( ) ) ;
178
+
179
+ // Break into multiple lines twice larger than domain
180
+ var maxWidth = size . w * ( domain . x [ 1 ] - domain . x [ 0 ] ) ;
181
+ if ( ( bBox . width > maxWidth / 2 ) ) {
182
+ var multilineAttributions = attributions . split ( '|' ) . join ( '<br>' ) ;
183
+ attributionText
184
+ . text ( multilineAttributions )
185
+ . attr ( 'data-unformatted' , multilineAttributions )
186
+ . call ( svgTextUtils . convertToTspans , gd ) ;
187
+
188
+ bBox = Drawing . bBox ( attributionText . node ( ) ) ;
189
+ }
190
+ attributionText . attr ( 'transform' , 'translate(-3, ' + ( - bBox . height + 8 ) + ')' ) ;
191
+
192
+ // Draw white rectangle behind text
179
193
attributionGroup
180
194
. insert ( 'rect' , '.static-attribution' )
181
195
. attr ( {
182
- x : size . l + size . w * domain . x [ 1 ] - bBox . width - 6 ,
183
- y : size . t + size . h * ( 1 - domain . y [ 0 ] ) - ( bBox . height + 3 ) ,
196
+ x : - bBox . width - 6 ,
197
+ y : - bBox . height - 3 ,
184
198
width : bBox . width + 6 ,
185
199
height : bBox . height + 3 ,
186
200
fill : 'rgba(255, 255, 255, 0.75)'
187
201
} ) ;
202
+
203
+ // Scale down if larger than domain
204
+ var scaleRatio = 1 ;
205
+ if ( ( bBox . width + 6 ) > maxWidth ) scaleRatio = maxWidth / ( bBox . width + 6 ) ;
206
+
207
+ var offset = [ ( size . l + size . w * domain . x [ 1 ] ) , ( size . t + size . h * ( 1 - domain . y [ 0 ] ) ) ] ;
208
+ attributionGroup . attr ( 'transform' , 'translate(' + offset [ 0 ] + ',' + offset [ 1 ] + ') scale(' + scaleRatio + ')' ) ;
188
209
}
189
210
} ;
190
211
0 commit comments