Repository navigation
fix(legend): respect horizontal padding for legend title - #12187
Conversation
kurkle
left a comment
There was a problem hiding this comment.
Thanks. The change is right for #12066. It is piece P3 of the legend rework designed in openspec/changes/rework-legend/ (#12482), and it lands after #12330, which reserves room for the title's padding in a vertical legend. I checked 'start', 'center' and 'end' in horizontal and vertical legends. Each lands where it should: 'start' at left + padding.left, 'end' at the right edge minus padding.right, and 'center' shifted by (left − right) / 2.
What is missing is a test. Here is one that fails on master in all six cases (for example "Expected 0 to be close to 50" for a vertical 'start') and passes with this change. Please add it to test/specs/plugin.legend.tests.js:
describe('title padding', function() {
function titleX(position, legendPosition) {
const chart = window.acquireChart({
type: 'bar',
data: {labels: [], datasets: [{label: 'a', data: []}, {label: 'b', data: []}]},
options: {plugins: {legend: {
position: legendPosition,
title: {display: true, text: 'TITLE', position, padding: {left: 50, right: 10}}
}}}
});
const spy = spyOn(chart.ctx, 'fillText').and.callThrough();
chart.legend.drawTitle();
const l = chart.legend;
const inner = l.isHorizontal() ? Math.max(...l.lineWidths) : l.width;
const left = l.isHorizontal() ? l.left + (l.width - inner) / 2 : l.left;
return {x: spy.calls.allArgs().find(a => a[0] === 'TITLE')[1], left, inner};
}
['top', 'left'].forEach(legendPosition => {
it(`offsets start by padding.left, legend ${legendPosition}`, function() {
const r = titleX('start', legendPosition);
expect(r.x).toBeCloseTo(r.left + 50, 3);
});
it(`offsets end by padding.right, legend ${legendPosition}`, function() {
const r = titleX('end', legendPosition);
expect(r.x).toBeCloseTo(r.left + r.inner - 10, 3);
});
it(`shifts center by (left - right) / 2, legend ${legendPosition}`, function() {
const r = titleX('center', legendPosition);
expect(r.x).toBeCloseTo(r.left + r.inner / 2 + (50 - 10) / 2, 3);
});
});
});Note: in an rtl legend, drawTitle passes x without rtlHelper.x(). That was already so on master, so this PR doesn't need to change it.
23ce9e1 to
a9834f3
Compare
✅ Deploy Preview for chartjs-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Size Change: +24 B (+0.01%) Total Size: 364 kB 📦 View Changed
ℹ️ View Unchanged
|
The legend title's horizontal paddings (left/right) were being ignored when calculating the x position. Only vertical padding was used. This fix accounts for titlePadding.left and titlePadding.right when calculating the title x position using _alignStartEnd. Fixes chartjs#12066
Draws the title of a top and a left legend with padding {left: 50,
right: 10} for each title position: 'start' lands at the left edge plus
padding.left, 'end' at the right edge minus padding.right, and 'center'
shifts by (left - right) / 2. Without the fix all six fail, for example
"Expected 0 to be close to 50" for 'start' in a left legend.
The specs use a top and a left legend; drawTitle places the title of a vertical legend from this.left and this.width on either side.
a9834f3 to
06510e0
Compare
|
The red |
kurkle
left a comment
There was a problem hiding this comment.
The requested test is in: start, center and end in a top and a left legend, each failing without the fix.
Summary
Fixes #12066
The legend title was not respecting the horizontal padding (
leftandright) specified intitlePadding. The title would be positioned without accounting for these padding values, causing visual misalignment.Changes
plugin.legend.jsto account fortitlePadding.leftandtitlePadding.rightwhen computing the title alignment positionBefore
After
A maintainer added the requested test (title padding for
'start','center'and'end'in a top and a left legend; all six fail without the fix) and rebased onto master. #11280 is not addressed here: it is answered by the legendpaddingoption, piece P16 of the legend rework inopenspec/changes/rework-legend/.