Changing color of labels in JFreeChart
Asked Answered
A

2

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?

Acanthocephalan answered 23/3, 2010 at 19:37 Comment(0)
N
10

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

Negus answered 24/3, 2010 at 2:37 Comment(4)
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.Acanthocephalan
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.Negus
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?Pyrology
I seem to recall support for AttributedString, but I've not tried it.Negus
P
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.

Paraglider answered 4/5, 2016 at 23:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.