3f622561ce
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>
98 lines
3.5 KiB
C++
98 lines
3.5 KiB
C++
#include "viz/pie_chart.h"
|
|
#include "viz/plot_static.h"
|
|
#include "imgui.h"
|
|
#include "implot.h"
|
|
|
|
#include <cmath>
|
|
|
|
namespace {
|
|
|
|
// Localiza que slice del pie esta bajo el cursor.
|
|
// Devuelve -1 si el cursor esta fuera del radio del pie.
|
|
//
|
|
// ImPlot dibuja slices en sentido ANTIHORARIO (matematico) desde angle0=90
|
|
// grados (arriba). atan2(dy, dx) en ejes Y-up devuelve radianes CCW desde +x,
|
|
// asi que en coords de datos: +x=0, +y=90 (arriba), -x=180, -y=-90 (abajo).
|
|
// El offset desde "arriba" en sentido CCW es angle_deg - 90, normalizado.
|
|
template <typename T>
|
|
int slice_at(const T* values, int count, double total, double mouse_x,
|
|
double mouse_y, double cx, double cy, double radius) {
|
|
double dx = mouse_x - cx;
|
|
double dy = mouse_y - cy;
|
|
double r = std::sqrt(dx * dx + dy * dy);
|
|
if (r > radius) return -1;
|
|
|
|
constexpr double kPI = 3.14159265358979323846;
|
|
double angle_deg = std::atan2(dy, dx) * 180.0 / kPI;
|
|
double offset = angle_deg - 90.0; // desde arriba, sentido CCW
|
|
while (offset < 0.0) offset += 360.0;
|
|
while (offset >= 360.0) offset -= 360.0;
|
|
|
|
double acc = 0.0;
|
|
for (int i = 0; i < count; i++) {
|
|
double sweep = (static_cast<double>(values[i]) / total) * 360.0;
|
|
if (offset >= acc && offset < acc + sweep) return i;
|
|
acc += sweep;
|
|
}
|
|
return count - 1;
|
|
}
|
|
|
|
template <typename T>
|
|
void draw_pie(const char* title, const char* const* labels, const T* values,
|
|
int count, double radius, float height) {
|
|
if (count <= 0) return;
|
|
|
|
double total = 0.0;
|
|
for (int i = 0; i < count; i++) total += static_cast<double>(values[i]);
|
|
if (total <= 0.0) return;
|
|
|
|
double outer;
|
|
if (radius < 0.0) outer = -radius;
|
|
else outer = (radius > 0.0) ? radius : 0.4;
|
|
|
|
const ImVec2 plot_size(-1.0f, height > 0.0f ? height : 200.0f);
|
|
|
|
const ImPlotFlags plot_flags = plot_static::kPlotFlags
|
|
| ImPlotFlags_Equal
|
|
| ImPlotFlags_NoLegend;
|
|
|
|
if (ImPlot::BeginPlot(title, plot_size, plot_flags)) {
|
|
ImPlot::SetupAxes(nullptr, nullptr,
|
|
plot_static::kAxisFlagsHidden,
|
|
plot_static::kAxisFlagsHidden);
|
|
ImPlot::SetupAxisLimits(ImAxis_X1, 0.0, 1.0, ImPlotCond_Always);
|
|
ImPlot::SetupAxisLimits(ImAxis_Y1, 0.0, 1.0, ImPlotCond_Always);
|
|
|
|
ImPlot::PlotPieChart(labels, values, count, 0.5, 0.5, outer, "%.0f", 90.0);
|
|
|
|
if (ImPlot::IsPlotHovered()) {
|
|
ImPlotPoint mp = ImPlot::GetPlotMousePos();
|
|
int idx = slice_at<T>(values, count, total, mp.x, mp.y,
|
|
0.5, 0.5, outer);
|
|
if (idx >= 0) {
|
|
double v = static_cast<double>(values[idx]);
|
|
double pct = 100.0 * v / total;
|
|
ImGui::BeginTooltip();
|
|
ImGui::TextUnformatted(labels[idx]);
|
|
ImGui::Separator();
|
|
ImGui::Text("%.0f (%.1f%%)", v, pct);
|
|
ImGui::EndTooltip();
|
|
}
|
|
}
|
|
|
|
ImPlot::EndPlot();
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void pie_chart(const char* title, const char* const* labels, const float* values,
|
|
int count, float radius, float height) {
|
|
draw_pie<float>(title, labels, values, count, static_cast<double>(radius), height);
|
|
}
|
|
|
|
void pie_chart(const char* title, const char* const* labels, const double* values,
|
|
int count, double radius, float height) {
|
|
draw_pie<double>(title, labels, values, count, radius, height);
|
|
}
|