-
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
Legend centering and label click zones broken with non-standard font #11384
Comments
Note, that this applies to axis label positioning as well (see the y labels in my example). Note that it doesn't make a difference, if I load the font via CSS font-face or JavaScript's FontFace API as suggested here: https://www.chartjs.org/docs/latest/general/fonts.html Am I missing something, or is this an actual bug? |
In terms of how we actually do the measuring, we're relying on If you hadn't already tried to wait for the fonts to be ready, that would have been my first suggestion. The code for us to investigate will be Chart.js/src/helpers/helpers.canvas.ts Lines 39 to 55 in f2cfb93
What's curious is that when the Y axis maximum is < 5000, the labels are fine but they're wrong otherwise. It almost seems like in some cases we're sending the wrong labels to be measured |
Thanks for the feedback! That helped me to further hunt down the issue. At least in Firefox and Safari (but not in Chrome), the problem disappears if I assign the font to the canvas element via CSS, instead of just doing It seems that the measuring uses the default font. |
Forgive me if I'm jumping in the thread (I was curious). I had a quick look to the code. @etimberg the I have anyway found another weird behavior using the code of @bolau. It seems something related to the max width acceptable on the legend, It seems (but not sure yet, I'm still having a look), that there is a different control between update (to calculate the hitboxes and location) and draw phases. It could be also related to PR #11352 which solved for vertical legend. |
@bolau @etimberg after a check, the code sound being correct. But I see that the font is not loaded when chart is invoked. const fontface = new FontFace("Vera", "url(Vera.ttf)")
document.fonts.add(fontface);
const config = {
type: 'line',
},
.....
}
window.onload = function() {
fontface.load().then(function() {
Chart.defaults.font.family = 'Vera';
var ctx = document.getElementById('chartcanvas').getContext('2d');
window.chart = new Chart(ctx, config);
});
}; I have tested locally and sounds working. |
@stockiNail very nice, indeed! This works! Thank you so much for your help! Should we close this issue? |
UR welcome
Yes, I think so. |
Thanks @stockiNail, good find on the font loading |
Expected behavior
Even with a non-standard font, the legend should be centered properly, and clicking on a label should disable the correct dataset.
Current behavior
The legend seems to be positioned based on the wrong font width. Clicking on the right half of the second last label hides the last dataset instead of the second last. After that, the legend is re-centered, now correctly, and the click regions are correct.
Reproducible sample
https://www.borislau.de/chartjs_bug.html
Optional extra steps/info to reproduce
Chart.defaults.font.family = 'Permanent Marker';
Possible solution
Didn't find a workaround.
Even waiting for document.fonts.ready doesn't help:
Context
I want to make a beautiful chart with the Bitstream Vera font, and I want to be able to hide and show datasets by clicking on the entries in the legend.
chart.js version
v4.3.0
Browser name and version
Firefox, Chrome, Safari
Link to your project
https://www.borislau.de/chartjs_bug.html
The text was updated successfully, but these errors were encountered: