Search code examples
javaswingjfreechart

JFreeChart - wrong chart font


I have a problem with XYLineChart. I don't know how to set the chart font to look like Swing components. When I use this:

chart.setTitle(new TextTitle("Tahoma title, style plain, size 11", new Font("Tahoma", Font.PLAIN, 11))); 

It is still wrong :(

EDIT: When I create the chart in a new frame, the font in the title is good. How do I set all the labels, axis titles, and other texts to the same font size, without bold?

SOLVED :)

public static void changeStyle(JFreeChart chart) {
    final StandardChartTheme chartTheme = (StandardChartTheme)StandardChartTheme.createJFreeTheme();
    final Font font = new Font("Tahoma", Font.PLAIN, 11);
    final Color color = new Color(0, 0, 0);

    chartTheme.setExtraLargeFont(font);
    chartTheme.setLargeFont(font);
    chartTheme.setRegularFont(font);
    chartTheme.setSmallFont(font);

    chartTheme.setAxisLabelPaint(color);
    chartTheme.setLegendItemPaint(color);
    chartTheme.setItemLabelPaint(color);
    chartTheme.apply(chart);
}

Solution

  • If you want to change the existing title's font, do something like this:

    chart.getTitle().setFont(new Font("Tahoma", Font.PLAIN, 11));
    

    Addendum:

    How do I change all items' fonts, e.g. labels, axis, etc.?

    StandardChartTheme offers this capability by operating on individual chart components.