Search code examples
javajfreechart

How can I make the desired axis on a chart invisible?


Please look at the picture.

enter image description here

The x-axis is removed, but the y-axis is not removed. Why? The Java code is down there.

ValueAxis axis = chart.getXYPlot().getDomainAxis();
axis.setVisible(false);

Solution

  • Note this distinction between the model, a Dataset, and view, an Axis:

    • XYPlot::getDomainAxis returns a reference to the axis that displays the domain (X values) of the chart's XYDataset.

    • XYPlot::getRangeAxis returns a reference to the axis that displays the range (Y values) of the chart's XYDataset.

    Focusing on the domain axis, the result of setVisible(false) depends on the PlotOrientation. XYPlot::getOrientation returns a reference to the orientation, typically specified in the ChartFactory used to construct the chart. Because a conventional plot has a vertical y-axis, the PlotOrientation is VERTICAL for a plot where the domain axis is horizontal, and the PlotOrientation is HORIZONTAL for a plot where the domain axis is vertical.

    In this example, the domain is minutes and the range is number of students.

    image

    In both examples below, the domain axis (minutes) is made invisible.

    ValueAxis domain = chart.getXYPlot().getDomainAxis();
    domain.setVisible(false);
    

    Result with PlotOrientation.VERTICAL; invisible domain axis is horizontal.

    PlotOrientation.VERTICAL

    Result with PlotOrientation.HORIZONTAL; invisible domain axis is vertical.

    PlotOrientation.HORIZONTAL

    A similar analysis applies to making a range axis invisible.