Skip to content

fix: take the scatter and bubble tooltip label from the category axis - #12446

Open
kurkle wants to merge 2 commits into
chartjs:masterfrom
kurkle:isolate-category-scale-labels-p2-point-tooltip-labels
Open

kurkle wants to merge 2 commits into
chartjs:masterfrom
kurkle:isolate-category-scale-labels-p2-point-tooltip-labels

Conversation

@kurkle

@kurkle kurkle commented Nov 24, 2026

Copy link
Copy Markdown
Member

Part of #11495 and #11893. This is piece P2 of the design in openspec/changes/isolate-category-scale-labels/ (Decision 7).

What was wrong

When data.labels has no entry for a point, the scatter and bubble controllers read the tooltip label from chart.data.labels[index]. That works only because the category scale writes the labels it infers from the data into the shared data.labels. Once each category scale keeps its inferred labels to itself (a later piece of the same design), that read gets nothing and the tooltip title goes blank. Reading by index was also wrong before that: it gives the wrong category when categories repeat, and with two category axes it reads from both axes' labels mixed into one array.

What changed

In getLabelAndValue() of controller.scatter.js and controller.bubble.js, the label is:

  1. the data.labels entry at the point's index, when there is one (unchanged);
  2. otherwise, when the index axis is a category axis, that axis' label for the point's own parsed value;
  3. otherwise, when the value axis is a category axis, that axis' label for the point's parsed value;
  4. otherwise '', which also covers a point that has no category.

Each controller gets the same small categoryLabel() helper. A category axis is recognised by scale.type === 'category', so a custom scale that extends CategoryScale under another id is not recognised.

Tests

New specs, under tooltip label in test/specs/controller.scatter.tests.js and test/specs/controller.bubble.tests.js:

  • (a) category x axis, labels inferred from the data: point 1 is 'b'
  • (b) linear x axis with a category y axis: point 1 is 'b'
  • (c) both axes category: the x axis' label 'b'
  • (d) category x with points at 'a', 'b', 'a': point 2 is 'a'
  • (e) data.labels has an entry: that entry
  • (f) no data.labels and no category axis: ''
  • (g) a point without a category: ''

Results, Karma in Chrome:

scatter bubble
master, without this fix (c), (d) fail: Expected '' to be 'b', Expected '' to be 'a' same
master with this fix 16 of 16 pass 26 of 26 pass
prototype of the per-scale label copy, without this fix (a), (b), (c), (d) fail: Expected '' to be 'b' / 'a' same
prototype of the per-scale label copy, with this fix 16 of 16 pass 26 of 26 pass
  • (a) and (b) pass on master, where the label still comes from the polluted data.labels. They protect the label once the scale stops writing there; the third row shows that.
  • (c) and (d) change master's result on purpose (Decision 7). The label now names the point's own category, from the index axis first. Before, it was whatever data.labels held at the point's index.
  • The full Karma suite passes, except 7 image fixtures (plugin.filler, scale.radialLinear, scale.time, scale.timeseries). Those 7 fail the same way on master in my local Chrome 158, so they are not caused by this change.
  • ESLint reports no errors. pnpm lint-types passes.

How to try it

new Chart(ctx, {
  type: 'scatter',
  data: {datasets: [{data: [{x: 'a', y: 1}, {x: 'b', y: 2}, {x: 'a', y: 3}]}]},
  options: {scales: {x: {type: 'category'}}}
});

Hover the third point. The tooltip title is a; on master it is empty.

Specs for the label that getLabelAndValue() gives a point in scatter
and bubble charts: from a category x axis, from a category y axis
beside a linear x axis, from the x axis when both are category axes,
for a point whose category repeats, from data.labels when it has an
entry, and empty without either or for a point without a category.
When data.labels has no entry for a point, getLabelAndValue() of the
scatter and bubble controllers now gives the label of the point's own
category: from the index axis when it is a category axis, else from
the value axis when that one is, else ''.

Before, the label was read from data.labels by index only. That worked
only because the category scale adds the labels it infers from the
data to data.labels, and it named the wrong category when categories
repeat or both axes are category axes.
@netlify

netlify Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for chartjs-docs ready!

Name Link
🔨 Latest commit 4cc3359
🔍 Latest deploy log https://app.netlify.com/projects/chartjs-docs/deploys/6b05053535421ae67c48e98c
😎 Deploy Preview https://deploy-preview-12446--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

Copy link
Copy Markdown

Size Change: +308 B (+0.09%)

Total Size: 362 kB

📦 View Changed
Filename Size Change
dist/chart.cjs 81.5 kB +86 B (+0.11%)
dist/chart.js 81.8 kB +85 B (+0.1%)
dist/chart.umd.js 72.1 kB +68 B (+0.09%)
dist/chart.umd.min.js 72.1 kB +69 B (+0.1%)
ℹ️ View Unchanged
Filename Size
dist/chunks/helpers.dataset.cjs 25.7 kB
dist/chunks/helpers.dataset.js 25.5 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 24, 2026 03:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants