@@ -15,6 +15,7 @@ var getSubplotCalcData = require('../../plots/get_data').getSubplotCalcData;
1515var xmlnsNamespaces = require ( '../../constants/xmlns_namespaces' ) ;
1616var d3 = require ( 'd3' ) ;
1717var Drawing = require ( '../../components/drawing' ) ;
18+ var svgTextUtils = require ( '../../lib/svg_text_utils' ) ;
1819
1920var Mapbox = require ( './mapbox' ) ;
2021
@@ -170,21 +171,41 @@ exports.toSVG = function(gd) {
170171 'font-family' : 'Arial' ,
171172 'color' : 'rgba(0, 0, 0, 0.75)' ,
172173 '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
176175 } ) ;
177176
178177 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
179193 attributionGroup
180194 . insert ( 'rect' , '.static-attribution' )
181195 . 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 ,
184198 width : bBox . width + 6 ,
185199 height : bBox . height + 3 ,
186200 fill : 'rgba(255, 255, 255, 0.75)'
187201 } ) ;
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 + ')' ) ;
188209 }
189210} ;
190211
0 commit comments