Skip to content

fix(legend): respect horizontal padding for legend title - #12187

Merged
kurkle merged 3 commits into
chartjs:masterfrom
jonathan-fulton:fix/12066-legend-title-padding
Nov 26, 2026
Merged

kurkle merged 3 commits into
chartjs:masterfrom
jonathan-fulton:fix/12066-legend-title-padding

Conversation

@jonathan-fulton

@jonathan-fulton jonathan-fulton commented Feb 1, 2026 •

Copy link
Copy Markdown

Summary

Fixes #12066

The legend title was not respecting the horizontal padding (left and right) specified in titlePadding. The title would be positioned without accounting for these padding values, causing visual misalignment.

Changes

  • Updated the x-coordinate calculation in plugin.legend.js to account for titlePadding.left and titlePadding.right when computing the title alignment position

Before

const x = _alignStartEnd(position, left, left + maxWidth);

After

const x = _alignStartEnd(position, left + titlePadding.left, left + maxWidth - titlePadding.right);

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 legend padding option, piece P16 of the legend rework in openspec/changes/rework-legend/.

@kurkle kurkle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@kurkle
kurkle marked this pull request as draft November 26, 2026 05:48
@kurkle
kurkle force-pushed the fix/12066-legend-title-padding branch from 23ce9e1 to a9834f3 Compare November 26, 2026 05:48
@netlify

netlify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for chartjs-docs ready!

Name Link
🔨 Latest commit 06510e0
🔍 Latest deploy log https://app.netlify.com/projects/chartjs-docs/deploys/6b082f2a3cf47a965f973927
😎 Deploy Preview https://deploy-preview-12187--chartjs-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Size Change: +24 B (+0.01%)

Total Size: 364 kB

📦 View Changed
Filename Size Change
dist/chart.cjs 82 kB +9 B (+0.01%)
dist/chart.js 82.3 kB +9 B (+0.01%)
dist/chart.umd.js 72.5 kB +3 B (0%)
dist/chart.umd.min.js 72.5 kB +3 B (0%)
ℹ️ View Unchanged
Filename Size
dist/chunks/helpers.dataset.cjs 25.8 kB
dist/chunks/helpers.dataset.js 25.6 kB
dist/helpers.cjs 1.52 kB
dist/helpers.js 1.33 kB

compressed-size-action

@kurkle
kurkle marked this pull request as ready for review November 26, 2026 07:40
etimberg
etimberg previously approved these changes Nov 26, 2026
@kurkle
kurkle marked this pull request as draft November 26, 2026 13:06
jonathan-fulton and others added 3 commits November 26, 2026 15:06
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.
@kurkle
kurkle force-pushed the fix/12066-legend-title-padding branch from a9834f3 to 06510e0 Compare November 26, 2026 13:07
@kurkle
kurkle marked this pull request as ready for review November 26, 2026 13:16
@kurkle

kurkle commented Nov 26, 2026

Copy link
Copy Markdown
Member

The red Coveralls - ubuntu-latest-chrome and Coveralls - ubuntu-latest-firefox checks are not from this PR. They measure only the specs that still run under Karma, and master fails them too (5eb416f: "Coverage decreased (-6.5%) to 79.929%") since the specs moved to Vitest. The Vitest coverage, the combined coverage and both required builds pass.

@kurkle
kurkle requested a review from etimberg November 26, 2026 13:32

@kurkle kurkle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requested test is in: start, center and end in a top and a left legend, each failing without the fix.

@kurkle
kurkle merged commit 02882d0 into chartjs:master Nov 26, 2026
10 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Legend title horizontal paddings doesn't work

3 participants