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

get_item_rect_size for plot annotations? #2302

Open
giogina opened this issue Mar 18, 2024 · 6 comments
Open

get_item_rect_size for plot annotations? #2302

giogina opened this issue Mar 18, 2024 · 6 comments
Labels
state: pending not addressed yet

Comments

@giogina
Copy link

giogina commented Mar 18, 2024

Is your feature request related to a problem? Please describe.
I'm trying to distribute a large amount of plot annotations dynamically in such a way that they don't overlap - and for that, I need the pixel sizes of the annotations. However, get_item_rect_size gives me a KeyError: 'rect_size', as the item_state dict is just {'ok': True, 'pos': [0, 0]}.
Describe the solution you'd like
Would be great to have get_item_rect_size working for annotations.

Describe alternatives you've considered
Is it possible to get the character heights/widths from the font textures directly? I assume those textures are saved somewhere explicitly, but I have no idea how to get them.

@giogina giogina added the state: pending not addressed yet label Mar 18, 2024
@v-ein
Copy link
Contributor

v-ein commented Mar 18, 2024

Is it possible to get the character heights/widths from the font textures directly?

Absolutely yes. dpg.get_text_size() does what you need; note, however, that it doesn't work until at least one frame is rendered (and if you're using non-ASCII characters, e.g. via dpg.add_font_range, wait 2 frames, not 1). Use set_frame_callback to postpone initialization until frame 2, if needed.

Also, all paddings and spacings can be hardcoded based off what values you specify in the themes or what defaults the theme has. See dpg.show_style_editor for the defaults. Like, a button width is the width of its label plus 2x frame padding.

@giogina
Copy link
Author

giogina commented Mar 19, 2024

Thanks, that works great!

By the way - if you don't mind me throwing in another annotation-related question - if I want annotations without the boxes (so, the text part colored, the box transparent), is there any way of making the text colored? I've found that if I set the box to be completely transparent, the text is white, which mostly works for me. But being able to make the text the same color as its line series would be awesome.

@v-ein
Copy link
Contributor

v-ein commented Mar 19, 2024

If you specify the background color as (0, 0, 0, 0) - the last zero is important here - ImPlot should be using the color mvPlotCol_InlayText for the text. You can then bind a theme to add_line_series (not to the annotation - annotations don't support themes) and configure that color in the theme. I haven't tried it myself, so let me know if it doesn't work for you.

@giogina
Copy link
Author

giogina commented Mar 19, 2024

Thanks! But I'm confused - what do you mean by binding the theme to add_line_series? Annotations belong to the plot, right? Can they be part of a line series? (Which would be super useful, as my annotations belong to different line series and I'd like them to be the same color as their plots to keep things clear)
I tried changing mvPlotCol_InlayText in my plot theme, but the text is still white.

@v-ein
Copy link
Contributor

v-ein commented Mar 19, 2024

Oh, yes, annotations can only belong to the plot. That said, you can only assign a single mvPlotCol_InlayText to all annotations in the plot (at least to those with transparent background). If you have multiple line series, I'm not sure whether possible to colorize annotations accordingly. Also, mvPlotCol_InlayText affects other text labels, like the coordinates of the hovered point shown in the bottom-right corner, so might be a poor choice after all.

I tried changing mvPlotCol_InlayText in my plot theme, but the text is still white.

Have you added category=dpg.mvThemeCat_Plots to dpg.add_theme_color?

@v-ein
Copy link
Contributor

v-ein commented Mar 19, 2024

If you really need it you might try overlaying multiple plots, each having a single line_series and annotations of the corresponding color. The problem is, the user will only be able to interact with the first plot, and all others will be drawn upon it. So if the user scales it or moves, you'll need to synchronize other plots as well. Haven't tried it, just a wild idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: pending not addressed yet
Projects
None yet
Development

No branches or pull requests

2 participants