using Godot; using RailEnterprise.Client.App; using static RailEnterprise.Client.App.Localization; namespace RailEnterprise.Client.Panels; /// /// 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. /// public partial class SeriesChart : Control { public enum Style { Line, Bars } private IReadOnlyList _values = []; private IReadOnlyList _labels = []; private Func _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; } /// Imposta la serie: valori, etichette dell'asse orizzontale (facoltative) e formato dei valori. public void Show(IReadOnlyList values, IReadOnlyList labels, Func 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(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); } /// Etichette dell'asse orizzontale, diradate per non sovrapporsi. 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); } } }