Files
fn_registry/cpp/functions/viz/bar_chart.cpp
T
egutierrez 9b1ca41c4d feat(shaders_lab): add gl_loader + Windows cross-compile
- cpp/functions/gfx/gl_loader.{h,cpp,md}: mini loader para OpenGL 2.0+
  (Linux no-op via GL_GLEXT_PROTOTYPES, Windows wglGetProcAddress)
- Portar gl_shader/gl_framebuffer/fullscreen_quad/shader_canvas al loader
- CMakeLists: WIN32_EXECUTABLE para lanzar sin consola en Windows
- apps/shaders_lab/shaders_lab.exe: binario PE32+ precompilado

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 20:52:37 +02:00

56 lines
2.1 KiB
C++

#include "viz/bar_chart.h"
#include "implot.h"
#include <vector>
namespace {
// Plot bars con ejes pineados (no se mueven entre frames) y labels categoricos.
// ImPlot por defecto auto-fitea Y en cada frame, lo que provoca oscilacion visual.
// Lo resolvemos calculando y_max una vez y forzandolo con ImPlotCond_Always.
template <typename T>
void draw_bars(const char* title, const char* const* labels, const T* values,
int count, double bar_width) {
if (count <= 0) return;
double y_max = 0.0;
for (int i = 0; i < count; i++) {
if (static_cast<double>(values[i]) > y_max) y_max = static_cast<double>(values[i]);
}
if (y_max <= 0.0) y_max = 1.0;
y_max *= 1.15; // 15% headroom sobre la barra mas alta
const ImPlotFlags plot_flags =
ImPlotFlags_NoMenus | ImPlotFlags_NoBoxSelect | ImPlotFlags_NoMouseText;
const ImPlotAxisFlags x_flags =
ImPlotAxisFlags_NoMenus | ImPlotAxisFlags_Lock | ImPlotAxisFlags_NoGridLines;
const ImPlotAxisFlags y_flags =
ImPlotAxisFlags_NoMenus | ImPlotAxisFlags_Lock;
if (ImPlot::BeginPlot(title, ImVec2(-1, 0), plot_flags)) {
std::vector<double> positions(count);
for (int i = 0; i < count; i++) positions[i] = i;
ImPlot::SetupAxes(nullptr, nullptr, x_flags, y_flags);
ImPlot::SetupAxisLimits(ImAxis_X1, -0.5, static_cast<double>(count) - 0.5,
ImPlotCond_Always);
ImPlot::SetupAxisLimits(ImAxis_Y1, 0.0, y_max, ImPlotCond_Always);
ImPlot::SetupAxisTicks(ImAxis_X1, positions.data(), count, labels);
ImPlot::PlotBars("##data", values, count, bar_width);
ImPlot::EndPlot();
}
}
} // namespace
void bar_chart(const char* title, const char* const* labels, const float* values,
int count, float bar_width) {
draw_bars<float>(title, labels, values, count, static_cast<double>(bar_width));
}
void bar_chart(const char* title, const char* const* labels, const double* values,
int count, double bar_width) {
draw_bars<double>(title, labels, values, count, bar_width);
}