Skip to content

Commit eb65794

Browse files
committed
fix(windows): cap bar-chart bar width so sparse data doesn't render as a full-plot block
On pages with few data points (e.g. Drive History 'Drives over time' with a single drive), BarRects computed slotWidth = full plot width, making the lone bar ~70% of the plot — a giant solid rectangle. Caps per-bar width at 64px (recharts maxBarSize parity) and recomputes the band so grouped series stay centred. Verified via screenshot. Adds a regression test. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9ed9f97 commit eb65794

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

apps/windows/TeslaSync.App.Core/Charts/ChartGeometry.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ public static IReadOnlyList<RectD> BarRects(
169169
: Math.Abs(x.RangeEnd - x.RangeStart);
170170
var bandWidth = Math.Max(1, slotWidth * slotRatio);
171171
var barWidth = bandWidth / groups;
172+
173+
// Cap the per-bar width so a single or sparse category does not render as a giant block spanning the
174+
// whole plot (web recharts maxBarSize parity); recompute the band from the capped bar so grouped
175+
// series stay centred on their category.
176+
const double maxBarWidth = 64;
177+
if (barWidth > maxBarWidth)
178+
{
179+
barWidth = maxBarWidth;
180+
bandWidth = barWidth * groups;
181+
}
182+
172183
var baseline = y.Map(0);
173184

174185
var rects = new List<RectD>(series.Points.Count);

apps/windows/TeslaSync.App.Tests/ChartLogicTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ public void BarRects_GroupsWithoutOverlap()
112112
Assert.NotEqual(first[0].X, second[0].X);
113113
}
114114

115+
[Fact]
116+
public void BarRects_SinglePoint_CapsBarWidth()
117+
{
118+
// A single data point must not render as a giant block spanning the plot (recharts maxBarSize parity).
119+
var bars = new[]
120+
{
121+
new ChartSeries("a", [new ChartPoint(0, 1)]) { Kind = ChartSeriesKind.Bar },
122+
};
123+
var plot = ChartGeometry.PlotArea(800, 100, new EdgeInsets(0, 0, 0, 0));
124+
var x = ChartGeometry.BuildXScale(bars, plot);
125+
var y = ChartGeometry.BuildYScale(bars, plot);
126+
var rects = ChartGeometry.BarRects(bars, 0, x, y);
127+
Assert.Single(rects);
128+
// Without the cap this single bar would be ~0.7 * 800 = 560px wide; the cap holds it to 64px.
129+
Assert.True(rects[0].Width <= 64, $"bar width {rects[0].Width} should be capped at 64");
130+
}
131+
115132
[Fact]
116133
public void GaugeFraction_ClampsToUnit()
117134
{

0 commit comments

Comments
 (0)