9

Anybody that has experience using JFreeChart, is there a way to change the color of my labels for my XY axes. Right now I'm using a XYPlot and I want to change the color of the labels on my axes. Is there a way to do this?

IvanRF
  • 7,115
  • 5
  • 47
  • 71
Albinoswordfish
  • 1,949
  • 7
  • 34
  • 50

2 Answers2

9

You should be able to use setTickLabelPaint() on the desired Axis.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Thank you that answered my question. For anybody else with this problem I got a little stuck with the fact that XYPlot's getDomainAxis() returns a ValueAxis. But I looked at the documentation and realized that Valueaxis is a child class of Axis. – Albinoswordfish Mar 25 '10 at 02:30
  • 1
    Excellent. One nice feature of `JFreeChart` is that the API documents are built with the `linksource` option, so you can navigate by clicking on names. – trashgod Mar 25 '10 at 03:30
  • Is it possible to change the paint color within one label? E.g. first word of the label in black and the second word in gray? – stefanbschneider Feb 13 '15 at 14:26
  • I seem to recall support for `AttributedString`, but I've not tried it. – trashgod Feb 13 '15 at 21:52
3

I used this code to change the color of all my labels:

private void setFontColor(Color fontColor) {
    JFreeChart chart = getChart();
    chart.getTitle().setPaint(fontColor);
    Plot plot = chart.getPlot();
    if (plot instanceof CategoryPlot) {
        setAxisFontColor(((CategoryPlot) plot).getDomainAxis(), fontColor);
        setAxisFontColor(((CategoryPlot) plot).getRangeAxis(), fontColor);
    } else if (plot instanceof XYPlot) {
        setAxisFontColor(((XYPlot) plot).getDomainAxis(), fontColor);
        setAxisFontColor(((XYPlot) plot).getRangeAxis(), fontColor);
    }
}

private void setAxisFontColor(Axis axis, Color fontColor) {
    if (!fontColor.equals(axis.getLabelPaint()))
        axis.setLabelPaint(fontColor);
    if (!fontColor.equals(axis.getTickLabelPaint()))
        axis.setTickLabelPaint(fontColor);
}

If you use subtitles, you need to add them too.

IvanRF
  • 7,115
  • 5
  • 47
  • 71