Files
fn_registry/cpp/functions/viz/scatter_plot.cpp
T
egutierrez 3f622561ce feat(cpp/viz): static-plot primitive + tooltips + rotated labels + card compacta
Nuevo primitivo compartido:
- cpp/functions/viz/plot_static.h: header-only con flags ImPlotFlags /
  ImPlotAxisFlags agrupados (NoFrame|NoMenus|NoBoxSelect|NoMouseText +
  Lock|NoInitialFit|NoHighlight) para visualizacion estatica en
  dashboards. Lo usan todos los charts de viz/.

Charts refactorizados a v1.1 con parametro `height` explicito (rompe el
feedback loop con contenedores AutoResizeY que producia vibracion al
redimensionar) y ejes pineados con ImPlotCond_Always:
- bar_chart v1.2: tooltip al hover (label + valor) + auto-rotacion de
  labels a 45 cuando no caben horizontalmente (medidos con CalcTextSize
  vs ancho del plot). Los labels rotados se dibujan manualmente con
  ImDrawList::PrimQuadUV + ImFontBaked::FindGlyph (API ImGui 1.92+).
- pie_chart v1.1: tooltip por slice (detecta cual via atan2 desde centro
  en sentido CCW matematico, que es como ImPlot dibuja los slices desde
  angle0=90) con label + valor + porcentaje. Aspect 1:1 mantenido.
- line_plot, scatter_plot, histogram v1.1: ejes pineados con limites
  calculados de min/max + 5% headroom (histogram usa AutoFit por los
  bins dinamicos, con Lock para bloquear pan/zoom).

kpi_card v1.2: card mas compacta — altura 78px (antes 108), font scale
1.4x (antes 1.8x), padding sm (antes md). Apto para densidades altas
de KPIs en dashboards.

fullscreen_window v0.2: NoScrollbar|NoScrollWithMouse para eliminar el
scrollbar fugaz que aparecia cuando el contenido excedia por 1-2px la
ventana, reflow de ancho y vibracion visible al redimensionar.

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

50 lines
1.8 KiB
C++

#include "viz/scatter_plot.h"
#include "viz/plot_static.h"
#include "implot.h"
namespace {
template <typename T>
void draw_scatter(const char* title, const T* xs, const T* ys, int count, float height) {
if (count <= 0) return;
T x_min = xs[0], x_max = xs[0];
T y_min = ys[0], y_max = ys[0];
for (int i = 1; i < count; i++) {
if (xs[i] < x_min) x_min = xs[i];
if (xs[i] > x_max) x_max = xs[i];
if (ys[i] < y_min) y_min = ys[i];
if (ys[i] > y_max) y_max = ys[i];
}
double dx = static_cast<double>(x_max) - static_cast<double>(x_min);
double dy = static_cast<double>(y_max) - static_cast<double>(y_min);
if (dx < 1e-9) dx = 1.0;
if (dy < 1e-9) dy = 1.0;
const ImVec2 plot_size(-1.0f, height > 0.0f ? height : 200.0f);
if (ImPlot::BeginPlot(title, plot_size, plot_static::kPlotFlags)) {
ImPlot::SetupAxes(nullptr, nullptr, plot_static::kAxisFlags, plot_static::kAxisFlags);
ImPlot::SetupAxisLimits(ImAxis_X1,
static_cast<double>(x_min) - dx * 0.05,
static_cast<double>(x_max) + dx * 0.05,
ImPlotCond_Always);
ImPlot::SetupAxisLimits(ImAxis_Y1,
static_cast<double>(y_min) - dy * 0.05,
static_cast<double>(y_max) + dy * 0.05,
ImPlotCond_Always);
ImPlot::PlotScatter("##data", xs, ys, count);
ImPlot::EndPlot();
}
}
} // namespace
void scatter_plot(const char* title, const float* xs, const float* ys, int count, float height) {
draw_scatter<float>(title, xs, ys, count, height);
}
void scatter_plot(const char* title, const double* xs, const double* ys, int count, float height) {
draw_scatter<double>(title, xs, ys, count, height);
}