Skip to content

setxAxisTickLabelsFormattingFunction not working as expected in XYChart #792

@creme332

Description

@creme332

I have the following chart and I want to hide every 2 tick labels on the x-axis.

image

The expected output should be:
image

I used the setxAxisTickLabelsFormattingFunction method as follows:

import org.knowm.xchart.QuickChart;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;

public class App {
    public static void main(String[] args) throws Exception {
        double[] yData = new double[] { 100, 90, 90, 89, 80, 101, 102, 99 };

        // Create Chart
        XYChart chart = QuickChart.getChart("Sample Chart", "X", "Y", "y(x)", null, yData);

        // hide every 2 xtick labels
        chart.getStyler().setxAxisTickLabelsFormattingFunction(x -> x.intValue() % 2 == 0 ? String.valueOf(x) : " ");

        // Show it
        new SwingWrapper(chart).displayChart();
    }
}

The resulting incorrect chart is:

image

If I change the formatting function to x -> x.intValue() % 2 == 0 ? String.valueOf(x) : "skip", the resulting chart becomes:

image

It seems that there are only 2 unique values of x that are passed to the formatting function instead of 8.

How can I achieve my desired output? Thanks.

Note: I am using xchart-3.8.5.jar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions