Wednesday, September 30, 2009

JFreeChart LegendTitle Font Change

I've been trying to search the whole day yesterday on how to change the Font for a chart's legend title. JFreeChart doesn't have any good free reference documentation about this use case but I was finally able to figure it out by playing with LegendTitle's methods. The code below works for changing the Font of the current LegendTitle object.

LegendTitle legendTitle = chart.getLegend();
legendTitle.setItemFont(font);
legendTitle.setNotify(true);

The setNotify() method somehow alerts the different objects dependent on the LegentTitle and update it's state. If that is not called, nothing happens. If that method is set to true that means, the current change on the chart, specifically the LegentTitle, call all dependent objects and notify of this change and properly act on it.

3 comments:

  1. That's odd. Are you using the latest version of JFreeChart? There was a bug (1749124 at SourceForge) that would cause this, but it was fixed in 2007. If you are seeing this problem in JFreeChart 1.0.13 it would be really great if you could e-mail me the steps to reproduce it.

    Best regards,

    Dave Gilbert
    JFreeChart Project Leader

    ReplyDelete
    Replies
    1. Can i Hyperlink to the legend items in a jfree pie chart.
      I am using jfreechart 1.0.14

      Delete
  2. Hi David. I'm using the JFreeChart 1.0.10 version. Was this the version you are referring to that was fixed in 2007?

    Please see the steps below to reproduce the issue.

    1. I created a JFreeChart of type Bar Chart object using the ChartFactory.createBarChart method. Show legend set to true.

    2. Set properties of chart instance.
    3. Set CategoryPlot parameters.
    4. Set the properties for the BarRenderer
    5. Set CategoryAxis properties.
    6. Set NumberAxis properties.
    7. Get the LegendTitle from chart instance and set the ItemFont to a Font object that's of name MS UI Gothic, style is PLAIN of size 12.
    8. Generate the image.

    ReplyDelete