Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing color of labels in JFreeChart

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?

like image 335
Albinoswordfish Avatar asked Mar 23 '10 19:03

Albinoswordfish


2 Answers

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

like image 65
trashgod Avatar answered Oct 22 '22 08:10

trashgod


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.

like image 39
IvanRF Avatar answered Oct 22 '22 08:10

IvanRF