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

Add support custom max value for barcharts #545

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/Spectre.Console/Extensions/BarChartExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,22 @@ public static BarChart RightAlignLabel(this BarChart chart)
chart.LabelAlignment = Justify.Right;
return chart;
}

/// <summary>
/// Sets the max fixed value for the chart.
/// </summary>
/// <param name="chart">The bar chart.</param>
/// <param name="maxValue">Max value for the chart.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static BarChart WithFixedMaxValue(this BarChart chart, double maxValue)
patriksvensson marked this conversation as resolved.
Show resolved Hide resolved
{
if (chart is null)
{
throw new ArgumentNullException(nameof(chart));
}

chart.MaxValue = maxValue;
return chart;
}
}
}
}
10 changes: 8 additions & 2 deletions src/Spectre.Console/Widgets/Charts/BarChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public sealed class BarChart : Renderable, IHasCulture
/// <remarks>Defaults to invariant culture.</remarks>
public CultureInfo? Culture { get; set; }

/// <summary>
/// Gets or sets the fixed max value for a bar chart.
/// </summary>
/// <remarks>Defaults to null, which corresponds to largest value in chart.</remarks>
public double? MaxValue { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="BarChart"/> class.
/// </summary>
Expand All @@ -62,7 +68,7 @@ protected override Measurement Measure(RenderContext context, int maxWidth)
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
{
var width = Math.Min(Width ?? maxWidth, maxWidth);
var maxValue = Data.Max(item => item.Value);
var maxValue = Math.Max(MaxValue ?? 0d, Data.Max(item => item.Value));

var grid = new Grid();
grid.Collapse();
Expand Down Expand Up @@ -96,4 +102,4 @@ protected override IEnumerable<Segment> Render(RenderContext context, int maxWid
return ((IRenderable)grid).Render(context, width);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Number of fruits
Apple ███ 12
Orange █████████████████████████ 54
Banana ██████████████ 33
21 changes: 21 additions & 0 deletions test/Spectre.Console.Tests/Unit/Widgets/BarChartTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,26 @@ public async Task Should_Render_Correctly_2()
// Then
await Verifier.Verify(console.Output);
}

[Fact]
[Expectation("Fixed_Max_Value")]
public async Task Should_Render_Correctly_3()
{
// Given
var console = new TestConsole();

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

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

}
}