namespace Spectre.Console; /// /// Contains extension methods for . /// public static class BarChartExtensions { /// /// Adds an item to the bar chart. /// /// The bar chart. /// The item label. /// The item value. /// The item color. /// The same instance so that multiple calls can be chained. public static BarChart AddItem(this BarChart chart, string label, double value, Color? color = null) { if (chart is null) { throw new ArgumentNullException(nameof(chart)); } chart.Data.Add(new BarChartItem(label, value, color)); return chart; } /// /// Adds an item to the bar chart. /// /// A type that implements . /// The bar chart. /// The item. /// The same instance so that multiple calls can be chained. public static BarChart AddItem(this BarChart chart, T item) where T : IBarChartItem { if (chart is null) { throw new ArgumentNullException(nameof(chart)); } if (item is BarChartItem barChartItem) { chart.Data.Add(barChartItem); } else { chart.Data.Add( new BarChartItem( item.Label, item.Value, item.Color)); } return chart; } /// /// Adds multiple items to the bar chart. /// /// A type that implements . /// The bar chart. /// The items. /// The same instance so that multiple calls can be chained. public static BarChart AddItems(this BarChart chart, IEnumerable items) where T : IBarChartItem { if (chart is null) { throw new ArgumentNullException(nameof(chart)); } if (items is null) { throw new ArgumentNullException(nameof(items)); } foreach (var item in items) { AddItem(chart, item); } return chart; } /// /// Adds multiple items to the bar chart. /// /// A type that implements . /// The bar chart. /// The items. /// The converter that converts instances of T to . /// The same instance so that multiple calls can be chained. public static BarChart AddItems(this BarChart chart, IEnumerable items, Func converter) { if (chart is null) { throw new ArgumentNullException(nameof(chart)); } if (items is null) { throw new ArgumentNullException(nameof(items)); } if (converter is null) { throw new ArgumentNullException(nameof(converter)); } foreach (var item in items) { chart.Data.Add(converter(item)); } return chart; } /// /// Sets the width of the bar chart. /// /// The bar chart. /// The bar chart width. /// The same instance so that multiple calls can be chained. public static BarChart Width(this BarChart chart, int? width) { if (chart is null) { throw new ArgumentNullException(nameof(chart)); } chart.Width = width; return chart; } /// /// Sets the label of the bar chart. /// /// The bar chart. /// The bar chart label. /// The same instance so that multiple calls can be chained. public static BarChart Label(this BarChart chart, string? label) { if (chart is null) { throw new ArgumentNullException(nameof(chart)); } chart.Label = label; return chart; } /// /// Shows values next to each bar in the bar chart. /// /// The bar chart. /// The same instance so that multiple calls can be chained. public static BarChart ShowValues(this BarChart chart) { return ShowValues(chart, true); } /// /// Hides values next to each bar in the bar chart. /// /// The bar chart. /// The same instance so that multiple calls can be chained. public static BarChart HideValues(this BarChart chart) { return ShowValues(chart, false); } /// /// Sets whether or not values should be shown /// next to each bar. /// /// The bar chart. /// Whether or not values should be shown next to each bar. /// The same instance so that multiple calls can be chained. public static BarChart ShowValues(this BarChart chart, bool show) { if (chart is null) { throw new ArgumentNullException(nameof(chart)); } chart.ShowValues = show; return chart; } /// /// Aligns the label to the left. /// /// The bar chart. /// The same instance so that multiple calls can be chained. public static BarChart LeftAlignLabel(this BarChart chart) { if (chart is null) { throw new ArgumentNullException(nameof(chart)); } chart.LabelAlignment = Justify.Left; return chart; } /// /// Centers the label. /// /// The bar chart. /// The same instance so that multiple calls can be chained. public static BarChart CenterLabel(this BarChart chart) { if (chart is null) { throw new ArgumentNullException(nameof(chart)); } chart.LabelAlignment = Justify.Center; return chart; } /// /// Aligns the label to the right. /// /// The bar chart. /// The same instance so that multiple calls can be chained. public static BarChart RightAlignLabel(this BarChart chart) { if (chart is null) { throw new ArgumentNullException(nameof(chart)); } chart.LabelAlignment = Justify.Right; return chart; } /// /// Sets the max fixed value for the chart. /// /// The bar chart. /// Max value for the chart. /// The same instance so that multiple calls can be chained. public static BarChart WithMaxValue(this BarChart chart, double maxValue) { if (chart is null) { throw new ArgumentNullException(nameof(chart)); } chart.MaxValue = maxValue; return chart; } }