feat: add ConsoleEncoder for terminal/console chart output#978
Merged
Conversation
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.
Member
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>
Member
|
Neat feature! What do you plan on using it for?? |
Contributor
Author
|
Thank you @timmolter. A ConsoleEncoder that renders charts straight to the terminal is a fun and genuinely useful addition. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Adds a new public utility class
ConsoleEncoder(inorg.knowm.xchart, alongsideBitmapEncoder) that renders any chart as a text string for terminal/console output. It reuses the existingBitmapEncoder.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 ratioString getConsoleString(IChart chart, int columns, int rows)- exact grid shapevoid printConsole(IChart chart)- writes to System.outNo core rendering, styler, or
IChartcode 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 onBitmapEncoderTest:rowslines ofcolumnscharactersVerified locally:
mvn -pl xchart -am compileandmvn -pl xchart -Dtest=ConsoleEncoderTest testboth pass (5/5 tests green).Fixes #861