-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Font in root of the options does not get applied. #10375
Comments
I'm having some trouble looking at this issue and could use some guidance. From what I read in the docs, to change the default font for a chart you'd have to modify the defaults object directly:
But to support specifying the default font in the root of options would you have to modify the defaults object inside the Chart constructor? Apologies if what I'm saying is confusing, it's my first time contributing to open source. |
Yes, like you said with underneath code you can modify the defaults. But this code modifies the default fontSize for all the charts. So if you have multiple charts in your application all of them now have a font size of 50 for all the elements in the chart that do not specify a different font size. Chart.defaults.font.size = 50 Using below syntax should apply a fontSize of 50 to all the elements in only that specific chart. So if I have multiple charts all the other charts still have the default font size: new Chart('canvasId', {
data,
options: {
font: {
size: 50
}
}
}) I hope this cleared it up for you |
Thank you for the clarification LeeLenaleee! I looked a little deeper into the plugins that use the font, and I noticed they use the toFont helper function to generate the font: Chart.js/src/plugins/plugin.legend.js Line 128 in 0616868
Chart.js/src/plugins/plugin.title.js Line 92 in 0616868
Taking a deeper look at the toFont function lead me to this: Chart.js/src/helpers/helpers.options.js Lines 107 to 133 in 0616868
The font is either the specified font option provided specifically by the plugin calling it or it defaults to fallback. For all the toFont calls however, there is no fallback provided and the fallback becomes the defaults object font. To resolve this issue, should the toFont function in all the plugins be supplied with a proper fallback (options.font)? |
I guess that everywhere that toFont is used the fallback has to be provided with Chart.js/src/core/core.scale.js Line 642 in 0616868
Chart.js/src/core/core.scale.js Lines 99 to 104 in 0616868
Since the helper function does not have access to the chart instance I guess (can be wrong did not test) so then everywhere where |
Thank you for the guidance, I'll make a pull request to change all the toFont() calls to include |
Another option is adding |
@kurkle That solution looks a lot easier to implement.
If I'm understanding correctly, it'd only be an issue if a Chart relied on the defaults and accidently overrode them using the root option scope. In my opinion though, this should be the ideal behaviour for any chart options. I think it should be fine to implement. What do you think? |
Hello! I am a little late to the party maybe, but I am suffering from the same / similar problem, using version 3.9.1. The same default value
The |
Expected behavior
When specifieng font options in the root of the options object it should get applied to all the text which does not get overriden explicetly.
Current behavior
It does nothing. While according to the current typings it should make it the default base font:
Chart.js/types/index.esm.d.ts
Lines 1466 to 1470 in cf780a5
Reproducible sample
https://jsfiddle.net/2L1pdtsz/
Optional extra steps/info to reproduce
No response
Possible solution
No response
Context
Found this while working on #10364
chart.js version
3.7.1
Browser name and version
No response
Link to your project
No response
The text was updated successfully, but these errors were encountered: