chartjs / chartjs-plugin-datalabels

Chart.js plugin to display labels on data elements
https://chartjs-plugin-datalabels.netlify.app
MIT License
863 stars 460 forks source link

Manage label even if the data element is skipped #322

Closed stockiNail closed 2 years ago

stockiNail commented 2 years ago

Fix #319

simonbrunel commented 2 years ago

Thanks @stockiNail to have a look!

From your comment on #319:

I think could make sense to manage labels even if the related element is skipped.

It does not make sense to me to create a label that will never be displayed. If I remember correctly, there is a few places in the code that assume there is no label for missing or invisible data. There are unit tests (you removed one) and I would prefer to not change this logic.

Instead, I think the issue comes from the initial values of the layout properties:

https://github.com/chartjs/chartjs-plugin-datalabels/blob/cdc2e94837a10d19eb1fc86996a271a298821b13/src/layout.js#L104-L110

_idx: j assumes that datasets contains all data since we use j.

So I would try to replace _idx: j by _idx: label._index to see if that fixes the issue reported in #319.

stockiNail commented 2 years ago

Thanks @simonbrunel I'm submitting new PR with your hint

stockiNail commented 2 years ago

PR #323