Remove an axis from JavaFX Chart


Question

I've created a XYChart with numerical values different (for example temperatue with pressure) so I want to draw my own axeS just beside my chart. To do the following I've to unshow the YAxis, how should I do that ?

1
1
6/28/2013 10:45:07 PM

Accepted Answer

By using a trick: The Chart needs the Y Axis to remain in place so it knows where to render your content. You can, however, hide it. Hide the tick labels and set the axis' opacity to 0 using this code:

    chart.getYAxis().setTickLabelsVisible(false);
    chart.getYAxis().setOpacity(0);

The axis will still be there, but not shown.

6
6/29/2013 9:43:28 AM

SOLVED: I got this to work for sharing a common x-axis for two charts stacked vertically:

enter image description here

  1. Create two charts, each with their own identical copy of the x-axis object, setting identical upper and lower bounds (optionally by binding).

  2. Then hide the x-axis in the second chart like this:

chart = new LineChart<Number,Number>(xaxis2,yaxis2) {
{// hide xAxis in constructor, since not public
            getChartChildren().remove(getXAxis());
            // not getPlotChildren()
      }
};
  1. You'll want to set the widths of your y-axes to be the identical, e.g.

    int w = 60;
    yaxis.setMaxWidth(w);
    yaxis.setMinWidth(w);
    yaxis.setPrefWidth(w);
    yaxis2.setMaxWidth(w);
    yaxis2.setMinWidth(w);
    yaxis2.setPrefWidth(w);
    

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon