namespace Spectre.Console;
///
/// Contains extension methods for .
///
public static class BreakdownChartExtensions
{
///
/// Adds an item to the breakdown chart.
///
/// The breakdown chart.
/// The item label.
/// The item value.
/// The item color.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart AddItem(this BreakdownChart chart, string label, double value, Color color)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}
chart.Data.Add(new BreakdownChartItem(label, value, color));
return chart;
}
///
/// Adds an item to the breakdown chart.
///
/// A type that implements .
/// The breakdown chart.
/// The item.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart AddItem(this BreakdownChart chart, T item)
where T : IBreakdownChartItem
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}
if (item is BreakdownChartItem chartItem)
{
chart.Data.Add(chartItem);
}
else
{
chart.Data.Add(
new BreakdownChartItem(
item.Label,
item.Value,
item.Color));
}
return chart;
}
///
/// Adds multiple items to the breakdown chart.
///
/// A type that implements .
/// The breakdown chart.
/// The items.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart AddItems(this BreakdownChart chart, IEnumerable items)
where T : IBreakdownChartItem
{
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 breakdown chart.
///
/// A type that implements .
/// The breakdown chart.
/// The items.
/// The converter that converts instances of T to .
/// The same instance so that multiple calls can be chained.
public static BreakdownChart AddItems(this BreakdownChart 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 breakdown chart.
///
/// The breakdown chart.
/// The breakdown chart width.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart Width(this BreakdownChart chart, int? width)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}
chart.Width = width;
return chart;
}
///
/// Tags will be shown.
///
/// The breakdown chart.
/// The value formatter to use.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart UseValueFormatter(this BreakdownChart chart, Func? func)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}
chart.ValueFormatter = func;
return chart;
}
///
/// Tags will be shown.
///
/// The breakdown chart.
/// The value formatter to use.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart UseValueFormatter(this BreakdownChart chart, Func? func)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}
chart.ValueFormatter = func != null
? (value, _) => func(value)
: null;
return chart;
}
///
/// Tags will be shown.
///
/// The breakdown chart.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart ShowPercentage(this BreakdownChart chart)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}
chart.ValueFormatter = (value, culture) => string.Format(culture, "{0}%", value);
return chart;
}
///
/// Tags will be shown.
///
/// The breakdown chart.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart ShowTags(this BreakdownChart chart)
{
return ShowTags(chart, true);
}
///
/// Tags will be not be shown.
///
/// The breakdown chart.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart HideTags(this BreakdownChart chart)
{
return ShowTags(chart, false);
}
///
/// Sets whether or not tags will be shown.
///
/// The breakdown chart.
/// Whether or not tags will be shown.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart ShowTags(this BreakdownChart chart, bool show)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}
chart.ShowTags = show;
return chart;
}
///
/// Tag values will be shown.
///
/// The breakdown chart.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart ShowTagValues(this BreakdownChart chart)
{
return ShowTagValues(chart, true);
}
///
/// Tag values will be not be shown.
///
/// The breakdown chart.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart HideTagValues(this BreakdownChart chart)
{
return ShowTagValues(chart, false);
}
///
/// Sets whether or not tag values will be shown.
///
/// The breakdown chart.
/// Whether or not tag values will be shown.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart ShowTagValues(this BreakdownChart chart, bool show)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}
chart.ShowTagValues = show;
return chart;
}
///
/// Chart and tags is rendered in compact mode.
///
/// The breakdown chart.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart Compact(this BreakdownChart chart)
{
return Compact(chart, true);
}
///
/// Chart and tags is rendered in full size mode.
///
/// The breakdown chart.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart FullSize(this BreakdownChart chart)
{
return Compact(chart, false);
}
///
/// Sets whether or not the chart and tags should be rendered in compact mode.
///
/// The breakdown chart.
/// Whether or not the chart and tags should be rendered in compact mode.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart Compact(this BreakdownChart chart, bool compact)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}
chart.Compact = compact;
return chart;
}
///
/// Sets the .
///
/// The breakdown chart.
/// The to set.
/// The same instance so that multiple calls can be chained.
public static BreakdownChart WithValueColor(this BreakdownChart chart, Color color)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}
chart.ValueColor = color;
return chart;
}
}