How to use labels for ticks indefinitely like when no limits are specified in imgui?

72 Views Asked by At

Currently I have a 2d plot which is not zoomable and the x-axis limit moves as the time passes.

if (ImPlot::BeginPlot(plotId.c_str(), ImVec2(-1,-1), ImPlotFlags_NoTitle|ImPlotFlags_NoFrame|ImPlotFlags_NoMenus))
{
ImPlot::SetupLegend(ImPlotLocation_NorthWest, ImPlotLegendFlags_NoButtons);
ImPlot::SetupAxes("Minutes", NULL, ImPlotAxisFlags_None, ImPlotAxisFlags_None);
ImPlot::SetupAxisLimits(ImAxis_X1,mPassedTime - mPlotInfo.X_SECOND, mPassedTime, ImGuiCond_Always);
ImPlot::SetupAxisTicks(ImAxis_X1, mXTimeTicks, mXTimeCount, mXTimeLabels, false);
        //ImPlot::SetupAxisFormat(ImAxis_X1, MetricFormatter, (void*)"min");
float yPadding = abs(mPlotInfo.Y_MAX-mPlotInfo.Y_MIN) * 0.005f;
ImPlot::SetupAxisLimits(ImAxis_Y1, mPlotInfo.Y_MIN-yPadding, mPlotInfo.Y_MAX+yPadding, ImGuiCond_Always);
    }
ImPlot::EndPlot()

Here I specify manually for what label to display at what tick where

static inline const double mXTimeTicks[] = {0,60,120,180,240,300,360,420,480,540,600, 660,720,780,840,900,960,1020,1080,1140,1200, 1260,1320,1380,1440,1500,1560,1620,1680,1740,1800, 1860,1920,1980,2040,2100,2160,2220,2280,2340,2400, 2460,2520,2580,2640,2700,2760,2820,2880,2940,3000, 3060,3120,3180,3240,3300,3360,3420,3480,3540,3600, };
static inline const char* const mXTimeLabels[] = {"0 min","1","2","3","4","5 min","6","7","8","9","10 min", "11","12","13","14","15 min","16","17","18","19","20 min", "21","22","23","24","25 min","26","27","28","29","30 min", "31","32","33","34","35 min","36","37","38","39","40 min", "41","42","43","44","45 min","46","47","48","49","50 min", "51","52","53","54","55 min","56","57","58","59","60 min" };
int mXTimeCount = 31;

I cannot increase it manually, as I want this to go on indefinitely. I want to label every 60 ticks as minutes.

So I tried to use formatter instead of normal one

ImPlot::SetupAxisFormat(ImAxis_X1, MetricFormatter, (void*)"min");

where metricformatter is

int MetricFormatter(double value, char* buff, int size, void* data) {
const char* unit = (const char*)data;
static double v = 60;
if (value < 0) {
    return snprintf(buff, size, "");
}
if (value == 0) {
    return snprintf(buff, size, "0 %s", unit);
}
if (fmod(value, v) == 0) {
    return snprintf(buff, size, "%g %s", value / v, unit);
}
return snprintf(buff, size, "");
}

But while using this I always get the label after every ticks a "0 min" instead of 1 or 2 min.

I can't even use a counter as the SetupAxisTicks only takes const values.

So how to go about this one?

0

There are 0 best solutions below