epezent / implot

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

BeginItem does not create a legend #467

Closed eladm-ultrawis closed 11 months ago

eladm-ultrawis commented 1 year ago

I am using ImPlot custom rendering quite a lot. when using the built-in plot functions (such as PlotLine, PlotImage) I get a legend with the item label.

However, when using BeginItem / EndItem I never get a legend.

Here is an example of a custom function that draws a dashed line:

    void PlotDashedLine(const std::array<Eigen::Vector3f, 2>& endpoints, const char* label, ImU32 color, float thickness = 3.0f, float dashPixelLength = 2.0f, float spacePixelLength = 1.0f)
    {
        auto p1 = PlotToPixels(endpoints[0]);
        auto p2 = PlotToPixels(endpoints[1]);
        Eigen::Vector2f line = (p2 - p1);
        auto length = line.norm();
        Eigen::Vector2f dir = line / length;

        auto pairLength = dashPixelLength + spacePixelLength;

        auto dashCount = std::ceil(length / pairLength);

        auto drawList = ImPlot::GetPlotDrawList();

        if (ImPlot::BeginItem("something", ImPlotCol_Fill)) // putting `legend` parameter does not work either.
        {
            auto dashStart = p1;

            for (auto i = 0u; i < static_cast<uint32_t>(dashCount); ++i)
            {
                Eigen::Vector2f dashEnd = dashStart + dir * dashPixelLength;

                auto d = (dashEnd - p1).norm();

                if (d > length)
                {
                    dashEnd = p2;
                }

                drawList->AddLine(ToImVec(dashStart), ToImVec(dashEnd), color, thickness);
                dashStart += dir * pairLength;
            }

            ImPlot::EndItem();
        }
    }

I never get a legend no matter what I put in BeginItem first parameter. Is this a bug? If not what am I doing wrong?

epezent commented 11 months ago

Hi -- your issue is that the signature of BeginItem changed. The second parameter is now ImPlotItemFlags. ImPlotCol_Fill needs to be passed as the third parameter if you intend to use it.

IMPLOT_API bool BeginItem(const char* label_id, ImPlotItemFlags flags=0, ImPlotCol recolor_from=IMPLOT_AUTO);

Sorry about this, but we don't make any guarantees about implot_internal.h so beware when using these functions!