- «Dove conviene vendere» dice che il prezzo locale è quello della compravendita in proprio: oggi le consegne del vettore si pagano al prezzo globale (ADR 0015 §9). - Gli hub che hanno esaurito la capacità del mese non compaiono tra le destinazioni. - Senza compagnia l'avanzamento verso il commercio mostra comunque i requisiti del livello. - I quattro test lunghi del mercato sono marcati lenti. - «oggi» va in cima alla riga del grafico, per non coprire le etichette dei mesi. - L'ADR annota la piccola differenza tra offerta e domanda mostrate e quelle del passo di mercato. Refs #29. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
9.0 KiB
C#
179 lines
9.0 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 = "";
|
|
|
|
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 = [];
|
|
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);
|
|
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);
|
|
}
|
|
}
|
|
}
|