diff --git a/Sources/StackedBarChart.cs b/Sources/StackedBarChart.cs index 8ed2a5f..b1d1c54 100755 --- a/Sources/StackedBarChart.cs +++ b/Sources/StackedBarChart.cs @@ -85,6 +85,8 @@ public object Chart(List values, string[] labels, string[] categoryNam // create bars var bars = new List(); + // Set a moderate bar width when there is only one element to prevent it from filling the entire chart area + double barSize = values.Count == 1 ? 0.5 : 0.8; // assign values and colors to each bar for (int x = 0; x < values.Count; x++) { @@ -101,6 +103,7 @@ public object Chart(List values, string[] labels, string[] categoryNam FillColor = colorPalette.GetColor(i), Label = $"{values[x][i]}", CenterLabel = true, + Size = barSize, }); nextBarBase += values[x][i]; } diff --git a/Tests/AsBuiltReport.Chart.Functions.Tests.ps1 b/Tests/AsBuiltReport.Chart.Functions.Tests.ps1 index 6b29037..c946728 100644 --- a/Tests/AsBuiltReport.Chart.Functions.Tests.ps1 +++ b/Tests/AsBuiltReport.Chart.Functions.Tests.ps1 @@ -70,5 +70,13 @@ Describe 'AsBuiltReport.Chart Exported Functions' { It 'Should throw error for mismatched Values and LegendCategories' { { New-StackedBarChart -Title 'Test' -Values @(@(1, 2), @(3, 4)) -Labels @('A', 'B') -LegendCategories @('X') -Format 'png' -OutputFolderPath $TestDrive } | Should -Throw -ExpectedMessage "Error: Values and category names must be equal." } + It 'Should run without error with a single element (single-bar chart)' { + { New-StackedBarChart -Title 'Test' -Values @(,[double[]]@(1, 2)) -Labels @('A') -LegendCategories @('X', 'Y') -Format 'png' -OutputFolderPath $TestDrive } | Should -Not -Throw + } + It 'Should return a file path as output for a single element' { + $result = New-StackedBarChart -Title 'Test' -Values @(,[double[]]@(1, 2)) -Labels @('A') -LegendCategories @('X', 'Y') -Format 'png' -OutputFolderPath $TestDrive + $result | Should -BeOfType 'System.IO.FileSystemInfo' + Test-Path $result | Should -BeTrue + } } }