@@ -13,6 +13,9 @@ var mapboxgl = require('mapbox-gl');
1313var Lib = require ( '../../lib' ) ;
1414var getSubplotCalcData = require ( '../../plots/get_data' ) . getSubplotCalcData ;
1515var xmlnsNamespaces = require ( '../../constants/xmlns_namespaces' ) ;
16+ var d3 = require ( 'd3' ) ;
17+ var Drawing = require ( '../../components/drawing' ) ;
18+ var svgTextUtils = require ( '../../lib/svg_text_utils' ) ;
1619
1720var Mapbox = require ( './mapbox' ) ;
1821
@@ -118,7 +121,91 @@ exports.toSVG = function(gd) {
118121 preserveAspectRatio : 'none'
119122 } ) ;
120123
121- mapbox . destroy ( ) ;
124+ var subplotDiv = d3 . select ( opts . _subplot . div ) ;
125+
126+ // Append logo if visible
127+ var hidden = subplotDiv . select ( '.mapboxgl-ctrl-logo' ) . node ( ) . offsetParent === null ;
128+ if ( ! hidden ) {
129+ var logo = fullLayout . _glimages . append ( 'g' ) ;
130+ logo . attr ( 'transform' , 'translate(' + ( size . l + size . w * domain . x [ 0 ] + 10 ) + ', ' + ( size . t + size . h * ( 1 - domain . y [ 0 ] ) - 31 ) + ')' ) ;
131+ logo . append ( 'path' )
132+ . attr ( 'd' , constants . mapboxLogo . path0 )
133+ . style ( {
134+ opacity : 0.9 ,
135+ fill : '#ffffff' ,
136+ 'enable-background' : 'new'
137+ } ) ;
138+
139+ logo . append ( 'path' )
140+ . attr ( 'd' , constants . mapboxLogo . path1 )
141+ . style ( 'opacity' , 0.35 )
142+ . style ( 'enable-background' , 'new' ) ;
143+
144+ logo . append ( 'path' )
145+ . attr ( 'd' , constants . mapboxLogo . path2 )
146+ . style ( 'opacity' , 0.35 )
147+ . style ( 'enable-background' , 'new' ) ;
148+
149+ logo . append ( 'polygon' )
150+ . attr ( 'points' , constants . mapboxLogo . polygon )
151+ . style ( {
152+ opacity : 0.9 ,
153+ fill : '#ffffff' ,
154+ 'enable-background' : 'new'
155+ } ) ;
156+ }
157+
158+ // Add attributions
159+ var attributions = subplotDiv
160+ . select ( '.mapboxgl-ctrl-attrib' ) . text ( )
161+ . replace ( 'Improve this map' , '' ) ;
162+
163+ var attributionGroup = fullLayout . _glimages . append ( 'g' ) ;
164+
165+ var attributionText = attributionGroup . append ( 'text' ) ;
166+ attributionText
167+ . text ( attributions )
168+ . classed ( 'static-attribution' , true )
169+ . attr ( {
170+ 'font-size' : 12 ,
171+ 'font-family' : 'Arial' ,
172+ 'color' : 'rgba(0, 0, 0, 0.75)' ,
173+ 'text-anchor' : 'end' ,
174+ 'data-unformatted' : attributions
175+ } ) ;
176+
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
193+ attributionGroup
194+ . insert ( 'rect' , '.static-attribution' )
195+ . attr ( {
196+ x : - bBox . width - 6 ,
197+ y : - bBox . height - 3 ,
198+ width : bBox . width + 6 ,
199+ height : bBox . height + 3 ,
200+ fill : 'rgba(255, 255, 255, 0.75)'
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 + ')' ) ;
122209 }
123210} ;
124211
0 commit comments