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

Add Legend Formatter support #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rickyelopez
Copy link

Original PR in the egui repo: emilk/egui#4662

Original PR description:

Small change to introduce the ability to provide a function which (if provided) will be used to format the text used in the legend.

The motivation for this is to be able to plot enumerated signals in which the specific enum name is included in the name of the point/line (eg. enum_name::enumeration). Without this change there is no way (as far as I can tell) to get all of the points/lines into the same entry in the legend, so you end up with a legend entry for each possible enumeration. There may be a better way to accomplish this that I'm not seeing, though.

Additional clarification added as a comment in the original PR:

I think this is reasonable regardless of my usecase, but let me try to give an example. Maybe you will have a better idea.

Let's say the data I want to plot is represented by an enum:

enum TestEnum {
    Val1,
    Val2,
    Val3,
    etc...
}

where each enumeration is mapped to an integer representation (i.e. Val1 is represented by 1). Then, each point to plot is effectively a (Time, (TestEnum, Value)) tuple. The data to plot might look like:

let data: (u32, (TestEnum, u32)) = vec![
    (0, (TestEnum::Val1, 1)),
    (1, (TestEnum::Val1, 1)),
    (2, (TestEnum::Val2, 2)),
    (3, (TestEnum::Val3, 3)),
    (4, (TestEnum::Val4, 4)),
]

I want to see the enumeration name when hovering over the point, so the way that I have been doing it so far is to create Points with the time and enum value, and then setting the name for each point to a stringified representation of the enumeration.

This works ok for showing the data on the plot. I would actually prefer if there was a way to change the Y axis from showing floats to the actual enumerations, but it doesn't seem like there's a way to do that (at least that I've found so far).

The problem is that the points now all have different names (i.e. TestEnum::Val1, TestEnum::Val2, etc.) so that the names show in the hover, but that results in the legend showing all of the enumerations as different plots. With the legend_formatter I've added here, I can just split on the :: and drop the second part, effectively grouping each of the points into its enum regardless of the specific enumeration.

Continuing with the above example, the first entry in the data vec would result in a Point with time 0 and value 1, with the name TestEnum::Val1 shown on hover, but it would still show up in the legend under TestEnum rather than TestEnum::Val1 thanks to the modifications performed in the legend_formatter function. The rest of the points would then also end up under the TestEnum group in the legend.

Does this make sense? Like I said, there may be a better way to do this. I feel like the real solution (which would address not only this issue, but also the one mentioned above regarding the Y axis) would be to implement a new Widget such as EnumeratedPlot which would support y-axes with data types other than floats, and which could also support the legend behavior I'm looking for. That seems like a much bigger task, though, and I feel like this legend_formatter could also be beneficial in general.

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.

Please add a motivating example to the demo

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.

2 participants