Skip to content

Commit dd28e9f

Browse files
Copilotrebelinux
andauthored
Fix stacked bar column filling entire chart area when single element is present (#10)
* Initial plan * Fix: Set moderate bar width for single-element stacked bar charts Co-authored-by: rebelinux <1002783+rebelinux@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: rebelinux <1002783+rebelinux@users.noreply.github.com>
1 parent 158e6bf commit dd28e9f

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

Sources/StackedBarChart.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ public object Chart(List<double[]> values, string[] labels, string[] categoryNam
8585

8686
// create bars
8787
var bars = new List<ScottPlot.Bar>();
88+
// Set a moderate bar width when there is only one element to prevent it from filling the entire chart area
89+
double barSize = values.Count == 1 ? 0.5 : 0.8;
8890
// assign values and colors to each bar
8991
for (int x = 0; x < values.Count; x++)
9092
{
@@ -101,6 +103,7 @@ public object Chart(List<double[]> values, string[] labels, string[] categoryNam
101103
FillColor = colorPalette.GetColor(i),
102104
Label = $"{values[x][i]}",
103105
CenterLabel = true,
106+
Size = barSize,
104107
});
105108
nextBarBase += values[x][i];
106109
}

Tests/AsBuiltReport.Chart.Functions.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,13 @@ Describe 'AsBuiltReport.Chart Exported Functions' {
7070
It 'Should throw error for mismatched Values and LegendCategories' {
7171
{ 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."
7272
}
73+
It 'Should run without error with a single element (single-bar chart)' {
74+
{ New-StackedBarChart -Title 'Test' -Values @(,[double[]]@(1, 2)) -Labels @('A') -LegendCategories @('X', 'Y') -Format 'png' -OutputFolderPath $TestDrive } | Should -Not -Throw
75+
}
76+
It 'Should return a file path as output for a single element' {
77+
$result = New-StackedBarChart -Title 'Test' -Values @(,[double[]]@(1, 2)) -Labels @('A') -LegendCategories @('X', 'Y') -Format 'png' -OutputFolderPath $TestDrive
78+
$result | Should -BeOfType 'System.IO.FileSystemInfo'
79+
Test-Path $result | Should -BeTrue
80+
}
7381
}
7482
}

0 commit comments

Comments
 (0)