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

Formatter for plot axis labels #1130

Merged
merged 7 commits into from Jan 24, 2022
Merged

Formatter for plot axis labels #1130

merged 7 commits into from Jan 24, 2022

Conversation

Bromeon
Copy link
Contributor

@Bromeon Bromeon commented Jan 17, 2022

Adds two methods Plot::x_axis_formatter and Plot::y_axis_formatter().
Also fixes the bug where Orientation was used in public fields, but wasn't public itself.

With them, it's possible to customize the displayed labels along the axes.

Plot::new("Complex plane")
    .y_axis_formatter(|val| format!("{}i"))
    .show(ui, |plot_ui| plot_ui.bar_chart(chart))

It's also possible to return the empty string, in order to not display a label. This can be useful for discrete X/Y domains, when there isn't a meaningful label beyond certain resolution or zoom level. An example can be dates, with only full days.

Here's a screenshot of the stacked bar-chart, with X axis labeled as days and Y axis as percentage.
This example is now implemented in the plot demo.
grafik

Closes #725.

@Bromeon
Copy link
Contributor Author

Bromeon commented Jan 17, 2022

One open question is positioning of the labels.

In the screenshot above I had to "hide" the Y=0 value 0%, because it would overlap with the X=0 label Day 0. I did this by returning an empty string in the formatter.

Problems like these could be solved by specifying the alignment (left/right/center) and orientation (horizontal/vertical) of the text along the axis. But I think it's an orthogonal problem to text formatting (no pun intended), so it should be possible to do it in a separate PR.

@Bromeon Bromeon marked this pull request as ready for review January 17, 2022 18:14
CHANGELOG.md Outdated Show resolved Hide resolved
@Bromeon
Copy link
Contributor Author

Bromeon commented Jan 24, 2022

Fixed merge conflicts.

CHANGELOG.md Show resolved Hide resolved
Copy link
Owner

@emilk emilk left a comment

Choose a reason for hiding this comment

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

Looks good! Just left a couple of nits

egui_demo_lib/src/apps/demo/plot_demo.rs Outdated Show resolved Hide resolved
egui/src/widgets/plot/mod.rs Outdated Show resolved Hide resolved
@Bromeon
Copy link
Contributor Author

Bromeon commented Jan 24, 2022

Applied the suggestions.


Sorry for the many CI runs, wasn't aware that previous runs would complete. In case you're interested, it's possible to abort previous runs as done here:

# If a new commit is pushed before the old one's CI has completed (on the same branch), abort previous run
concurrency:
  group: ${{ github.head_ref }}
  cancel-in-progress: true

This might only work for the pull_request and not the push trigger however (not sure if you use CI for the latter, too, or if you do all changes via PRs).

@emilk emilk merged commit 366f544 into emilk:master Jan 24, 2022
@Bromeon Bromeon deleted the feature/custom-plot-axes branch January 24, 2022 21:34
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.

Configurable Plot Axis Notation
2 participants