Skip to content

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

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions implot.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,9 @@ typedef int (*ImPlotFormatter)(double value, char* buff, int size, void* user_da
// Callback signature for data getter.
typedef ImPlotPoint (*ImPlotGetter)(int idx, void* user_data);

// Callback signature for color getter.
typedef ImU32 (*ImPlotColorGetter)(ImPlotCol col, int idx, void* user_data);

// Callback signature for axis transform.
typedef double (*ImPlotTransform)(double value, void* user_data);

Expand Down Expand Up @@ -858,11 +861,13 @@ IMPLOT_API void SetNextAxesToFit();
IMPLOT_TMP void PlotLine(const char* label_id, const T* values, int count, double xscale=1, double xstart=0, ImPlotLineFlags flags=0, int offset=0, int stride=sizeof(T));
IMPLOT_TMP void PlotLine(const char* label_id, const T* xs, const T* ys, int count, ImPlotLineFlags flags=0, int offset=0, int stride=sizeof(T));
IMPLOT_API void PlotLineG(const char* label_id, ImPlotGetter getter, void* data, int count, ImPlotLineFlags flags=0);
IMPLOT_API void PlotLineCG(const char* label_id, ImPlotGetter getter_func, void* data, int count, ImPlotColorGetter color_func, void* color_data, ImPlotLineFlags flags=0);

// Plots a standard 2D scatter plot. Default marker is ImPlotMarker_Circle.
IMPLOT_TMP void PlotScatter(const char* label_id, const T* values, int count, double xscale=1, double xstart=0, ImPlotScatterFlags flags=0, int offset=0, int stride=sizeof(T));
IMPLOT_TMP void PlotScatter(const char* label_id, const T* xs, const T* ys, int count, ImPlotScatterFlags flags=0, int offset=0, int stride=sizeof(T));
IMPLOT_API void PlotScatterG(const char* label_id, ImPlotGetter getter, void* data, int count, ImPlotScatterFlags 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);

// Plots a a stairstep graph. The y value is continued constantly to the right from every x position, i.e. the interval [x[i], x[i+1]) has the value y[i]
IMPLOT_TMP void PlotStairs(const char* label_id, const T* values, int count, double xscale=1, double xstart=0, ImPlotStairsFlags flags=0, int offset=0, int stride=sizeof(T));
Expand Down
33 changes: 33 additions & 0 deletions implot_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2158,7 +2158,39 @@ void Demo_CustomPlottersAndTooltips() {
MyImPlot::PlotCandlestick("GOOGL",dates, opens, closes, lows, highs, 218, tooltip, 0.25f, bullCol, bearCol);
ImPlot::EndPlot();
}
}

void Demo_CustomColors() {

if (ImPlot::BeginPlot("Custom Colors")){
ImPlot::PlotLineCG("Line Plot",
[](int idx, void* user_data){
return ImPlotPoint(0.1*idx, sin(0.1*idx));
}, nullptr, 100,
[](ImPlotCol col, int idx, void* user_data){
switch (idx % 3){
case 0 : return 0xFF0000FFu;
case 1 : return 0xFF00FF00u;
case 2 : return 0xFFFF0000u;
default: return 0u;
}
}, nullptr);
ImPlot::PlotScatterCG("Scatter Plot",
[](int idx, void* user_data){
srand(idx);
return ImPlotPoint(10.0*float(rand())/float(RAND_MAX), -1.0+2.0*float(rand())/float(RAND_MAX));
}, nullptr, 20,
[](ImPlotCol col, int idx, void* user_data){
switch (idx % 3){
case 0 : return 0xFF0000FFu;
case 1 : return 0xFF00FF00u;
case 2 : return 0xFFFF0000u;
default: return 0u;
}
}, nullptr);
ImPlot::EndPlot();
}
}

//-----------------------------------------------------------------------------
// DEMO WINDOW
Expand Down Expand Up @@ -2247,6 +2279,7 @@ void ShowDemoWindow(bool* p_open) {
DemoHeader("Images", Demo_Images);
DemoHeader("Markers and Text", Demo_MarkersAndText);
DemoHeader("NaN Values", Demo_NaNValues);
DemoHeader("Custom Colors", Demo_CustomColors);
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Subplots")) {
Expand Down
Loading