Skip to content

test: regression test for issue #707 (last bar label overwritten in CategoryChart)#977

Merged
timmolter merged 2 commits into
developfrom
timmolter-issue-707-category-bar-label
Jun 20, 2026
Merged

test: regression test for issue #707 (last bar label overwritten in CategoryChart)#977
timmolter merged 2 commits into
developfrom
timmolter-issue-707-category-bar-label

Conversation

@timmolter

Copy link
Copy Markdown
Member

Summary

Adds a regression test for issue #707 — the last bar in a Bar-style CategoryChart with setLabelsVisible(true) was rendered twice, causing its label to be overwritten.

Root Cause

The bar rendering loop reused the shared path variable (also used by Area render style). After the inner loop, closePath() was called unconditionally and would re-fill path — which at that point held the last bar's Path2D — painting solid fill over the already-drawn label.

Fix (already in codebase)

Commit ff2536f4 changed bar rendering to use a local barPath variable instead of the shared path. This keeps path == null throughout bar rendering so closePath() becomes a no-op for bar series.

A demo file (TestForIssue707.java) also already exists.

What This PR Adds

A headless render regression test in CategoryChartTest that exercises the exact scenario from the issue report (Matlab theme, setLabelsVisible(true), 5 data points). The test uses BitmapEncoder.getBitmapBytes to trigger the full AWT rendering path and will catch any re-introduction of the shared-path bug.

Closes #707

Adds a headless render test that exercises the exact scenario from
issue #707: a Bar-style CategoryChart with setLabelsVisible(true).
The bug (using shared 'path' so closePath() repainted the last bar
over its label) was fixed in ff2536f by switching to a local
'barPath'. This test guards against any future regression.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a JUnit regression test in the XChart test suite intended to prevent reintroducing issue #707, where the last bar label in a Bar-style CategoryChart could be overwritten due to an extra post-loop fill.

Changes:

  • Add a new regression test to CategoryChartTest targeting issue #707’s “last bar label overwritten” scenario.
  • Introduce an import for Styler.ChartTheme to construct a CategoryChart with the Matlab theme in the new test.

Comment thread xchart/src/test/java/org/knowm/xchart/CategoryChartTest.java
Replace the no-throw assertion with a pixel-level regression check that
actually detects the visual bug:

- Render the 5-bar chart twice (labels ON and OFF) and count differing
  pixels -> diff5 (covers all 5 label contributions).
- Render the same chart with the last value as NaN (bar 5 skipped) twice
  (labels ON and OFF) -> diffNaN (covers only 4 label contributions).
- Assert diff5 > diffNaN.

When the original bug is reintroduced (bar loop reuses shared 'path' and
closePath() repaints the last bar over its label), the 5th label's pixels
vanish and diff5 equals diffNaN, making the assertion fail. The 5th label
currently contributes 64 distinct pixels, providing a clear margin.

Addresses Copilot PR review comment.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@timmolter timmolter merged commit ae9f563 into develop Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Label of Last Bar in CategoryChart Not Visible

2 participants