epezent / implot

Immediate Mode Plotting
MIT License
4.55k stars 503 forks source link

Allow custom ID for a subplot #516

Open remss opened 10 months ago

remss commented 10 months ago

Hello Implot-Team! Currently each plot of a subplot will have a PushID with its index. There should be a way to set a custom value for this. In my case I have options to show/hide/reorder subplot items and therefore the same plotted metric ends with a different ID path depending of the user choice. When the user changes the layout then the plot state is lost because its ID path has changed.

remss commented 10 months ago

Here is a sample repro case where the state is lost. I create a subplots of 2 plots P1 and P2, I zoom/pan P2 then I remove P1 to display only P2. Then you see the P2 has lost its state and the zoom/pan is reset.

QTWXBA8tBV

int cols = 0;
static auto sinLamda = [](int idx, void *f) { return ImPlotPoint(idx, sinf(idx * (*(float *)f))); };
static bool showP1 = true;
ImGui::Checkbox("Show P1", &showP1);
if (showP1)
{
    cols++;
}
static bool showP2 = true;
ImGui::Checkbox("Show P2", &showP2);
if (showP2)
{
    cols++;
}
ImPlotSubplotFlags flags = 0;
if (ImPlot::BeginSubplots("Test Subplots", 1, cols, ImVec2(0, 0), flags))
{
    if (showP1)
    {
        if (ImPlot::BeginPlot("p1"))
        {
            static float f1 = 0.3f;
            ImPlot::PlotLineG("v", sinLamda, &f1, 100);
            ImPlot::EndPlot();
        }
    }
    if (showP2)
    {
        if (ImPlot::BeginPlot("p2"))
        {
            static float f2 = 0.1f;
            ImPlot::PlotLineG("v", sinLamda, &f2, 100);
            ImPlot::EndPlot();
        }
    }
    ImPlot::EndSubplots();
}
epezent commented 10 months ago

Hi! I think we can support this by adding a new subplot flag, e.g. ImPlotSubplotFlags_NoAutoID, which would disable the internal ID push/pop and place the responsibility on the user. Thoughts?

remss commented 10 months ago

It's ok for me thanks!