From fa1538915812c57c3cb4ba88ae16575edadc49f1 Mon Sep 17 00:00:00 2001 From: rifatx Date: Tue, 5 Oct 2021 01:49:09 +0300 Subject: [PATCH] Add support custom max value for barcharts (#545) --- .../Extensions/BarChartExtensions.cs | 19 ++++++++++++++++- .../Widgets/Charts/BarChart.cs | 10 +++++++-- .../Fixed_Max_Value.Output.verified.txt | 4 ++++ .../Unit/Widgets/BarChartTests.cs | 21 +++++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 test/Spectre.Console.Tests/Expectations/Widgets/BarChart/Fixed_Max_Value.Output.verified.txt diff --git a/src/Spectre.Console/Extensions/BarChartExtensions.cs b/src/Spectre.Console/Extensions/BarChartExtensions.cs index d54f608..22f8884 100644 --- a/src/Spectre.Console/Extensions/BarChartExtensions.cs +++ b/src/Spectre.Console/Extensions/BarChartExtensions.cs @@ -238,5 +238,22 @@ namespace Spectre.Console chart.LabelAlignment = Justify.Right; return chart; } + + /// + /// Sets the max fixed value for the chart. + /// + /// The bar chart. + /// Max value for the chart. + /// The same instance so that multiple calls can be chained. + public static BarChart WithMaxValue(this BarChart chart, double maxValue) + { + if (chart is null) + { + throw new ArgumentNullException(nameof(chart)); + } + + chart.MaxValue = maxValue; + return chart; + } } -} +} \ No newline at end of file diff --git a/src/Spectre.Console/Widgets/Charts/BarChart.cs b/src/Spectre.Console/Widgets/Charts/BarChart.cs index 169f4a0..c05a2e6 100644 --- a/src/Spectre.Console/Widgets/Charts/BarChart.cs +++ b/src/Spectre.Console/Widgets/Charts/BarChart.cs @@ -43,6 +43,12 @@ namespace Spectre.Console /// Defaults to invariant culture. public CultureInfo? Culture { get; set; } + /// + /// Gets or sets the fixed max value for a bar chart. + /// + /// Defaults to null, which corresponds to largest value in chart. + public double? MaxValue { get; set; } + /// /// Initializes a new instance of the class. /// @@ -62,7 +68,7 @@ namespace Spectre.Console protected override IEnumerable 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(); @@ -96,4 +102,4 @@ namespace Spectre.Console return ((IRenderable)grid).Render(context, width); } } -} +} \ No newline at end of file diff --git a/test/Spectre.Console.Tests/Expectations/Widgets/BarChart/Fixed_Max_Value.Output.verified.txt b/test/Spectre.Console.Tests/Expectations/Widgets/BarChart/Fixed_Max_Value.Output.verified.txt new file mode 100644 index 0000000..4f88ffe --- /dev/null +++ b/test/Spectre.Console.Tests/Expectations/Widgets/BarChart/Fixed_Max_Value.Output.verified.txt @@ -0,0 +1,4 @@ + Number of fruits + Apple ███ 12 +Orange █████████████████████████ 54 +Banana ██████████████ 33 diff --git a/test/Spectre.Console.Tests/Unit/Widgets/BarChartTests.cs b/test/Spectre.Console.Tests/Unit/Widgets/BarChartTests.cs index ea565b7..c96b576 100644 --- a/test/Spectre.Console.Tests/Unit/Widgets/BarChartTests.cs +++ b/test/Spectre.Console.Tests/Unit/Widgets/BarChartTests.cs @@ -47,5 +47,26 @@ namespace Spectre.Console.Tests.Unit // 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) + .WithMaxValue(100) + .Label("Number of fruits") + .AddItem("Apple", 12) + .AddItem("Orange", 54) + .AddItem("Banana", 33)); + + // Then + await Verifier.Verify(console.Output); + } + } }