1

How can I remove the y labels from a JFreeChart chart? I use a NumberAxis for my y axis.

I can't seem to find a simple method for this anywhere.

I would like something similar to the remove legend syntax:

    // Remove the legend
    chart.removeLegend();

Note that I do want to define the title in the NumberAxis:

NumberAxis axis1 = new NumberAxis("A random title");

I simply don't want it to show up in the final chart.

Jean-Paul
  • 19,910
  • 9
  • 62
  • 88

2 Answers2

3

I think that you mean that you want to hide the tick labels for the Y axis, but still want to see the label for the axis itself. Am I correct?

You can do that with:

axis1.setTickLabelsVisible(false);

Okay, if you want to:

  • hide the label in the chart
  • but still have it in the NumberAxis

Then there is one solution, that isn't perfect either, that you could use. If you set the "attributed label" (a label with extra font markup attributes), it will draw the attributed label instead. You can set it to a single space (a zero-length string doesn't work - the font rendering code doesn't allow that).

rangeAxis.setAttributedLabel(" ");

At least axis1.getLabel() will still return your old label, but that's the only benefit of this that I can see.

Otherwise, you can subclass NumberAxis and override the method drawLabel in the subclass to do nothing:

protected AxisState drawLabel(String label, Graphics2D g2,
        Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge,
        AxisState state) {
    return state;
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • No, exactly the other way around :) – Jean-Paul Mar 23 '14 at 13:23
  • Two other ways you can do it. The first one isn't great but maybe it satisfies your requirements. The second one is better but now you have to construct and change the RangeAxis in your Plot yourself. Doable, but more code. – Erwin Bolwidt Mar 23 '14 at 13:35
0

My best solution so far is:

axis1.setLabel(null);

But this is just overwriting the original label (so not really a good solution).

Jean-Paul
  • 19,910
  • 9
  • 62
  • 88