Skip to content

Commit

Permalink
add SetNextColorsData to give per point colors
Browse files Browse the repository at this point in the history
Implementation and demo for lines, scatter, VLines and HLines
  • Loading branch information
niavok committed Feb 21, 2022
1 parent b47c8ba commit bf03a2d
Show file tree
Hide file tree
Showing 4 changed files with 350 additions and 57 deletions.
2 changes: 2 additions & 0 deletions implot.h
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,8 @@ IMPLOT_API void SetNextFillStyle(const ImVec4& col = IMPLOT_AUTO_COL, float alph
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);
// Set the error bar style for the next item only.
IMPLOT_API void SetNextErrorBarStyle(const ImVec4& col = IMPLOT_AUTO_COL, float size = IMPLOT_AUTO, float weight = IMPLOT_AUTO);
// Set color buffer for the next item only. The colors array must be keep alive until the next item plot.
IMPLOT_API void SetNextColorsData(ImPlotCol_ target, ImU32* const& colors, int stride = sizeof(ImU32));

// Gets the last item primary color (i.e. its legend icon color)
IMPLOT_API ImVec4 GetLastItemColor();
Expand Down
95 changes: 95 additions & 0 deletions implot_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,99 @@ void ShowDemo_MarkersAndText() {
}
}

void ShowDemo_PlotColors() {
// Lines
{
static float xs1[1001], ys1[1001];
ImU32 cs1[1001];
for (int i = 0; i < 1001; ++i) {
xs1[i] = i * 0.001f;
ys1[i] = 0.5f + 0.5f * sinf(50 * (xs1[i] + (float)ImGui::GetTime() / 10));
ImVec4 lineColor = ImPlot::SampleColormap(ys1[i], ImPlotColormap_Spectral);
lineColor.w = 1 - powf(1 - (i * 0.001f), 4);
cs1[i] = ImGui::GetColorU32(lineColor);
}
static double xs2[11], ys2[11];
ImU32 cls2[11], cms2[11];
for (int i = 0; i < 11; ++i) {
xs2[i] = i * 0.1f;
ys2[i] = xs2[i] * xs2[i];
cls2[i] = ImGui::GetColorU32(ImPlot::SampleColormap(float(ys2[i]), ImPlotColormap_Twilight));
cms2[i] = ImGui::GetColorU32(ImPlot::RandomColor());
}
if (ImPlot::BeginPlot("Line Plot with colors")) {
ImPlot::SetupAxes("x", "f(x)");
ImVec4 s1Color = ImPlot::SampleColormap(0.5f, ImPlotColormap_Spectral);
ImPlot::SetNextLineStyle(s1Color);
ImPlot::SetNextColorsData(ImPlotCol_Line, cs1);
ImPlot::PlotLine("sin(x)", xs1, ys1, 1001);
ImVec4 s2Color = ImPlot::SampleColormap(0.5f, ImPlotColormap_Twilight);
ImPlot::SetNextLineStyle(s2Color);
ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
ImPlot::SetNextColorsData(ImPlotCol_Line, cls2);
ImPlot::SetNextColorsData(ImPlotCol_MarkerFill, cms2);
ImPlot::SetNextColorsData(ImPlotCol_MarkerOutline, cms2);
ImPlot::PlotLine("x^2", xs2, ys2, 11);
ImPlot::EndPlot();
}
}
// Scatter
{
srand(0);
static float xs1[100], ys1[100];
static ImU32 cs1[1001];
for (int i = 0; i < 100; ++i) {
xs1[i] = i * 0.01f;
ys1[i] = xs1[i] + 0.1f * ((float)rand() / (float)RAND_MAX);
cs1[i] = ImGui::GetColorU32(ImPlot::SampleColormap(10 * (ys1[i] - xs1[i]), ImPlotColormap_Viridis));
}
static float xs2[50], ys2[50];
ImU32 cmos2[50], cmfs2[50];

for (int i = 0; i < 50; i++) {
xs2[i] = 0.25f + 0.2f * ((float)rand() / (float)RAND_MAX);
ys2[i] = 0.75f + 0.2f * ((float)rand() / (float)RAND_MAX);

ImVec4 markerOutlineColor = ImPlot::SampleColormap(((float)rand() / (float)RAND_MAX), ImPlotColormap_Plasma);
ImVec4 markerFillColor = ImVec4(markerOutlineColor.x, markerOutlineColor.x, markerOutlineColor.z, 0.5);
cmfs2[i] = ImGui::GetColorU32(markerFillColor);
cmos2[i] = ImGui::GetColorU32(markerOutlineColor);
}

if (ImPlot::BeginPlot("Scatter Plot with colors")) {
ImVec4 s1Color = ImPlot::SampleColormap(0.5f, ImPlotColormap_Viridis);
ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle, 4, s1Color, IMPLOT_AUTO, s1Color);
ImPlot::SetNextColorsData(ImPlotCol_MarkerOutline, cs1);
ImPlot::SetNextColorsData(ImPlotCol_MarkerFill, cs1);
ImPlot::PlotScatter("Data 1", xs1, ys1, 100);

ImVec4 s2Color = ImPlot::SampleColormap(0.5f, ImPlotColormap_Plasma);
ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 6, s2Color, IMPLOT_AUTO, s2Color);
ImPlot::SetNextColorsData(ImPlotCol_MarkerOutline, cmos2);
ImPlot::SetNextColorsData(ImPlotCol_MarkerFill, cmfs2);
ImPlot::PlotScatter("Data 2", xs2, ys2, 50);

ImPlot::EndPlot();
}
}

