Skip to content

Commit bf03a2d

Browse files
committed
add SetNextColorsData to give per point colors
Implementation and demo for lines, scatter, VLines and HLines
1 parent b47c8ba commit bf03a2d

File tree

4 files changed

+350
-57
lines changed

4 files changed

+350
-57
lines changed

implot.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,8 @@ IMPLOT_API void SetNextFillStyle(const ImVec4& col = IMPLOT_AUTO_COL, float alph
968968
IMPLOT_API void SetNextMarkerStyle(ImPlotMarker marker = IMPLOT_AUTO, float size = IMPLOT_AUTO, const ImVec4& fill = IMPLOT_AUTO_COL, float weight = IMPLOT_AUTO, const ImVec4& outline = IMPLOT_AUTO_COL);
969969
// Set the error bar style for the next item only.
970970
IMPLOT_API void SetNextErrorBarStyle(const ImVec4& col = IMPLOT_AUTO_COL, float size = IMPLOT_AUTO, float weight = IMPLOT_AUTO);
971+
// Set color buffer for the next item only. The colors array must be keep alive until the next item plot.
972+
IMPLOT_API void SetNextColorsData(ImPlotCol_ target, ImU32* const& colors, int stride = sizeof(ImU32));
971973

972974
// Gets the last item primary color (i.e. its legend icon color)
973975
IMPLOT_API ImVec4 GetLastItemColor();

implot_demo.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,99 @@ void ShowDemo_MarkersAndText() {
926926
}
927927
}
928928

