Files
Rail_Enterprise/client/RailEnterprise.Client/scripts/Panels/SeriesChart.cs
T
Alby96andClaude Opus 5.5 2e5215c570
CI / Build e test (pull_request) Successful in 1m32s
CI / Immagine Docker (pull_request) Successful in 35s
F6: livelli e scalata, correzioni della revisione
La revisione del pannello «La tua scalata» ha trovato numeri che promettevano
più di quanto il motore concede e qualche incoerenza con l'immagine 08.

- Avanzamento: il client mostra le percentuali per difetto (Fmt.Progress),
  così il 100 % compare solo a requisito o livello raggiunto; prima 0,996
  si leggeva «100%» senza promozione. Il tetto del motore resta 0,999.
- Rating: la frazione è 1 − gradini mancanti / ratingProgressSteps (2,5 in
  tiers.json, con schema e validatore). Lo stato dell'immagine 08 torna al
  58 % (rating al 60 %); misurato dalla D dava l'86 % per un solo gradino.
- Concessioni: non compaiono più tra gli sblocchi né nella notizia finché il
  comando non esiste (F7); il rifiuto dice che non sono ancora disponibili.
- Validatore: gli sblocchi non possono diminuire salendo di livello (fido e
  funzioni), come presumono notizie, rifiuti e pannello.
- Industrie possedute inattive (livello 0): a libro valgono solo la cassa.
- Migrazione 1-5: il giorno del livello ricostruito dallo storico è quello
  della fotografia (primo giorno dell'anno dopo), tra fondazione e oggi.
- Storico del patrimonio: un punto per anno di calendario (oggi sostituisce
  il primo giorno dell'anno in corso); i livelli si segnano sull'anno della
  promozione, anche sull'ultimo punto.
- Barra superiore: richiede di nuovo il quadro se uno stato completo lo ha
  azzerato.
- ADR 0018 aggiornato; ADR 0015 §12 segnato come sostituito.
- Test nuovi per avanzamento, storico, migrazione, concessioni, industria
  inattiva e validatore (174 test verdi).

Refs #38.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:31:10 +01:00

204 lines
10 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 e
/// delle merci (con la previsione tratteggiata e la sua banda, immagine 06), 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 = "";
private IReadOnlyList<double> _forecast = [], _low = [], _high = [];
private string _todayLabel = "", _forecastLabel = "";
private IReadOnlyList<(int Index, string Label)> _markers = [];
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;
_forecast = _low = _high = [];
_markers = [];
QueueRedraw();
}
/// <summary>
/// Segni sulla serie a linea (dopo <see cref="Show"/>): un punto con l'etichetta sopra, per
/// esempio i livelli raggiunti nello storico del patrimonio (immagine 08).
/// </summary>
public void SetMarkers(IReadOnlyList<(int Index, string Label)> markers)
{
_markers = markers;
QueueRedraw();
}
/// <summary>
/// Serie a linea seguita dalla previsione (prezzi, banda bassa e alta), un punto per passo
/// dopo l'ultimo valore; una riga verticale segna «oggi».
/// </summary>
public void ShowWithForecast(IReadOnlyList<double> values, IReadOnlyList<string> labels, IReadOnlyList<double> forecast,
IReadOnlyList<double> low, IReadOnlyList<double> high, Func<double, string> format, Color color, string empty,
string todayLabel, string forecastLabel)
{
Show(values, labels, format, Style.Line, color, empty);
bool consistent = forecast.Count == low.Count && forecast.Count == high.Count;
_forecast = consistent ? forecast : [];
_low = consistent ? low : [];
_high = consistent ? high : [];
_todayLabel = todayLabel;
_forecastLabel = forecastLabel;
}
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.Line && _forecast.Count > 0)
{
min = Math.Min(min, _low.Min());
max = Math.Max(max, _high.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;
}
int f = _forecast.Count;
float step = area.Size.X / (n - 1 + f);
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(points[^1].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);
if (f > 0) DrawForecast(area, points[^1], step, Y, font, fontSize);
foreach (var (index, label) in _markers)
{
if (index < 0 || index >= n) continue;
var p = points[index];
bool atEnd = index == n - 1;
DrawCircle(p, 5, Palette.AmberLight);
DrawArc(p, 5, 0, MathF.Tau, 16, Palette.AppBackground, 1.5f, true);
var dims = font.GetStringSize(label, HorizontalAlignment.Left, -1, 12);
// sull'ultimo punto (che ha sopra il suo valore) l'etichetta va a sinistra, alla sua altezza
float mx = atEnd ? Math.Max(area.Position.X, p.X - dims.X - 10) : Math.Clamp(p.X - dims.X / 2, area.Position.X, Size.X - dims.X - 4);
float my = atEnd ? p.Y + 16 : p.Y - 12 < area.Position.Y + 10 ? p.Y + 20 : p.Y - 12;
DrawString(font, new Vector2(mx, my), label, HorizontalAlignment.Left, -1, 12, Palette.AmberLight);
}
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>Previsione: banda chiara, linea tratteggiata e riga verticale di «oggi».</summary>
private void DrawForecast(Rect2 area, Vector2 today, float step, Func<double, float> y, Font font, int fontSize)
{
int f = _forecast.Count;
var band = new List<Vector2> { today };
for (int i = 0; i < f; i++) band.Add(new Vector2(today.X + step * (i + 1), y(_high[i])));
for (int i = f - 1; i >= 0; i--) band.Add(new Vector2(today.X + step * (i + 1), y(_low[i])));
DrawColoredPolygon(band.ToArray(), Palette.WithAlpha(Palette.Workers, 0.22f));
var previous = today;
for (int i = 0; i < f; i++)
{
var next = new Vector2(today.X + step * (i + 1), y(_forecast[i]));
DrawDashedLine(previous, next, Palette.Staff, 2f, 6f);
previous = next;
}
DrawDashedLine(new Vector2(today.X, area.Position.Y), new Vector2(today.X, area.End.Y), Palette.TextSecondary, 1f, 4f);
if (_todayLabel.Length > 0)
{
// in cima alla riga verticale: in basso si sovrapporrebbe alle etichette dei mesi
var dims = font.GetStringSize(_todayLabel, HorizontalAlignment.Left, -1, fontSize);
float x = Math.Min(today.X + 4, Size.X - dims.X - 4);
DrawString(font, new Vector2(x, area.Position.Y + fontSize), _todayLabel, HorizontalAlignment.Left, -1, fontSize, Palette.Text);
}
if (_forecastLabel.Length > 0)
{
var dims = font.GetStringSize(_forecastLabel, HorizontalAlignment.Left, -1, fontSize);
DrawString(font, new Vector2(Math.Min(area.End.X - dims.X / 2, Size.X - dims.X - 4), area.End.Y + 16), _forecastLabel,
HorizontalAlignment.Left, -1, fontSize, Palette.Staff);
}
}
/// <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);
}
}
}