Skip to content

Commit 55cb0eb

Browse files
committed
add extra axis padding when bars have outside text labels
1 parent 5cb0bcd commit 55cb0eb

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/traces/bar/cross_trace_calc.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ var Axes = require('../../plots/cartesian/axes');
99
var getAxisGroup = require('../../plots/cartesian/constraints').getAxisGroup;
1010
var Sieve = require('./sieve.js');
1111

12+
var TEXTPAD = require('./constants').TEXTPAD;
13+
1214
/*
1315
* Bar chart stacking/grouping positioning and autoscaling calculations
1416
* for each direction separately calculate the ranges and positions
@@ -565,13 +567,32 @@ function setBaseAndTop(sa, sieve) {
565567
}
566568
}
567569

570+
const textPadding = estimateExtraPaddingForText(fullTrace);
568571
fullTrace._extremes[sa._id] = Axes.findExtremes(sa, pts, {
569572
tozero: tozero,
570-
padded: true
573+
padded: true,
574+
ppad: textPadding
571575
});
572576
}
573577
}
574578

579+
// Very lightweight estimate of extra padding needed
580+
// to accommodate outside text labels on bars
581+
// Only considers vertical bars with 'outside' textposition for now
582+
// This solves the most common scenario of a simple vertical bar chart
583+
// with textposition set to 'outside'
584+
// More complex scenarios (horizontal bars, multi-line text labels)
585+
// are not (yet) handled here
586+
function estimateExtraPaddingForText(trace) {
587+
if (trace.orientation === 'v' && (trace.text || trace.texttemplate) && trace.textposition == 'outside') {
588+
// could count <br> elements here
589+
// but before that, need to make sure we are only
590+
// adding padding on the side(s) where it is needed
591+
return trace.outsidetextfont.size + TEXTPAD;
592+
}
593+
return 0;
594+
}
595+
575596
function stackBars(sa, sieve, opts) {
576597
var sLetter = getAxisLetter(sa);
577598
var calcTraces = sieve.traces;
@@ -637,13 +658,16 @@ function stackBars(sa, sieve, opts) {
637658
}
638659
}
639660

661+
const textPadding = estimateExtraPaddingForText(fullTrace);
662+
640663
// if barnorm is set, let normalizeBars update the axis range
641664
if (!opts.norm) {
642665
fullTrace._extremes[sa._id] = Axes.findExtremes(sa, pts, {
643666
// N.B. we don't stack base with 'base',
644667
// so set tozero:true always!
645668
tozero: true,
646-
padded: true
669+
padded: true,
670+
ppad: textPadding
647671
});
648672
}
649673
}

0 commit comments

Comments
 (0)