JFreeChart Chart Title setTitle Example Program

In this tutorial, we will explain how to add chart title to a chart using JFreechart, using example Java code. You may be wondering, what is there in a chart title. In plain simple language, it is a string added to JFreechart object. However, there is a wide range of customization options available for the chart title. You many not have a real requirement to customize chart title, but this exercise will give you a head start if you really want to do one. It is always good to know that such options are available for your chart, thanks to JFreechart, a very powerful library.

I'm going to split this tutorial into multiple parts and focus on two key options on chart title in this post. As we move on, I will be taking some complex customization options like vertical alignment of title etc. By default, a chart title is positioned at the top of the chart, in the middle. (we will cover alignment options also later). To execute the examples that I'm going to provide here, you will need the following JAR files:

  • jfreechart-1.0.14.jar
  • jcommon-1.0.17.jar
  • ojdbc6.jar
  • Base code from the tutorial, JDBC JFreeChart Example. We will discuss only modifications required.

JFreeChart - Available Methods to Modify Chart Title

If you refer to the API documentation, there are two methods available in org.jfree.chart.JFreeChart to modify chart title. Both methods carry the same name, setTitle. This method can take a plain string as input or an object of type org.jfree.chart.title.TextTitle (to be discussed in depth later). Let us discuss two options in this tutorial viz using plain string as title and specifying a title with a fixed font.

Plain string as Chart title

This is the most simple and basic approach. You call setTitle method directly be passing a string. The code example snippet is given below:

/* Modifying chart title in JFreeChart Examples*/
BarChartObject.setTitle("Title modified by setTitle method ");
/* Modifying chart title in JFreeChart Examples*/

The output chart produced by this approach is given below:
JFreeChart - setTitle Method - Example Output
JFreeChart - setTitle Method - Example Output

Specify a different font for Chart title

Now, the interesting part. How to specify a different font for Chart title? We need to set the specifications in the object org.jfree.chart.title.TextTitle and pass this object to setTitle method. The TextTitle class has multiple constructors. One of the constructor accepts chart tile as a string, and a font object of type java.awt.font. Wow, using java.awt.font opens up a plethora of customization options on the chart title for us. We are going to discuss all of them later. Simple things first. Here is an example to set chart title font to Verdana with size 17.
                TextTitle my_Chart_title=new TextTitle("Title With new Font", new Font ("Verdana", Font.PLAIN , 17));
                BarChartObject.setTitle(my_Chart_title);

We pass a Font object and specify three parameters. Type of font, which is a string and we say it is Verdana, style of the font - which is set to PLAIN and size of the font - which we set as 17. Simple. The output chart produced by this method is given below.

JFreeChart - Chart Title with Custom Font Example
JFreeChart - Chart Title with Custom Font Example

That closes this part of the tutorial. It is not done yet. In the next part of this tutorial, we are going to discuss how to customize the title even further. Make the title bold, italic and so on. The easiest way to make sure you don't miss a tutorial is to subscribe to our blog. Stay connected for another example, on your way soon.




No comments:

Post a Comment