epezent / implot

Immediate Mode Plotting
MIT License
4.65k stars 517 forks source link

Min ticks in SetupAxisTicks #391

Closed rokups closed 2 years ago

rokups commented 2 years ago

Hello, I wanted to ask, why does SetupAxisTicks() assert for n_ticks > 1? Seems to me plots with a single tick are perfectly valid. At the moment workarounds have to be put in place for a special case when only one item is plotted on the axis.

epezent commented 2 years ago

Good question. I know of a least one reason it might be there. There is a function AxisPrecision in implot.cpp which makes an assumption about two ticks being available. It takes the difference between the two tick locations to compute a precision value that is ultimately used to round mouse position text. The function seems to have a fallback if 1 or fewer ticks are present, so I think it should be ok to remove the assert. It would need to be tested though, as there may be other reasons which I have forgotten!

epezent commented 2 years ago

@rokups -- I overlooked the issue. There are two variants of SetupAxisTicks. One which takes an array of values, and another which takes a min/max range and count and generates a uniformly spaced array of ticks for the user (min/max inclusive). Only the latter asserts n_ticks > 1, which makes sense because the min and max value need to be included (as an aside, this function should really just clamp n_ticks between [2,n_ticks] instead of assert).

If you want to add just one tick, you should use the first variant:

if (ImPlot::BeginPlot("My Plot")) {
    double tick = 0.5;
    ImPlot::SetupAxisTicks(ImAxis_X1, &tick, 1);
    ImPlot::EndPlot();
}