Add formatting support for breakdown chart values

Closes #252
This commit is contained in:
Patrik Svensson
2021-02-01 21:53:49 +01:00
committed by Patrik Svensson
parent b64e016e8c
commit 705cf745ea
9 changed files with 58 additions and 91 deletions

View File

@ -20,12 +20,6 @@ namespace Spectre.Console
/// </summary>
public int? Width { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not
/// to show values as percentages or not.
/// </summary>
public bool ShowAsPercentages { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not to show tags.
/// </summary>
@ -36,6 +30,11 @@ namespace Spectre.Console
/// </summary>
public bool ShowTagValues { get; set; } = true;
/// <summary>
/// Gets or sets the tag value format.
/// </summary>
public string? TagValueFormat { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not the
/// chart and tags should be rendered in compact mode.
@ -91,8 +90,8 @@ namespace Spectre.Console
{
Width = width,
Culture = Culture,
ShowPercentages = ShowAsPercentages,
ShowTagValues = ShowTagValues,
TagValueFormat = TagValueFormat,
});
}

View File

@ -11,8 +11,8 @@ namespace Spectre.Console
public int? Width { get; set; }
public CultureInfo? Culture { get; set; }
public bool ShowPercentages { get; set; }
public bool ShowTagValues { get; set; } = true;
public string? TagValueFormat { get; set; }
public BreakdownTags(List<IBreakdownChartItem> data)
{
@ -56,11 +56,13 @@ namespace Spectre.Console
private string FormatValue(IBreakdownChartItem item, CultureInfo culture)
{
var formatter = TagValueFormat ?? "{0}";
if (ShowTagValues)
{
return string.Format(culture, "{0} [grey]{1}{2}[/]",
item.Label.EscapeMarkup(), item.Value,
ShowPercentages ? "%" : string.Empty);
return string.Format(culture, "{0} [grey]{1}[/]",
item.Label.EscapeMarkup(),
string.Format(culture, formatter, item.Value));
}
return item.Label.EscapeMarkup();