Skip to content

feat: add ConsoleEncoder for terminal/console chart output#978

Merged
timmolter merged 2 commits into
knowm:developfrom
mvanhorn:feat/861-console-encoder
Jun 22, 2026
Merged

feat: add ConsoleEncoder for terminal/console chart output#978
timmolter merged 2 commits into
knowm:developfrom
mvanhorn:feat/861-console-encoder

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Summary

Adds a new public utility class ConsoleEncoder (in org.knowm.xchart, alongside BitmapEncoder) that renders any chart as a text string for terminal/console output. It reuses the existing BitmapEncoder.getBufferedImage(chart) to rasterize the chart, then downsamples the image into a character grid: each block's average luminance is mapped to a Unicode block-shade glyph (space, light, medium, dark, full). Because charts render dark ink on a light background, darker pixels map to denser glyphs so the data is visible.

Public API:

  • String getConsoleString(IChart chart) - default width with rows derived from the chart aspect ratio
  • String getConsoleString(IChart chart, int columns, int rows) - exact grid shape
  • void printConsole(IChart chart) - writes to System.out

No core rendering, styler, or IChart code is touched. The class is fully self-contained, matching the approach the maintainer outlined in the issue thread.

Why this matters

Issue #861 requests a console/terminal backend so charts can be rendered as text in environments without a display (CI logs, SSH sessions, quick debugging), similar to the rust-plotters console example. This lets users get a readable ASCII/Unicode preview of any chart with a single call, building on the existing encoder family without any risk to the rendering pipeline.

Testing

Added ConsoleEncoderTest (JUnit 5), patterned on BitmapEncoderTest:

  • Requested grid produces exactly rows lines of columns characters
  • Small grid (10x5) renders without index-out-of-bounds on pixel-block sampling
  • Output spans the ramp: contains both background (space) and visible data glyphs, confirming the brightness-to-glyph direction
  • Default-dimension overload does not throw and contains newlines
  • Null chart throws NullPointerException; non-positive columns/rows throw IllegalArgumentException

Verified locally: mvn -pl xchart -am compile and mvn -pl xchart -Dtest=ConsoleEncoderTest test both pass (5/5 tests green).

Fixes #861

Add a self-contained ConsoleEncoder utility alongside BitmapEncoder that
renders any IChart as a Unicode block-glyph string for terminal output.
It reuses BitmapEncoder.getBufferedImage(chart), downsamples the image
into a character grid by averaging each block's luminance, and maps
darker pixels to denser glyphs so chart data is visible on the light
background. No core rendering, styler, or IChart code is changed.
@timmolter

Copy link
Copy Markdown
Member
Screenshot 2026-06-22 at 1 12 32 PM

Complete the empty @param chart tags on the public ConsoleEncoder methods
and remove the duplicate validateDimensions call in the private overload;
validation already runs early in the public (chart, columns, rows) overload
before rasterization, and the default overload always passes positive values.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@timmolter

Copy link
Copy Markdown
Member

Neat feature! What do you plan on using it for??

@timmolter timmolter merged commit 60c3d99 into knowm:develop Jun 22, 2026
1 check passed
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Thank you @timmolter. A ConsoleEncoder that renders charts straight to the terminal is a fun and genuinely useful addition.

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.

Output for terminal/console

2 participants