Skip to content

Commit a54b8a3

Browse files
committed
mapbox: break into multiple lines and scale down attributions on export
1 parent dad62fc commit a54b8a3

18 files changed

+26
-5
lines changed

src/plots/mapbox/index.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var getSubplotCalcData = require('../../plots/get_data').getSubplotCalcData;
1515
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
1616
var d3 = require('d3');
1717
var Drawing = require('../../components/drawing');
18+
var svgTextUtils = require('../../lib/svg_text_utils');
1819

1920
var 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

test/image/baselines/mapbox_0.png

-27 Bytes
Loading
-18 Bytes
Loading
-22 Bytes
Loading
2 Bytes
Loading
-36 Bytes
Loading
Loading
-6 Bytes
Loading
185 Bytes
Loading
39 Bytes
Loading
-128 Bytes
Loading
-3 Bytes
Loading
Loading

test/image/baselines/mapbox_fill.png

-1 Bytes
Loading
-21 Bytes
Loading
-92 Bytes
Loading
-1 Bytes
Loading
7 Bytes
Loading

0 commit comments

Comments
 (0)