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?
Changing color of labels in JFreeChart
Asked Answered
You should be able to use setTickLabelPaint()
on the desired Axis
.
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 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.
© 2022 - 2024 — McMap. All rights reserved.