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);
+ }
+
}
}