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

✨ (grapher) improve line legend algorithm #3647

Merged
merged 2 commits into from
Jun 11, 2024
Merged

Conversation

sophiamersmann
Copy link
Member

@sophiamersmann sophiamersmann commented May 24, 2024

Resolves #3351

Current implementation

The current implementation uses two different placing algorithms:

  1. If all labels fit into the space, then labels are placed in a non-overlapping way using connector lines
  2. If the labels don't fit into the available space, then labels are simply placed at their y-value and overlapping labels are greyed out and rendered into the background

Updated implementation

I got rid of the second placing algorithm. Instead, if not all labels fit into the available space, then we drop as many labels as necessary, and then the first placing algorithm is used to compute the positions of the remaining labels.

How labels are dropped

The default strategy, used by line charts, tries to pick labels in a balanced way so that they're roughly at equidistance from each other. Stacked area charts overwrite this behaviour and drop labels based on the area size.

The default strategy works like this: Given a set of placed labels and a set of candidates, for each candidate, we find the two closest already placed labels, one to each side, and calculate a score based on the available space between the two placed labels (the bigger, the better) and the candidate's distance to the midpoint (the smaller, the better). We then pick the candidate with the best score that fits into the available space.

Future Work

Lines/Areas that don't get labelled should show a label on demand (previously, you could hover over the grayed-out background labels). Stacked area charts highlight the currently hovered area in the tooltip – we could do something similar for line charts. To be worked on...

Before After
total-population-living-in-extreme-poverty-by-world-region-mobile (1) total-population-living-in-extreme-poverty-by-world-region-mobile
Screenshot 2024-05-28 at 16 15 48 Screenshot 2024-05-28 at 16 36 15

Copy link
Member Author

sophiamersmann commented May 24, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @sophiamersmann and the rest of your teammates on Graphite Graphite

@owidbot
Copy link
Contributor

owidbot commented May 24, 2024

Quick links (staging server):

Site Admin Wizard

Login: ssh owid@staging-site-line-legend-algo

SVG tester:

Number of differences (default views): 5 (9f2da8) ❌
Number of differences (all views): 0 ✅

Edited: 2024-06-11 09:33:45 UTC
Execution time: 1.35 seconds

@sophiamersmann sophiamersmann force-pushed the line-legend-algo branch 4 times, most recently from 995454e to 76e94f0 Compare May 27, 2024 08:53
Copy link

@sophiamersmann sophiamersmann force-pushed the line-legend-algo branch 2 times, most recently from c1dee4e to 2879b3b Compare May 27, 2024 14:12
@sophiamersmann sophiamersmann marked this pull request as ready for review May 28, 2024 08:53
@sophiamersmann sophiamersmann requested review from marcelgerber and removed request for marcelgerber May 28, 2024 09:20
@sophiamersmann sophiamersmann marked this pull request as draft May 28, 2024 10:25
@sophiamersmann sophiamersmann force-pushed the line-legend-algo branch 5 times, most recently from 81f49db to ea4136e Compare May 29, 2024 12:52
@sophiamersmann sophiamersmann marked this pull request as ready for review May 29, 2024 13:35
Copy link
Member

@marcelgerber marcelgerber left a comment

Choose a reason for hiding this comment

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

Great idea!
I had a performance comment, and also found a case where this errors, but otherwise this is great, and certainly a nice improvement 🙌🏻

Co-authored-by: Marcel Gerber <mgerber59@gmail.com>

🤖 style: prettify code
Copy link
Member Author

sophiamersmann commented Jun 11, 2024

Merge activity

@sophiamersmann sophiamersmann merged commit c0ff650 into master Jun 11, 2024
20 of 21 checks passed
@sophiamersmann sophiamersmann deleted the line-legend-algo branch June 11, 2024 09:49
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.

LineChart/StackedArea: Improve labelling when space is tight
3 participants