using System; using System.Collections.Generic; using System.Globalization; using Spectre.Console.Rendering; namespace Spectre.Console { /// /// A renderable breakdown chart. /// public sealed class BreakdownChart : Renderable, IHasCulture { /// /// Gets the breakdown chart data. /// public List Data { get; } /// /// Gets or sets the width of the breakdown chart. /// public int? Width { get; set; } /// /// Gets or sets a value indicating whether or not /// to show values as percentages or not. /// public bool ShowAsPercentages { get; set; } /// /// Gets or sets a value indicating whether or not to show tags. /// public bool ShowTags { get; set; } = true; /// /// Gets or sets a value indicating whether or not to show tag values. /// public bool ShowTagValues { get; set; } = true; /// /// Gets or sets a value indicating whether or not the /// chart and tags should be rendered in compact mode. /// public bool Compact { get; set; } = true; /// /// Gets or sets the to use /// when rendering values. /// /// Defaults to invariant culture. public CultureInfo? Culture { get; set; } /// /// Initializes a new instance of the class. /// public BreakdownChart() { Data = new List(); Culture = CultureInfo.InvariantCulture; } /// protected override Measurement Measure(RenderContext context, int maxWidth) { var width = Math.Min(Width ?? maxWidth, maxWidth); return new Measurement(width, width); } /// protected override IEnumerable Render(RenderContext context, int maxWidth) { var width = Math.Min(Width ?? maxWidth, maxWidth); var grid = new Grid().Width(width); grid.AddColumn(new GridColumn().NoWrap()); // Bar grid.AddRow(new BreakdownBar(Data) { Width = width, }); if (ShowTags) { if (!Compact) { grid.AddEmptyRow(); } // Tags grid.AddRow(new BreakdownTags(Data) { Width = width, Culture = Culture, ShowPercentages = ShowAsPercentages, ShowTagValues = ShowTagValues, }); } return ((IRenderable)grid).Render(context, width); } } }