Files
Rail_Enterprise/client/RailEnterprise.Client/scripts/Panels/SeriesChart.cs
T
Alby96andClaude Opus 5.5 c1a0049bcf
CI / Build e test (pull_request) Successful in 1m20s
CI / Immagine Docker (pull_request) Successful in 37s
Infrastruttura: nome «Rail Enterprise», CI e rilasci su Gitea, documentazione
Il progetto adotta il nome «Rail Enterprise» ovunque (codice RailEnterprise.*,
variabili RE_*, intestazioni RESV/RECL, immagine rail-enterprise-server) per
allinearsi al repository Gitea. Aggiunge Gitea Actions sul modello di Chronos:
CI su PR e main, rilascio da tag con gioco Windows (client Godot + server in
server/), server Windows/Linux e immagine Docker nel registro Gitea, nightly.
Aggiunge i riferimenti visivi 06–09, i modelli di PR e issue, l'ADR 0014 e
aggiorna README (comandi locali), STATE e CLAUDE.md. 120 test verdi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 11:14:54 +01:00

120 lines
5.9 KiB
C#

using Godot;
using RailEnterprise.Client.App;
using static RailEnterprise.Client.App.Localization;
namespace RailEnterprise.Client.Panels;
/// <summary>
/// Grafico di una serie (come ProfileChart): linea con area per i prezzi mensili delle azioni,
/// barre per il patrimonio netto anno per anno della Cronaca. Disegna solo i valori ricevuti.
/// </summary>
public partial class SeriesChart : Control
{
public enum Style { Line, Bars }
private IReadOnlyList<double> _values = [];
private IReadOnlyList<string> _labels = [];
private Func<double, string> _format = v => Fmt.Number(v, 0);
private Style _style;
private Color _color = Palette.Amber;
private string _empty = "";
public SeriesChart(float height = 150)
{
CustomMinimumSize = new Vector2(0, height);
MouseFilter = MouseFilterEnum.Ignore;
}
/// <summary>Imposta la serie: valori, etichette dell'asse orizzontale (facoltative) e formato dei valori.</summary>
public void Show(IReadOnlyList<double> values, IReadOnlyList<string> labels, Func<double, string> format, Style style, Color color, string empty)
{
_values = values;
_labels = labels;
_format = format;
_style = style;
_color = color;
_empty = empty;
QueueRedraw();
}
public override void _Draw()
{
DrawStyleBox(UiTheme.Box(Palette.Inner, Palette.Border, 8, 0), new Rect2(Vector2.Zero, Size));
var font = UiTheme.Body;
const int fontSize = 11;
if (_values.Count == 0 || (_style == Style.Line && _values.Count < 2))
{
string text = _empty.Length > 0 ? _empty : T("Dati insufficienti");
var dims = font.GetStringSize(text, HorizontalAlignment.Left, -1, 13);
DrawString(font, new Vector2((Size.X - dims.X) / 2, Size.Y / 2 + 4), text, HorizontalAlignment.Left, -1, 13, Palette.TextSecondary);
return;
}
double min = _values.Min(), max = _values.Max();
if (_style == Style.Bars) min = Math.Min(0, min);
if (max - min < 1e-9)
{
max += Math.Max(1, Math.Abs(max) * 0.1);
if (_style == Style.Line) min -= Math.Max(1, Math.Abs(min) * 0.1);
}
string maxText = _format(max), minText = _format(min);
float left = 12 + Math.Max(font.GetStringSize(maxText, HorizontalAlignment.Left, -1, fontSize).X, font.GetStringSize(minText, HorizontalAlignment.Left, -1, fontSize).X) + 8;
bool hasLabels = _labels.Count == _values.Count;
var area = new Rect2(left, 12, Size.X - left - 14, Size.Y - (hasLabels ? 34 : 22));
float Y(double v) => area.End.Y - (float)((v - min) / (max - min)) * area.Size.Y;
// griglia: massimo, metà, minimo
foreach (double v in new[] { min, (min + max) / 2, max })
DrawLine(new Vector2(area.Position.X, Y(v)), new Vector2(area.End.X, Y(v)), Palette.WithAlpha(Palette.Border, 0.9f), 1);
DrawString(font, new Vector2(10, Y(max) + 4), maxText, HorizontalAlignment.Left, -1, fontSize, Palette.TextSecondary);
DrawString(font, new Vector2(10, Y(min) + 4), minText, HorizontalAlignment.Left, -1, fontSize, Palette.TextSecondary);
int n = _values.Count;
if (_style == Style.Bars)
{
float slot = area.Size.X / n;
float width = Math.Max(2, Math.Min(42, slot * 0.7f));
for (int i = 0; i < n; i++)
{
float x = area.Position.X + slot * (i + 0.5f);
float y0 = Y(Math.Max(min, 0)), y1 = Y(_values[i]);
var color = _values[i] < 0 ? Palette.Danger : i == n - 1 ? Palette.AmberLight : _color;
DrawRect(new Rect2(x - width / 2, Math.Min(y0, y1), width, Math.Max(1, Math.Abs(y1 - y0))), color);
}
DrawLabels(area, n, slot, font, fontSize, centered: true);
return;
}
float step = area.Size.X / (n - 1);
var points = new Vector2[n];
for (int i = 0; i < n; i++) points[i] = new Vector2(area.Position.X + step * i, Y(_values[i]));
var fill = new List<Vector2>(points) { new(area.End.X, area.End.Y), new(area.Position.X, area.End.Y) };
DrawColoredPolygon(fill.ToArray(), Palette.WithAlpha(_color, 0.18f));
DrawPolyline(points, _color, 2.5f, true);
DrawCircle(points[^1], 4, Palette.AmberLight);
string last = _format(_values[^1]);
var lastDims = font.GetStringSize(last, HorizontalAlignment.Left, -1, 12);
float lx = Math.Min(points[^1].X - lastDims.X / 2, Size.X - lastDims.X - 6);
float ly = points[^1].Y - 8 < area.Position.Y + 10 ? points[^1].Y + 18 : points[^1].Y - 8;
DrawString(UiTheme.BodyBold, new Vector2(lx, ly), last, HorizontalAlignment.Left, -1, 12, Palette.AmberLight);
DrawLabels(area, n, step, font, fontSize, centered: false);
}
/// <summary>Etichette dell'asse orizzontale, diradate per non sovrapporsi.</summary>
private void DrawLabels(Rect2 area, int n, float step, Font font, int fontSize, bool centered)
{
if (_labels.Count != n) return;
float widest = _labels.Max(l => font.GetStringSize(l, HorizontalAlignment.Left, -1, fontSize).X) + 10;
int every = Math.Max(1, (int)Math.Ceiling(widest / Math.Max(1, step)));
for (int i = 0; i < n; i++)
{
// l'ultima etichetta si aggiunge solo se non tocca la precedente
bool show = i % every == 0 || (i == n - 1 && (n - 1) % every * step >= widest);
if (!show) continue;
var dims = font.GetStringSize(_labels[i], HorizontalAlignment.Left, -1, fontSize);
float x = area.Position.X + step * (centered ? i + 0.5f : i) - dims.X / 2;
x = Math.Clamp(x, area.Position.X - 4, Size.X - dims.X - 4);
DrawString(font, new Vector2(x, area.End.Y + 16), _labels[i], HorizontalAlignment.Left, -1, fontSize, Palette.TextSecondary);
}
}
}