Skip to content

Fix stacked bars generating zero-width segments - #592

Open
luc-x41 wants to merge 1 commit into
Kozea:masterfrom
luc-x41:master
Open

Fix stacked bars generating zero-width segments#592
luc-x41 wants to merge 1 commit into
Kozea:masterfrom
luc-x41:master

Conversation

@luc-x41

@luc-x41 luc-x41 commented Aug 23, 2026

Copy link
Copy Markdown

When generating a stacked bar chart using pygal, we noticed that it still generates bar segments that should not be visible in the first place. Some renderers show this (notice the yellow sliver on the right of the bar in this zoomed-in screenshot):

image

The grey part is correct and has the value drawn somewhere to the left (this is the screenshot as I got it from a colleague). We have show_zeroes=False and so the 0 value for the yellow metric is not shown (correctly), but the corresponding bar should also not be shown.

This patch prevents pygal from generating the zero-width segments in the first place. I chose to use math.isclose(value, 0) instead of value == 0 to avoid potential floating point comparison issues. The default tolerance is 1e-9, presumably chosen with CPython's default precision in mind. This function is available as of Python 3.5, whereas pygal requires 3.8.

The new test_bar_stacked.py is adapted from test_bar.py (test_stacked.py is already in use for line charts). I assume the copyright notice should be repeated everywhere regardless of who contributed a file so I left it as-is.

Per the contributing guidelines:

  • Test for the new code added
  • All tests (old and new) pass
  • Linter shows no new issues
  • No LLM was used in the identification of the issue or the addition of the fix or test
  • I looked at the documentation and would not say that this is a significant change that needs remarking
  • I wasn't sure if you would want a changelog entry for this, let me know if I should write one and squash it into this commit!

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.

1 participant