implot icon indicating copy to clipboard operation
implot copied to clipboard

Add PlotShadedClip, fills region only if the second line is higher than the first one.

Open WildRackoon opened this issue 5 years ago • 2 comments

PlotShaded only supports a single color for the overlap region, It could use a segregation based on a quantity being higher than another.

Here is a Simple example, used to visually assert wether a stock market's limit orders are appropriate for transactions to be done: ShadedClip

This is achieved by two PlotShadedClip calls, one from ys1 to ys2 and a second from ys2 to ys1;

//ClipShades
ImPlot::SetNextFillStyle(ImVec4(1.0f,0.0f,0.0f,0.25f));
ImPlot::PlotShadedClip  ("Market Bad" , &history_price_buy.Data[0].x , &history_price_buy.Data[0].y , &history_price_sell.Data[0].y, history_price_buy.Data.size() , history_price_buy.Offset , 2*sizeof(float));
ImPlot::SetNextFillStyle(ImVec4(0.0f,1.0f,0.0f,0.25f));
ImPlot::PlotShadedClip  ("Market Good", &history_price_sell.Data[0].x, &history_price_sell.Data[0].y, &history_price_buy.Data[0].y , history_price_sell.Data.size(), history_price_sell.Offset, 2*sizeof(float));

The Implementation is very basic, it comes down to 2 modified lines in comparison with PlotShaded, upper vertices coords are squeezed down if relative height conditions are not met.

DrawList._IdxWritePtr[0] = (ImDrawIdx)(DrawList._VtxCurrentIdx);
DrawList._IdxWritePtr[1] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 1 + intersect);
DrawList._IdxWritePtr[2] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 0 + 3 * (P11.y >= P12.y) );
DrawList._IdxWritePtr[3] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 1);
DrawList._IdxWritePtr[4] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 3 - intersect);
DrawList._IdxWritePtr[5] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 1 + 3 * (P21.y >= P22.y) );
DrawList._IdxWritePtr += 6;
DrawList._VtxCurrentIdx += 5;

This PR only a baseline for a better implementation of this kind of feature, it also only does ShadeClipping between two series, not with a single serie and a ref, simply because I didn't know how to elegantly handle the choice between a shaded region above or under the reference line. (switching positional arguments doesn't seem very clean to me, but its work).

EDIT: I mean extending this to lines, scatter etc... and have dual/multi color thresholds would need significant changes.

Looking forward to your suggestions.

WildRackoon avatar Dec 31 '20 16:12 WildRackoon

@WildRackoon , thanks for the PR. This looks useful! I'll try to take a look soon (currently swamped with school).

epezent avatar Jan 06 '21 15:01 epezent

Rebased this to latest code changes just in case anyone needs this Added Demo example But still no idea to reduce code duplication in an intuitive manner / handle

WildRackoon avatar Jan 23 '23 18:01 WildRackoon