Skip to content

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

Closed
mihaly-sisak wants to merge 8 commits intoepezent:masterfrom
mihaly-sisak:master
Closed

Custom per-item colors for line and scatter plot#608
mihaly-sisak wants to merge 8 commits intoepezent:masterfrom
mihaly-sisak:master

Conversation

@mihaly-sisak
Copy link
Copy Markdown
Contributor

@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.

@mihaly-sisak
Copy link
Copy Markdown
Contributor Author

Renamed fill_colors() to FillColors() I dont know how I did not realize the proper naming convention.

Modifies implot.h DragPoint, DragLineX, DragLineY, DragRect functions held argument to out_held.
The last parameter is called out_held in the implementation cpp file.
This change brings that to the header, provides clearer understanding for the user.
@brenocq
Copy link
Copy Markdown
Collaborator

brenocq commented Apr 4, 2026

Hi @mihaly-sisak! I'm closing this PR since #672 was merged. Thank you so much for the initial effort, your code served as a lot of inspiration while implementing #672 😃

@brenocq brenocq closed this Apr 4, 2026
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