// Infinite lines
{
static double vals[] = { 0.25, 0.5, 0.75 };
static ImU32 colors[] = { ImGui::GetColorU32(ImPlot::RandomColor()), ImGui::GetColorU32(ImPlot::RandomColor()), ImGui::GetColorU32(ImPlot::RandomColor()) };
if (ImPlot::BeginPlot("##Infinite with colors")) {
ImPlot::SetupAxes(NULL, NULL, ImPlotAxisFlags_NoInitialFit, ImPlotAxisFlags_NoInitialFit);
ImPlot::SetNextLineStyle(ImVec4(1, 1, 1, 0.5));
ImPlot::SetNextColorsData(ImPlotCol_Line, colors);
ImPlot::PlotVLines("VLines", vals, 3);
ImPlot::SetNextLineStyle(ImVec4(1, 1, 1, 0.5));
ImPlot::SetNextColorsData(ImPlotCol_Line, colors);
ImPlot::PlotHLines("HLines", vals, 3);
ImPlot::EndPlot();
}
}
}

void ShowDemo_LogAxes() {
static double xs[1001], ys1[1001], ys2[1001], ys3[1001];
for (int i = 0; i < 1001; ++i) {
Expand Down Expand Up @@ -2032,6 +2125,8 @@ void ShowDemoWindow(bool* p_open) {
ShowDemo_Images();
if (ImGui::CollapsingHeader("Markers and Text"))
ShowDemo_MarkersAndText();
if (ImGui::CollapsingHeader("Plot Colors"))
ShowDemo_PlotColors();
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Subplots")) {
Expand Down
5 changes: 5 additions & 0 deletions implot_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,8 @@ struct ImPlotNextPlotData
// Temporary data storage for upcoming item
struct ImPlotNextItemData {
ImVec4 Colors[5]; // ImPlotCol_Line, ImPlotCol_Fill, ImPlotCol_MarkerOutline, ImPlotCol_MarkerFill, ImPlotCol_ErrorBar
ImU32 const* ColorsData[5];
int ColorsDataStride[5];
float LineWeight;
ImPlotMarker Marker;
float MarkerSize;
Expand All @@ -1097,7 +1099,10 @@ struct ImPlotNextItemData {
ImPlotNextItemData() { Reset(); }
void Reset() {
for (int i = 0; i < 5; ++i)
{
Colors[i] = IMPLOT_AUTO_COL;
ColorsData[i] = nullptr;
}
LineWeight = MarkerSize = MarkerWeight = FillAlpha = ErrorBarSize = ErrorBarWeight = DigitalBitHeight = DigitalBitGap = IMPLOT_AUTO;
Marker = IMPLOT_AUTO;
HasHidden = Hidden = false;
Expand Down
Loading

0 comments on commit bf03a2d

Please sign in to comment.