Skip to content

Commit da9160d

Browse files
rebelinuxCopilot
andauthored
Potential fix for pull request finding
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent bdd5017 commit da9160d

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

Sources/StackedBarChart.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,44 @@ public object Chart(List<double[]> values, string[] labels, string[] categoryNam
236236
myPlot.DataBackground.Color = GetDrawingColor(DataBackgroundColor.Value);
237237
}
238238

239-
// Set axis limits if values are empty or contain only one value to prevent auto-scaling issues
240-
if (values.Count == 1)
239+
// Set axis limits if there is only one value to prevent auto-scaling issues
240+
if (values.Count == 1)
241241
{
242+
// Compute the stacked total for the single bar
243+
double stackedTotal = 0;
244+
if (values[0] != null)
245+
{
246+
foreach (double v in values[0])
247+
{
248+
stackedTotal += v;
249+
}
250+
}
251+
252+
// Ensure a non-zero span even if stackedTotal is zero or negative
253+
double paddingFraction = 0.1;
254+
double effectiveTotal = stackedTotal > 0 ? stackedTotal * (1 + paddingFraction) : 1.0;
255+
256+
double xMin, xMax, yMin, yMax;
257+
double barIndex = 0; // single bar at index 0
258+
242259
if (AreaOrientation == Orientations.Horizontal)
243260
{
244-
myPlot.Axes.SetLimits(0, 0, -2, 2);
261+
// Horizontal bars: X is value, Y is category (bar index)
262+
xMin = 0;
263+
xMax = effectiveTotal;
264+
yMin = barIndex - 0.5;
265+
yMax = barIndex + 0.5;
245266
}
246267
else
247268
{
248-
myPlot.Axes.SetLimits(-2, 2, 0, 0);
269+
// Vertical bars: X is category (bar index), Y is value
270+
xMin = barIndex - 0.5;
271+
xMax = barIndex + 0.5;
272+
yMin = 0;
273+
yMax = effectiveTotal;
249274
}
275+
276+
myPlot.Axes.SetLimits(xMin, xMax, yMin, yMax);
250277
}
251278

252279
// Apply watermark if enabled

0 commit comments

Comments
 (0)