From 7de1a596ddecfddae2e0b62ea52ded1c58fb13f8 Mon Sep 17 00:00:00 2001 From: Aptivi Date: Mon, 13 Jan 2025 13:46:43 +0300 Subject: [PATCH] imp - Improved bar chart appearance --- We've improved the bar chart appearance to make it more beautiful. --- Type: imp Breaking: False Doc Required: False Backport Required: False Part: 1/1 --- .../Writer/CyclicWriters/BarChart.cs | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/public/Terminaux/Writer/CyclicWriters/BarChart.cs b/public/Terminaux/Writer/CyclicWriters/BarChart.cs index ea2d962b3..c686fb7fd 100644 --- a/public/Terminaux/Writer/CyclicWriters/BarChart.cs +++ b/public/Terminaux/Writer/CyclicWriters/BarChart.cs @@ -21,6 +21,7 @@ using System.Text; using Terminaux.Base.Extensions; using Terminaux.Colors; +using Terminaux.Colors.Data; using Terminaux.Writer.ConsoleWriters; using Terminaux.Writer.CyclicWriters.Renderer.Tools; @@ -107,13 +108,12 @@ internal static string RenderBarChart(ChartElement[] elements, int InteriorWidth { // Some variables int maxNameLength = InteriorWidth / 4; - int nameLength = elements.Max((element) => ConsoleChar.EstimateCellWidth(element.Name)); + var shownElements = elements.Where((ce) => !ce.Hidden).ToArray(); + double maxValue = shownElements.Max((element) => element.Value); + int nameLength = shownElements.Max((element) => " ■ ".Length + ConsoleChar.EstimateCellWidth(element.Name) + $" {element.Value}".Length); nameLength = nameLength > maxNameLength ? maxNameLength : nameLength; int showcaseLength = showcase ? nameLength + 3 : 0; - int wholeLength = InteriorWidth - showcaseLength; - int maxNumLength = elements.Max((element) => $" {element.Value}".Length); - int chartLength = wholeLength - maxNumLength; - double maxValue = elements.Max((element) => element.Value); + int wholeLength = InteriorWidth - showcaseLength - (showcase ? 3 : 0); // Fill the bar chart with the elements first StringBuilder barChart = new(); @@ -129,25 +129,27 @@ internal static string RenderBarChart(ChartElement[] elements, int InteriorWidth // Render the showcase if (showcase) { - int nameWidth = ConsoleChar.EstimateCellWidth(name); - int spaces = nameLength - nameWidth; + int nameWidth = ConsoleChar.EstimateCellWidth(element.Name.Truncate(nameLength - 4 - $"{maxValue}".Length)); + int spaces = showcaseLength - (" ■ ".Length + nameWidth + 2 + $"{element.Value}".Length); + spaces = spaces < 0 ? 0 : spaces; barChart.Append( - (useColor ? ColorTools.RenderSetConsoleColor(color) : "") + - name.Truncate(nameLength) + + (useColor ? ColorTools.RenderSetConsoleColor(element.Color) : "") + + " ■ " + + (useColor ? ColorTools.RenderSetConsoleColor(ConsoleColors.Grey) : "") + + element.Name.Truncate(nameLength - 4 - $"{maxValue}".Length) + " " + + (useColor ? ColorTools.RenderSetConsoleColor(ConsoleColors.Silver) : "") + + element.Value + new string(' ', spaces) + - (useColor ? ColorTools.RenderResetForeground() : "") + " ┃ " ); } // Render the element and its value - int length = (int)(value * chartLength / maxValue); + int length = (int)(value * wholeLength / maxValue); barChart.AppendLine( (useColor ? ColorTools.RenderSetConsoleColor(color, true) : "") + new string(' ', length) + - (useColor ? ColorTools.RenderSetConsoleColor(color) : "") + (useColor ? ColorTools.RenderResetBackground() : "") + - $" {value}" + (useColor ? ColorTools.RenderResetForeground() : "") ); }