From 47ef9a4375ece61d590f9bf7f2b8e8335d639357 Mon Sep 17 00:00:00 2001 From: Tim Molter Date: Mon, 22 Jun 2026 16:53:47 +0200 Subject: [PATCH] demo: Strings on the Y-axis via custom tick formatter (issue #884) Adds TestForIssue884 showing how to display String labels on the Y-axis of any axes-based chart by plotting numeric category indices and mapping them back to Strings with setYAxisTickLabelsFormattingFunction. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../standalone/issues/TestForIssue884.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue884.java diff --git a/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue884.java b/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue884.java new file mode 100644 index 00000000..ea772134 --- /dev/null +++ b/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue884.java @@ -0,0 +1,66 @@ +package org.knowm.xchart.standalone.issues; + +import java.util.Arrays; +import java.util.List; +import org.knowm.xchart.SwingWrapper; +import org.knowm.xchart.XYChart; +import org.knowm.xchart.XYChartBuilder; +import org.knowm.xchart.XYSeries.XYSeriesRenderStyle; + +/** + * Reproducer / how-to for https://github.com/knowm/XChart/issues/884 + * + *

"For a chart I need on the Y-Axis Strings instead of Numbers." + * + *

XChart does not transpose arbitrary charts, but any axes-based chart can show Strings on the + * Y-axis by plotting numeric category indices (0, 1, 2, ...) and supplying a custom Y-axis tick + * label formatter that maps each numeric tick position back to a String. This works for XYChart, + * line/scatter, etc. — not just bar charts. + */ +public class TestForIssue884 { + + /** The String labels we want to see on the Y-axis instead of numbers. */ + private static final List CATEGORIES = + Arrays.asList("Apples", "Bananas", "Cherries", "Dates", "Elderberries"); + + public static void main(String[] args) { + + new SwingWrapper<>(getChart()).displayChart(); + } + + /** Constructs and returns the chart without launching a window (headless-safe). */ + public static XYChart getChart() { + + XYChart chart = + new XYChartBuilder() + .width(800) + .height(600) + .title("Strings on the Y-Axis (issue 884)") + .xAxisTitle("Value") + .yAxisTitle("Fruit") + .build(); + + chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Scatter); + + // Y values are simply the category indices: 0..CATEGORIES.size()-1 + double[] xData = {12, 7, 19, 4, 15}; + double[] yData = {0, 1, 2, 3, 4}; + chart.addSeries("counts", xData, yData); + + // The key: map each numeric Y tick position to its String label. Non-integer tick positions + // (the formatter may be asked about fractional grid lines) are blanked out so only the + // category rows are labelled. + chart + .getStyler() + .setYAxisTickLabelsFormattingFunction( + value -> { + int index = (int) Math.round(value); + if (Math.abs(value - index) < 1e-9 && index >= 0 && index < CATEGORIES.size()) { + return CATEGORIES.get(index); + } + return ""; + }); + + return chart; + } +}