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

LineChart/StackedArea: Improve labelling when space is tight #3351

Closed
Tracked by #3561
sophiamersmann opened this issue Mar 15, 2024 · 1 comment · Fixed by #3647
Closed
Tracked by #3561

LineChart/StackedArea: Improve labelling when space is tight #3351

sophiamersmann opened this issue Mar 15, 2024 · 1 comment · Fixed by #3647

Comments

@sophiamersmann
Copy link
Member

Description

  • When space is tight, the labelling algorithm used in line charts and stacked area charts doesn't do a good job
  • This has been noted by Pablo A when trying to prepare a mobile static chart for data insights (see screenshots below)
  • This is also the reason why we don't re-use the labelling algorithm for slope charts

Expected behaviour

  • Show as many labels as possible, even when space is tight

Steps to reproduce

  1. Go to https://owid.cloud/admin/charts/383/edit
  2. Click on the 'Export' tab
  3. Switch to the mobile version

Screenshots

When space is tight:

Screenshot 2024-03-15 at 12 02 04

If there is more space (here achieved by removing text elements):

Screenshot 2024-03-15 at 12 02 29
@danyx23
Copy link
Contributor

danyx23 commented Apr 3, 2024

Discussed this in issue triage - because this now comes up more in thumbnails and mobile chart exports we think it's important to fix this.

To fix it, we should probably drop entities from the list of labelled entities if there is insufficient space until there is enough space. It's a bit unclear if we should drop entities by lowest value in the last year first or by area in the visible chart space.

@sophiamersmann sophiamersmann self-assigned this May 27, 2024
sophiamersmann added a commit that referenced this issue Jun 11, 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)](https://github.com/owid/owid-grapher/assets/12461810/71f8a036-784d-4ae1-a146-096bdc47f9d4) | ![total-population-living-in-extreme-poverty-by-world-region-mobile](https://github.com/owid/owid-grapher/assets/12461810/0c7c76c5-5869-4871-a7de-87024ed96e48) |
| <img width="317" alt="Screenshot 2024-05-28 at 16 15 48" src="https://github.com/owid/owid-grapher/assets/12461810/325ab991-4cc3-4b41-a9c9-c94c69126765"> |<img width="317" alt="Screenshot 2024-05-28 at 16 36 15" src="https://github.com/owid/owid-grapher/assets/12461810/5f2cf38c-1419-47af-a4b8-085e39c718e6">|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants