mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 17:02:51 +08:00
Add support custom max value for barcharts (#545)
This commit is contained in:
parent
a5716a35e2
commit
fa15389158
@ -238,5 +238,22 @@ namespace Spectre.Console
|
|||||||
chart.LabelAlignment = Justify.Right;
|
chart.LabelAlignment = Justify.Right;
|
||||||
return chart;
|
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 WithMaxValue(this BarChart chart, double maxValue)
|
||||||
|
{
|
||||||
|
if (chart is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(chart));
|
||||||
|
}
|
||||||
|
|
||||||
|
chart.MaxValue = maxValue;
|
||||||
|
return chart;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -43,6 +43,12 @@ namespace Spectre.Console
|
|||||||
/// <remarks>Defaults to invariant culture.</remarks>
|
/// <remarks>Defaults to invariant culture.</remarks>
|
||||||
public CultureInfo? Culture { get; set; }
|
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>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="BarChart"/> class.
|
/// Initializes a new instance of the <see cref="BarChart"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -62,7 +68,7 @@ namespace Spectre.Console
|
|||||||
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
|
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
|
||||||
{
|
{
|
||||||
var width = Math.Min(Width ?? maxWidth, 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();
|
var grid = new Grid();
|
||||||
grid.Collapse();
|
grid.Collapse();
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
Number of fruits
|
||||||
|
Apple ███ 12
|
||||||
|
Orange █████████████████████████ 54
|
||||||
|
Banana ██████████████ 33
|
@ -47,5 +47,26 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
// Then
|
// Then
|
||||||
await Verifier.Verify(console.Output);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user