Skip to content

Commit 34b3e06

Browse files
committed
genericChartHelper: increase x-axis label position if wrapping tick text
1 parent d91f74f commit 34b3e06

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

visualization/resources/web/vis/genericChart/genericChartHelper.js

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,18 @@ LABKEY.vis.GenericChartHelper = new function(){
11951195
scales.x.tickLabelMax = Math.floor((plotConfig.width - 300) / 30);
11961196
}
11971197

1198-
var margins = _getPlotMargins(renderType, scales, aes, data, plotConfig, chartConfig);
1198+
var wrapLines = _wrapXAxisTickTextLines(plotConfig, chartConfig, aes, scales, data);
1199+
var margins = _getPlotMargins(renderType, data, chartConfig, wrapLines);
1200+
1201+
if (wrapLines > 1) {
1202+
labels = {
1203+
...labels,
1204+
// x-axis position defaults to 50 (see D3Renderer.renderLabel) but if we're wrapping our tick labels
1205+
// then they will probably collide with the label, so we add 20 to hopefully not collide.
1206+
x: { value: labels.x.value, position: 70 }
1207+
}
1208+
}
1209+
11991210
if (LABKEY.Utils.isObject(margins)) {
12001211
plotConfig.margins = margins;
12011212
}
@@ -1422,42 +1433,46 @@ LABKEY.vis.GenericChartHelper = new function(){
14221433
});
14231434
};
14241435

1425-
var _wrapXAxisTickTextLines = function(scales, plotConfig, maxTickLength, data) {
1426-
if (scales.x && scales.x.scaleType === 'discrete') {
1427-
var tickCount = scales.x && scales.x.tickLabelMax ? Math.min(scales.x.tickLabelMax, data.length) : data.length;
1436+
var _wrapXAxisTickTextLines = function(plotConfig, chartConfig, aes, scales, data) {
1437+
if (LABKEY.Utils.isArray(data) && scales.x && scales.x.scaleType === 'discrete') {
1438+
let maxTickLength = 0;
1439+
$.each(data, function(idx, d) {
1440+
const val = LABKEY.Utils.isFunction(aes.x) ? aes.x(d) : d[aes.x];
1441+
const subVal = LABKEY.Utils.isFunction(aes.xSub) ? aes.xSub(d) : d[aes.xSub];
1442+
if (LABKEY.Utils.isString(subVal)) {
1443+
maxTickLength = Math.max(maxTickLength, subVal.length);
1444+
} else if (LABKEY.Utils.isString(val)) {
1445+
maxTickLength = Math.max(maxTickLength, val.length);
1446+
}
1447+
});
1448+
1449+
let tickCount = scales.x && scales.x.tickLabelMax ? Math.min(scales.x.tickLabelMax, data.length) : data.length;
14281450
// after 10 tick labels, we switch to rotating the label, so use that as the max here
14291451
tickCount = Math.min(tickCount, 10);
1430-
var approxTickLabelWidth = plotConfig.width / tickCount;
1431-
return Math.max(1, Math.floor((maxTickLength * 8) / approxTickLabelWidth));
1452+
const approxTickLabelWidth = plotConfig.width / tickCount;
1453+
return Math.max(1, Math.floor((maxTickLength * 12) / approxTickLabelWidth));
14321454
}
14331455

14341456
return 1;
14351457
};
14361458

1437-
var _getPlotMargins = function(renderType, scales, aes, data, plotConfig, chartConfig) {
1438-
var margins = {};
1459+
const chartConfigHasLegend = (chartConfig) => {
1460+
const { color, series, shape, xSub } = chartConfig.measures;
1461+
return !!(color || series || shape || xSub);
1462+
}
1463+
1464+
const _getPlotMargins = function(renderType, data, chartConfig, wrapLines) {
1465+
const margins = {};
1466+
const hasLegend = chartConfigHasLegend(chartConfig);
14391467

14401468
// issue 29690: for bar and box plots, set default bottom margin based on the number of labels and the max label length
14411469
if (LABKEY.Utils.isArray(data)) {
1442-
var maxLen = 0;
1443-
$.each(data, function(idx, d) {
1444-
var val = LABKEY.Utils.isFunction(aes.x) ? aes.x(d) : d[aes.x];
1445-
var subVal = LABKEY.Utils.isFunction(aes.xSub) ? aes.xSub(d) : d[aes.xSub];
1446-
if (LABKEY.Utils.isString(subVal)) {
1447-
maxLen = Math.max(maxLen, subVal.length);
1448-
} else if (LABKEY.Utils.isString(val)) {
1449-
maxLen = Math.max(maxLen, val.length);
1450-
}
1451-
});
1452-
1453-
var wrapLines = _wrapXAxisTickTextLines(scales, plotConfig, maxLen, data);
1454-
1455-
if (chartConfig.legendPos === 'bottom') {
1470+
if (chartConfig.legendPos === 'bottom' && hasLegend) {
14561471
// min bottom margin: 170, max bottom margin: 360
14571472
margins.bottom = Math.min(360, 170 + ((wrapLines - 1) * 25));
14581473
} else {
1459-
// min bottom margin: 60, max bottom margin: 150
1460-
margins.bottom = Math.min(150, 60 + ((wrapLines - 1) * 25));
1474+
// min bottom margin: 80, max bottom margin: 150
1475+
margins.bottom = Math.min(150, 80 + ((wrapLines - 1) * 25));
14611476
}
14621477
}
14631478

0 commit comments

Comments
 (0)