929+
void ShowDemo_PlotColors() {
930+
// Lines
931+
{
932+
static float xs1[1001], ys1[1001];
933+
ImU32 cs1[1001];
934+
for (int i = 0; i < 1001; ++i) {
935+
xs1[i] = i * 0.001f;
936+
ys1[i] = 0.5f + 0.5f * sinf(50 * (xs1[i] + (float)ImGui::GetTime() / 10));
937+
ImVec4 lineColor = ImPlot::SampleColormap(ys1[i], ImPlotColormap_Spectral);
938+
lineColor.w = 1 - powf(1 - (i * 0.001f), 4);
939+
cs1[i] = ImGui::GetColorU32(lineColor);
940+
}
941+
static double xs2[11], ys2[11];
942+
ImU32 cls2[11], cms2[11];
943+
for (int i = 0; i < 11; ++i) {
944+
xs2[i] = i * 0.1f;
945+
ys2[i] = xs2[i] * xs2[i];
946+
cls2[i] = ImGui::GetColorU32(ImPlot::SampleColormap(float(ys2[i]), ImPlotColormap_Twilight));
947+
cms2[i] = ImGui::GetColorU32(ImPlot::RandomColor());
948+
}
949+
if (ImPlot::BeginPlot("Line Plot with colors")) {
950+
ImPlot::SetupAxes("x", "f(x)");
951+
ImVec4 s1Color = ImPlot::SampleColormap(0.5f, ImPlotColormap_Spectral);
952+
ImPlot::SetNextLineStyle(s1Color);
953+
ImPlot::SetNextColorsData(ImPlotCol_Line, cs1);
954+
ImPlot::PlotLine("sin(x)", xs1, ys1, 1001);
955+
ImVec4 s2Color = ImPlot::SampleColormap(0.5f, ImPlotColormap_Twilight);
956+
ImPlot::SetNextLineStyle(s2Color);
957+
ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
958+
ImPlot::SetNextColorsData(ImPlotCol_Line, cls2);
959+
ImPlot::SetNextColorsData(ImPlotCol_MarkerFill, cms2);
960+
ImPlot::SetNextColorsData(ImPlotCol_MarkerOutline, cms2);
961+
ImPlot::PlotLine("x^2", xs2, ys2, 11);
962+
ImPlot::EndPlot();
963+
}
964+
}
965+
// Scatter
966+
{
967+
srand(0);
968+
static float xs1[100], ys1[100];
969+
static ImU32 cs1[1001];
970+
for (int i = 0; i < 100; ++i) {
971+
xs1[i] = i * 0.01f;
972+
ys1[i] = xs1[i] + 0.1f * ((float)rand() / (float)RAND_MAX);
973+
cs1[i] = ImGui::GetColorU32(ImPlot::SampleColormap(10 * (ys1[i] - xs1[i]), ImPlotColormap_Viridis));
974+
}
975+
static float xs2[50], ys2[50];
976+
ImU32 cmos2[50], cmfs2[50];
977+
978+
for (int i = 0; i < 50; i++) {
979+
xs2[i] = 0.25f + 0.2f * ((float)rand() / (float)RAND_MAX);
980+
ys2[i] = 0.75f + 0.2f * ((float)rand() / (float)RAND_MAX);
981+
982+
ImVec4 markerOutlineColor = ImPlot::SampleColormap(((float)rand() / (float)RAND_MAX), ImPlotColormap_Plasma);
983+
ImVec4 markerFillColor = ImVec4(markerOutlineColor.x, markerOutlineColor.x, markerOutlineColor.z, 0.5);
984+
cmfs2[i] = ImGui::GetColorU32(markerFillColor);
985+
cmos2[i] = ImGui::GetColorU32(markerOutlineColor);
986+
}
987+
988+
if (ImPlot::BeginPlot("Scatter Plot with colors")) {
989+
ImVec4 s1Color = ImPlot::SampleColormap(0.5f, ImPlotColormap_Viridis);
990+
ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle, 4, s1Color, IMPLOT_AUTO, s1Color);
991+
ImPlot::SetNextColorsData(ImPlotCol_MarkerOutline, cs1);
992+
ImPlot::SetNextColorsData(ImPlotCol_MarkerFill, cs1);
993+
ImPlot::PlotScatter("Data 1", xs1, ys1, 100);
994+
995+
ImVec4 s2Color = ImPlot::SampleColormap(0.5f, ImPlotColormap_Plasma);
996+
ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 6, s2Color, IMPLOT_AUTO, s2Color);
997+
ImPlot::SetNextColorsData(ImPlotCol_MarkerOutline, cmos2);
998+
ImPlot::SetNextColorsData(ImPlotCol_MarkerFill, cmfs2);
999+
ImPlot::PlotScatter("Data 2", xs2, ys2, 50);
1000+
1001+
ImPlot::EndPlot();
1002+
}
1003+
}
1004+
1005+
// Infinite lines
1006+
{
1007+
static double vals[] = { 0.25, 0.5, 0.75 };
1008+
static ImU32 colors[] = { ImGui::GetColorU32(ImPlot::RandomColor()), ImGui::GetColorU32(ImPlot::RandomColor()), ImGui::GetColorU32(ImPlot::RandomColor()) };
1009+
if (ImPlot::BeginPlot("##Infinite with colors")) {
1010+
ImPlot::SetupAxes(NULL, NULL, ImPlotAxisFlags_NoInitialFit, ImPlotAxisFlags_NoInitialFit);
1011+
ImPlot::SetNextLineStyle(ImVec4(1, 1, 1, 0.5));
1012+
ImPlot::SetNextColorsData(ImPlotCol_Line, colors);
1013+
ImPlot::PlotVLines("VLines", vals, 3);
1014+
ImPlot::SetNextLineStyle(ImVec4(1, 1, 1, 0.5));
1015+
ImPlot::SetNextColorsData(ImPlotCol_Line, colors);
1016+
ImPlot::PlotHLines("HLines", vals, 3);
1017+
ImPlot::EndPlot();
1018+
}
1019+
}
1020+
}
1021+
9291022
void ShowDemo_LogAxes() {
9301023
static double xs[1001], ys1[1001], ys2[1001], ys3[1001];
9311024
for (int i = 0; i < 1001; ++i) {
@@ -2032,6 +2125,8 @@ void ShowDemoWindow(bool* p_open) {
20322125
ShowDemo_Images();
20332126
if (ImGui::CollapsingHeader("Markers and Text"))
20342127
ShowDemo_MarkersAndText();
2128+
if (ImGui::CollapsingHeader("Plot Colors"))
2129+
ShowDemo_PlotColors();
20352130
ImGui::EndTabItem();
20362131
}
20372132
if (ImGui::BeginTabItem("Subplots")) {

implot_internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,8 @@ struct ImPlotNextPlotData
10781078
// Temporary data storage for upcoming item
10791079
struct ImPlotNextItemData {
10801080
ImVec4 Colors[5]; // ImPlotCol_Line, ImPlotCol_Fill, ImPlotCol_MarkerOutline, ImPlotCol_MarkerFill, ImPlotCol_ErrorBar
1081+
ImU32 const* ColorsData[5];
1082+
int ColorsDataStride[5];
10811083
float LineWeight;
10821084
ImPlotMarker Marker;
10831085
float MarkerSize;
@@ -1097,7 +1099,10 @@ struct ImPlotNextItemData {
10971099
ImPlotNextItemData() { Reset(); }
10981100
void Reset() {
10991101
for (int i = 0; i < 5; ++i)
1102+
{
11001103
Colors[i] = IMPLOT_AUTO_COL;
1104+
ColorsData[i] = nullptr;
1105+
}
11011106
LineWeight = MarkerSize = MarkerWeight = FillAlpha = ErrorBarSize = ErrorBarWeight = DigitalBitHeight = DigitalBitGap = IMPLOT_AUTO;
11021107
Marker = IMPLOT_AUTO;
11031108
HasHidden = Hidden = false;

0 commit comments

Comments
 (0)