Skip to content

Fit stacked bars to the requested width - #134

Merged
mkaz merged 1 commit into
mkaz:mainfrom
RClaudiuM:fix/69-stacked-width
Nov 20, 2026
Merged

mkaz merged 1 commit into
mkaz:mainfrom
RClaudiuM:fix/69-stacked-width

Conversation

@RClaudiuM

Copy link
Copy Markdown
Contributor

--stacked ignored --width: Data.normalize() scales the largest single value to the width, but a stacked bar is the sum of its segments, so bars could be several times wider than requested.

This overrides StackedChart._normalize() to scale by the largest row total instead, so the longest stacked bar spans --width and the others stay proportional to their totals. The running total is rounded rather than each segment, so a bar doesn't lose a block per segment to truncation. --scale (fixed units per tick) is unchanged.

Fixes #69. This also covers the "bar wider than width" part of #83.

Before (example from the issue):

$ printf "test1 10 20 40 40 40\ntest2 10 20 40 40 10\ntest3 10 10 10 10 10\n" | termgraph --custom-tick=X --stacked --width=10
test1: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 150.00
test2: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX 120.00
test3: XXXXXXXXXX 50.00

After:

test1: XXXXXXXXXX 150.00
test2: XXXXXXXX 120.00
test3: X▏XX▏ 50.00

Tests: two regression tests in tests/test_charts.py (longest bar equals the width; bar lengths follow row totals). Both fail on main and pass with the change.

  • uv run python -m pytest: 90 passed
  • uv run python -m ruff check .: passed
  • uv run python -m mypy termgraph/: no issues
  • tests/module-test*.py all run

Data.normalize() scales the largest single value to the width, so a
stacked bar, which is the sum of its segments, could be several times
wider than --width. Scale stacked charts by the largest row total and
round the running total so that each bar's length follows its total.
@mkaz
mkaz merged commit 142187c into mkaz:main Nov 20, 2026
1 check passed
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.

--width only applies to the highest value with used with --stack

2 participants