Search code examples
jfreechart

JFreeChart different fonts or font sizes in title


Is it possible to use different fonts or font sizes in the title of a JFreeChart object?

Using:

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

allows one to set the font but I am trying to create a bold, large font-size title followed by an explanatory sub-title with smaller font size on the line below.

E.g. something like: "Intensive Care Infections / data from 2008-01-01 up to 2012-12-31"

Any other way to achieve this effect, if not possible in the title object?


Solution

  • You can add one or many subtitles using the addSubtitle() method in the JFreeChart class. The title is always drawn first, then the subtitles are drawn (by index order). Note that the legend is added to the chart as a "subtitle", so depending on the position you may want to insert your real subtitle at index 0 so that it is positioned and drawn before the legend.