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 15 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
10 changes: 10 additions & 0 deletions docs/input/widgets/barchart.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,13 @@ AnsiConsole.Write(new BarChart()
.AddItem(new Fruit("Mango", 3))
.AddItems(items));
```

### Addd items with bar and label style
jsheely marked this conversation as resolved.
Show resolved Hide resolved

```csharp
AnsiConsole.Write(new BarChart()
.Width(60)
.Label("[green bold underline]Number of fruits[/]")
.CenterLabel()
.AddItem("Apple", 12, Color.Red, Color.Red);
```
7 changes: 4 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="labelStyle">The label style.</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, Style? labelStyle = 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, labelStyle));
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.LabelStyle));
}

return chart;
Expand Down
2 changes: 1 addition & 1 deletion src/Spectre.Console/Widgets/Charts/BarChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected override IEnumerable<Segment> Render(RenderOptions options, int maxWid
foreach (var item in Data)
{
grid.AddRow(
new Markup(item.Label),
item.LabelStyle != null ? new Text(item.Label, item.LabelStyle) : new Markup(item.Label),
jsheely marked this conversation as resolved.
Show resolved Hide resolved
new ProgressBar()
{
Value = item.Value,
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 Style? LabelStyle { 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="labelStyle">The label color.</param>
public BarChartItem(string label, double value, Color? color = null, Style? labelStyle = null)
{
Label = label ?? throw new ArgumentNullException(nameof(label));
Value = value;
Color = color;
LabelStyle = labelStyle;
}
}
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>
Style? LabelStyle { get; }
}
1 change: 1 addition & 0 deletions 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 @@ -21,6 +21,7 @@
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; }

Check failure on line 24 in src/Spectre.Console/Widgets/ProgressBar.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

The type 'ProgressBar' already contains a definition for 'ValueFormatter'

Check failure on line 24 in src/Spectre.Console/Widgets/ProgressBar.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

The type 'ProgressBar' already contains a definition for 'ValueFormatter'

Check failure on line 24 in src/Spectre.Console/Widgets/ProgressBar.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

The type 'ProgressBar' already contains a definition for 'ValueFormatter'

Check failure on line 24 in src/Spectre.Console/Widgets/ProgressBar.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

The type 'ProgressBar' already contains a definition for 'ValueFormatter'

Check failure on line 24 in src/Spectre.Console/Widgets/ProgressBar.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

The type 'ProgressBar' already contains a definition for 'ValueFormatter'

Check failure on line 24 in src/Spectre.Console/Widgets/ProgressBar.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

The type 'ProgressBar' already contains a definition for 'ValueFormatter'

Check failure on line 24 in src/Spectre.Console/Widgets/ProgressBar.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

The type 'ProgressBar' already contains a definition for 'ValueFormatter'

Check failure on line 24 in src/Spectre.Console/Widgets/ProgressBar.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

The type 'ProgressBar' already contains a definition for 'ValueFormatter'

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
 Number of fruits
Apple ████████ 12 
Orange █████████████████████████████████████████████████ 54
Banana ████████████████████████████ 33 
19 changes: 19 additions & 0 deletions test/Spectre.Console.Tests/Unit/Widgets/BarChartTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,23 @@ public async Task Should_Render_Correctly_3()
// Then
await Verifier.Verify(console.Output);
}

[Fact]
[Expectation("Style")]
public async Task Should_Render_Correctly_WithStyle()
{
// Given
var console = new TestConsole().EmitAnsiSequences();

// When
console.Write(new BarChart()
.Width(60)
.Label("Number of fruits")
.AddItem("Apple", 12, Color.Red, Color.Red)
.AddItem("Orange", 54, Color.Orange1, Color.Orange1)
.AddItem("Banana", 33, Color.Yellow, Color.Yellow));

// Then
await Verifier.Verify(console.Output);
}
}