Rocketmagnet
Rocketmagnet

Reputation: 5880

How do I specify the bar colors in ImPlot::DrawBarGroups?

I'm struggling to understand how the colors work in ImPlot.

Here, I'm plotting bars in groups of 3.

static const char* labels[] = {"X", "Y", "Z"};
if (ImPlot::BeginPlot("Sensor activity"))
{
    ImPlot::PlotBarGroups(labels, &(barData[0]), 3, numGroups, 1.0f, 0, 0);
    ImPlot::EndPlot();
}

I would like the X, Y, and Z bars to be plotted in Red, Green and Blue respectively. How would I do that?

Upvotes: 5

Views: 1214

Answers (2)

An important thing for future readers. Since there is (or I haven't found) any other post here or on GitHub on how to add custom colors for LinePlot. And I spent almost the whole day trying to figure out how to do it, the whole time I was based on this post and it didn't help I was naive that in my case the method would be the same.

The solution was really simple. If you define a field in your object with type ImColor m_color and than the only thing you need to do later in the code

ImPlot::SetNextLineStyle(object.m_color);
ImPlot::PlotLine(/*arguments needed*/);

I hope I saved someone some time

Upvotes: 3

beyond
beyond

Reputation: 514

You should be able to add your own colormap, first setup your colormap like this (only do it once):

static ImU32 colorDataRGB[3] = { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF };
int customRGBMap = ImPlot::AddColormap("RGBColors", colorDataRGB, 3);

And then

ImPlot::PushColorMap(customRGBMap);
ImPlot::BeginPlot(...);
...
ImPlot::EndPlot();
ImPlot::PopColorMap();

Upvotes: 4

Related Questions