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

Custom per-item colors for line and scatter plot #608

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

mihaly-sisak
Copy link

@mihaly-sisak mihaly-sisak commented Jan 18, 2025

Hello!
I've implemented user-settable per-item colors for line and scatter plots.
I've tried to match your coding style as close as I was able.
Please consider merging this pull request, or provide feedback how can I improve the implementation so it can be merged.
All previously available public plot APIs are unchanged.
I've added two new plot types:

typedef ImU32 (*ImPlotColorGetter)(ImPlotCol col, int idx, void* user_data);
IMPLOT_API void PlotLineCG(const char* label_id, ImPlotGetter getter_func, void* data, int count, ImPlotColorGetter color_func, void* color_data, ImPlotLineFlags flags=0);
IMPLOT_API void PlotScatterCG(const char* label_id, ImPlotGetter getter, void* data, int count, ImPlotColorGetter color_func, void* color_data, ImPlotScatterFlags flags=0);

The user can write a callback to set the item color based on the index.
I've added to the demo to showcase the new APIs.
The single-color plots are drawn with a new struct ColorGetter that is basically just a wrapper around ImGui::GetColorU32(GetItemData().Colors[col]).
I wanted to cache the plot colors so the ImVec4->ImU32 conversion happens only once. If you can give me any direction how can I achieve this more elegantly (than the current fill_colors()) please point me to the right direction. With this setup the ImVec4->ImU32 conversion happens only once, just as before with the single-color only version.
The per-item-colored plots are drawn with struct ColorGetterFuncPtr, implemented in the same style as struct GetterXY and struct GetterFuncPtr.

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.

1 participant