JFreeChart Chart Width Superscript Kerning Example

This is the last set in the tutorial series on formatting chart titles in JFreeChart API using Java. We have been discussing most of the formatting options in length so far, and we will cover a few other things in this tutorial. The options we will be covering in this tutorial are listed below:

1) Extending the width of Chart Title
2) Superscript based chart titles
3) Kerning of chart title text
4) Combining multiple attributes into Chart title


Extended width for Chart title


You can have an extended width on the chart title by using the attribute WIDTH with a value WIDTH_EXTENDED. An example code and the output you get on the chart title in this case is provided below

Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
map.put(TextAttribute.WIDTH, TextAttribute.WIDTH_EXTENDED);
TextTitle my_Chart_title=new TextTitle("Chart Title with Extended Width", new Font (map));
BarChartObject.setTitle(my_Chart_title);

JFreeChart - Chart Title-Extended Width - Example Output
JFreeChart - Chart Title-Extended Width - Example Output
And, WIDTH_CONDENSED on the same line will give you a condensed version of the title. Here is a code fragment and output;

Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
map.put(TextAttribute.WIDTH, TextAttribute.WIDTH_CONDENSED);
TextTitle my_Chart_title=new TextTitle("Chart Title with Condensed Width", new Font (map));
BarChartObject.setTitle(my_Chart_title);

JFreeChart - Chart Title - Condensed Width - Example
JFreeChart - Chart Title - Condensed Width - Example

There are also options like SEMI_EXTENDED and SEMI_CONDENSED that provide a semi expanded and condensed version of the chart title respectively. Example code fragment and output are shown below
Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
map.put(TextAttribute.WIDTH, TextAttribute.WIDTH_SEMI_EXTENDED );
TextTitle my_Chart_title=new TextTitle("Chart Title with semi extended Width", new Font (map));
BarChartObject.setTitle(my_Chart_title);

JFreeChart - Chart Title - Semi Extended - Example
JFreeChart - Chart Title - Semi Extended - Example

Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
map.put(TextAttribute.WIDTH, TextAttribute.WIDTH_SEMI_CONDENSED );
TextTitle my_Chart_title=new TextTitle("Chart Title with semi condensed Width", new Font (map));
BarChartObject.setTitle(my_Chart_title);

JFreeChart - Chart Title - Semi Condensed width - Example
JFreeChart - Chart Title - Semi Condensed width - Example

Chart Title SuperScript Example


You can set the title to be of superscript type by using the code fragment below:
Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
map.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER);
TextTitle my_Chart_title=new TextTitle("Chart Title - Superscript example", new Font (map));
BarChartObject.setTitle(my_Chart_title);

You use SUPERSCRIPT property with a value of SUPERSCRIPT_SUPER for this purpose. A sample output is shown below

JFreeChart - Chart Title - Superscript Example
JFreeChart - Chart Title - Superscript Example

The core Java API is so powerful that the options are virtually unlimited. You can also combine multiple options instead of specifying only one at a time. A complete set of attributes that you can use to format your chart title is available in the Oracle Documentation.As a last leg to this tutorial series, we will provide an example to set kerning on the font. You can use the code as below for that. Output with and without kerning applied is shown below

Chart Title - Kerning Example


/* Modifying chart title in JFreeChart Examples*/               
Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
map.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
map.put(TextAttribute.SIZE, new Double("19.0"));
TextTitle my_Chart_title=new TextTitle("Chart Title - Kerning and Size example", new Font (map));
BarChartObject.setTitle(my_Chart_title);
  /* Modifying chart title in JFreeChart Examples*/

This example also illustrates how to set multiple font attributes against the title. We have set both SIZE and Kerning attributes in this case. The output produced with kerning is shown below:

JFreeChart - Kerning Example (Kerning ON)
JFreeChart - Kerning Example (Kerning ON)
The same code with Kerning turned off is provided below:

Chart Title without Kerning
Chart Title without Kerning
If you have a specific requirement towards chart title, that you want us to cover, post it in the comments section of this post. We will have a look.

No comments:

Post a Comment