epezent / implot

Immediate Mode Plotting
MIT License
4.71k stars 521 forks source link

PlotStems index problem? #550

Open tasiek30 opened 9 months ago

tasiek30 commented 9 months ago

Hello

I have probelm with ImPlot::PlotStems. i'm trying to draw Real Time plot but some of line segments draw only at x=1 position.... When i change to PlotLine type everything is working fine. Also I found that changing ScrollingBuffer implementation to have two independent vectors for x and y fix the problem, but i was wondering why PlotLines was working properly....

Zrzut ekranu 2024-01-08 083953 Zrzut ekranu 2024-01-08 084056

struct ScrollingBuffer {
    int MaxSize;
    int Offset;
    ImVector<ImVec2> Data;

    ScrollingBuffer(int max_size = 2000) {
        MaxSize = max_size;
        Offset  = 0;
        Data.reserve(MaxSize);
    }
    void AddPoint(float x, float y) {
        if (Data.size() < MaxSize)
            Data.push_back(ImVec2(x,y));
        else {
            Data[Offset] = ImVec2(x,y);
            Offset =  (Offset + 1) % MaxSize;
        }
    }
    void Erase() {
        if (Data.size() > 0) {
            Data.shrink(0);
            Offset  = 0;
        }
    }
};

ImPlotFlags plotFlags = ImPlotFlags_NoTitle| ImPlotFlags_NoLegend;
ImPlotAxisFlags axisFlags = ImPlotAxisFlags_None;//ImPlotAxisFlags_NoTickLabels;

static bool m_plotEnable;
ImGui::Checkbox("Plot Enable", &m_plotEnable);

static ScrollingBuffer data1;

static float t = 0;
static float delta = 0;
delta += ImGui::GetIO().DeltaTime;

if(delta > 0.25f) {
    delta = 0.0;
    if(m_plotEnable) {
        data1.AddPoint(t, 1.0);
        t += 1;
    }
}

static float history = 10.0f;
ImGui::SliderFloat("History",&history,0.1,30,"%.1f s");

if (ImPlot::BeginPlot("Stem Plots", ImVec2(-1,0), plotFlags) ) {
    ImPlot::SetupAxes(nullptr, nullptr, axisFlags, axisFlags);

    ImPlot::SetupAxisLimits(ImAxis_X1, t-history, t, ImGuiCond_Always);
    ImPlot::SetupAxisLimits(ImAxis_Y1,-1,1.5);
    if( data1.Data.size() > 0) {
        ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
        ImPlot::PlotStems("Plot 1",&data1.Data[0].x, &data1.Data[0].y, data1.Data.size(), 0, data1.Offset, 2*sizeof(float));
    }
    ImPlot::EndPlot();
}

Compiler: gcc 12.2.0 ImPlot: 0.17