Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom labels with lambda #173

Closed
kv-gits opened this issue Jan 29, 2021 · 4 comments
Closed

Custom labels with lambda #173

kv-gits opened this issue Jan 29, 2021 · 4 comments
Labels
enhancement New feature or request
Milestone

Comments

@kv-gits
Copy link

kv-gits commented Jan 29, 2021

Is it possible to set custom label not only with predefined array, but with c++ lambda?

@epezent
Copy link
Owner

epezent commented Jan 29, 2021

Not currently. But that's not a bad idea. We could easily support it.

@epezent epezent added this to the v1.0 milestone Jan 29, 2021
@epezent epezent added the enhancement New feature or request label Jan 29, 2021
@epezent
Copy link
Owner

epezent commented Mar 25, 2021

@kv-gits, I've add the ability to set custom tick format specifiers in #198. Does something like this support your use case, or would you still require a callback? If so, can you describe what your ideal callback signature would be, i.e. what information would you require to generate the ticks? (e.g. plot range, pixel size, etc).

@kv-gits
Copy link
Author

kv-gits commented Apr 13, 2021

@epezent, sorry, I missed this issue. I think callback should be as it is already implemented for Y values

auto lamda = [](void* data, int idx) { ... return "const char array"; };

So we could consider state of graphic e.g. scale and prepare appropriate format inside lambda.

@epezent
Copy link
Owner

epezent commented Oct 20, 2021

#294 (see also #168 (comment)) adds:

// Sets the format of numeric axis labels via formatter callback. Given #value, write a label into #buff. Optionally pass user data.
void SetupAxisFormat(ImAxis axis, ImPlotFormatter formatter, void* data = NULL);

where:

// Callback signature for axis tick label formatter.
typedef void (*ImPlotFormatter)(double value, char* buff, int size, void* user_data);

example:

void MetricFormatter(double value, char* buff, int size, void* data) {
    const char* unit = (const char*)data;
    static double v[]      = {1000000000,1000000,1000,1,0.001,0.000001,0.000000001};
    static const char* p[] = {"G","M","k","","m","u","n"};
    if (value == 0) {
        snprintf(buff,size,"0 %s", unit);
        return;
    }
    for (int i = 0; i < 7; ++i) {
        if (fabs(value) >= v[i]) {
            snprintf(buff,size,"%g %s%s",value/v[i],p[i],unit);
            return;
        }
    }
    snprintf(buff,size,"%g %s%s",value/v[6],p[6],unit);
}
if (ImPlot::BeginPlot("MyPlot")) {
    ImPlot::SetupAxisFormat(ImAxis_Y1, MetricFormatter, (void*)"Hz");
    ImPlot::EndPlot();
}

img

@epezent epezent closed this as completed Oct 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants