Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/barchart updates #1388

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 41 additions & 3 deletions src/Spectre.Console/Extensions/BarChartExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ public static class BarChartExtensions
/// <param name="label">The item label.</param>
/// <param name="value">The item value.</param>
/// <param name="color">The item color.</param>
/// <param name="labelColor">The label color.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static BarChart AddItem(this BarChart chart, string label, double value, Color? color = null)
public static BarChart AddItem(this BarChart chart, string label, double value, Color? color = null, Color? labelColor = null)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}

chart.Data.Add(new BarChartItem(label, value, color));
chart.Data.Add(new BarChartItem(label, value, color, labelColor));
jsheely marked this conversation as resolved.
Show resolved Hide resolved
return chart;
}

Expand Down Expand Up @@ -49,7 +50,7 @@ public static BarChart AddItem<T>(this BarChart chart, T item)
new BarChartItem(
item.Label,
item.Value,
item.Color));
item.Color, item.LabelColor));
}

return chart;
Expand Down Expand Up @@ -116,6 +117,43 @@ public static BarChart AddItems<T>(this BarChart chart, IEnumerable<T> items, Fu
return chart;
}

/// <summary>
/// Adds a value formatter to the <see cref="BarChart"/>.
/// </summary>
/// <param name="chart">The <see cref="BarChart"/> instance.</param>
/// <param name="func">The value formatter function.</param>
/// <returns>The modified <see cref="BarChart"/> instance.</returns>
public static BarChart UseValueFormatter(this BarChart chart, Func<double, CultureInfo, string>? func)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}

chart.ValueFormatter = func;
return chart;
}

/// <summary>
/// Tags will be shown.
/// </summary>
/// <param name="chart">The breakdown chart.</param>
/// <param name="func">The value formatter to use.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static BarChart UseValueFormatter(this BarChart chart, Func<double, string>? func)
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}

chart.ValueFormatter = func != null
? (value, _) => func(value)
: null;

return chart;
}

/// <summary>
/// Sets the width of the bar chart.
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions src/Spectre.Console/Spectre.Console.sln
jsheely marked this conversation as resolved.
Show resolved Hide resolved
jsheely marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spectre.Console", "Spectre.Console.csproj", "{7F082C13-9824-406F-B54E-078D76DE0B0B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7F082C13-9824-406F-B54E-078D76DE0B0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7F082C13-9824-406F-B54E-078D76DE0B0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F082C13-9824-406F-B54E-078D76DE0B0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F082C13-9824-406F-B54E-078D76DE0B0B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B97DD650-0EB4-49D2-9E2A-B458E75C1673}
EndGlobalSection
EndGlobal
8 changes: 7 additions & 1 deletion src/Spectre.Console/Widgets/Charts/BarChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public sealed class BarChart : Renderable, IHasCulture
/// <remarks>Defaults to null, which corresponds to largest value in chart.</remarks>
public double? MaxValue { get; set; }

/// <summary>
/// Gets or sets the value formatter function.
/// </summary>
public Func<double, CultureInfo, string>? ValueFormatter { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="BarChart"/> class.
/// </summary>
Expand Down Expand Up @@ -78,7 +83,7 @@ protected override IEnumerable<Segment> Render(RenderOptions options, int maxWid
foreach (var item in Data)
{
grid.AddRow(
new Markup(item.Label),
item.LabelColor != null ? new Text(item.Label, new Style(item.LabelColor)) : new Markup(item.Label),
new ProgressBar()
{
Value = item.Value,
Expand All @@ -90,6 +95,7 @@ protected override IEnumerable<Segment> Render(RenderOptions options, int maxWid
AsciiBar = '█',
ShowValue = ShowValues,
Culture = Culture,
ValueFormatter = ValueFormatter,
});
}

Expand Down
9 changes: 8 additions & 1 deletion src/Spectre.Console/Widgets/Charts/BarChartItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@ public sealed class BarChartItem : IBarChartItem
/// </summary>
public Color? Color { get; }

/// <summary>
/// Gets color of the label associated with the bar.
/// </summary>
public Color? LabelColor { get; }

/// <summary>
/// Initializes a new instance of the <see cref="BarChartItem"/> class.
/// </summary>
/// <param name="label">The item label.</param>
/// <param name="value">The item value.</param>
/// <param name="color">The item color.</param>
public BarChartItem(string label, double value, Color? color = null)
/// <param name="labelColor">The label color.</param>
public BarChartItem(string label, double value, Color? color = null, Color? labelColor = null)
{
Label = label ?? throw new ArgumentNullException(nameof(label));
Value = value;
Color = color;
LabelColor = labelColor;
}
}
5 changes: 5 additions & 0 deletions src/Spectre.Console/Widgets/Charts/IBarChartItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public interface IBarChartItem
/// Gets the item color.
/// </summary>
Color? Color { get; }

/// <summary>
/// Gets the label color.
/// </summary>
Color? LabelColor { get; }
jsheely marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 2 additions & 1 deletion src/Spectre.Console/Widgets/ProgressBar.cs
jsheely marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal sealed class ProgressBar : Renderable, IHasCulture
public Style FinishedStyle { get; set; } = Color.Green;
public Style RemainingStyle { get; set; } = Color.Grey;
public Style IndeterminateStyle { get; set; } = DefaultPulseStyle;
public Func<double, CultureInfo, string>? ValueFormatter { get; set; }

internal static Style DefaultPulseStyle { get; } = new Style(foreground: Color.DodgerBlue1, background: Color.Grey23);

Expand Down Expand Up @@ -50,7 +51,7 @@ protected override IEnumerable<Segment> Render(RenderOptions options, int maxWid
var barCount = Math.Max(0, (int)(width * (completedBarCount / MaxValue)));

// Show value?
var value = completedBarCount.ToString(Culture ?? CultureInfo.InvariantCulture);
var value = ValueFormatter != null ? ValueFormatter(completedBarCount, Culture ?? CultureInfo.InvariantCulture) : completedBarCount.ToString(Culture ?? CultureInfo.InvariantCulture);
if (ShowValue)
{
barCount = barCount - value.Length - 1;
Expand Down