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

fix long label being truncated by chartjs #632

Merged
merged 1 commit into from
Jan 27, 2025

Conversation

struan
Copy link
Member

@struan struan commented Jan 22, 2025

Split long labels into an array of strings no longer than 30 characters which chartjs will then render as multiple lines.

Fixes #436 (again)

@struan struan requested a review from zarino January 22, 2025 12:09
Copy link
Member

@zarino zarino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @struan. I discovered that if you set ticks: { autoSkip: false } on the axis, Chart.js will render every label, even if it thinks they’re going to be too closely smooshed together, which avoids the "chart.js is hiding labels when you set the max_length to something small like 20" problem.

I’ve tried this out locally, with a max_label_length of 20, and it seems to produce pretty good results on all the charts in my local environment, at least. Even labels nearing 60 characters have been fine (broken onto 3 lines).

I’ve also bumped up the right padding on the charts to 35px, as I noticed text was still sometimes getting cut off. (I think this cutting off happens because Chart.js calculates chart dimensions based on the labels being set in Arial, but then we apply a custom font, which results in slightly wider labels.)

@struan
Copy link
Member Author

struan commented Jan 27, 2025

I think 20 might be too short as you get lines smashing into each other vertically. I've tweaked it to 28 which is maybe a reasonable compromise.

Screenshot 2025-01-27 at 14 08 57

@struan struan force-pushed the fix-long-labels-on-range-charts branch 2 times, most recently from 5a5fa5f to 9c59327 Compare January 27, 2025 14:17
Split long labels into an array of strings no longer than 28 characters
which chartjs will then render as multiple lines.

Fixes #436 (again)
@struan struan force-pushed the fix-long-labels-on-range-charts branch from 9c59327 to e388fbc Compare January 27, 2025 14:41
@struan struan merged commit e388fbc into main Jan 27, 2025
5 checks passed
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.

'Neither' is truncated to 'her' in 'Public opinion' dataset
2 participants