Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fit method to LegendElement interface #11796

Merged
merged 1 commit into from
May 31, 2024

Conversation

MichelHMachado
Copy link
Contributor

Purpose
This pull request aims to add the fit method to the LegendElement interface in Chart.js. The primary purpose of this change is to enable the usage of chart.legend.fit within custom plugins, ensuring TypeScript correctly recognizes and supports this method.

Background
While working on a custom plugin that adds a margin below the legend, I encountered a TypeScript error indicating that the fit method was not recognized. The fit method is, however, present on the legend object at runtime. To address this discrepancy and improve TypeScript support, I propose adding the fit method to the LegendElement interface.

Changes
Added the fit method to the LegendElement interface in Chart.js type definitions.
Example Usage
Here's an example of how this change can be utilized in a custom plugin:

const legendMargin: Plugin = { id: 'legendMargin', afterInit(chart, args, plugins) { const originalFit = chart?.legend?.fit; const margin = plugins.margin || 0; if (chart.legend) { chart.legend.fit = function fit() { if (originalFit) { originalFit.call(this); } return (this.height += margin); }; } }, };

@LeeLenaleee LeeLenaleee added the type: types Typescript type changes label May 31, 2024
@LeeLenaleee LeeLenaleee added this to the 4.5.0 milestone May 31, 2024
@etimberg etimberg merged commit 4ee6c9e into chartjs:master May 31, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: types Typescript type changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants