JFreeChart Chart Title Bold Font Color Example

In the last tutorial, we discussed briefly on some of the chart formatting options in JFreeChart, that included underline / strikethrough of chart title. In this tutorial, we will focus on some more chart title formatting options. Precisely, we will be looking at the following chart title options:

1) Making Chart title bold
2) Changing the chart title foreground color
3) Changing the chart title background color
4) Modifying chart title posture

This is an extension of the previous tutorial. Please make sure you read that first before reading on. The previous tutorial describes some theory if you are keen to understand how different formatters work on the JFreechart library.

JFreeChart – Making Chart Title Bold


We have already seen how to bold chart title in JFreeChart. Here we describe one other way to make the chart title bold by using a different constructor in java.awt.Font class.  An example code fragment and the chart produced by the example is given below:
Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
map.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
TextTitle my_Chart_title=new TextTitle("Chart Title  Bold Title Font Example", new Font (map));
BarChartObject.setTitle(my_Chart_title);

JFreeChart-WEIGHT_BOLD-Example Output
JFreeChart-WEIGHT_BOLD-Example Output
Instead of specifying WEIGHT_BOLD, you can also specify the following, WEIGHT_DEMIBOLD, and this produces an output as provided below (this is a moderately ligher weight that WEIGHT_BOLD)
Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
map.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
TextTitle my_Chart_title=new TextTitle("Chart Title  DemiBold Title Font Example", new Font (map));
BarChartObject.setTitle(my_Chart_title);

JFreeChart - WEIGHT_DEMIBOLD-Example Output
JFreeChart - WEIGHT_DEMIBOLD-Example Output
If you want the heaviest bold value, you have to use WEIGHT_ULTRABOLD, an example is provided below:

JFreeChart - WEIGHT_ULTRABOLD-Example
JFreeChart - WEIGHT_ULTRABOLD-Example
There are also other options like WEIGHT_REGULAR, WEIGHT_MEDIUM, WEIGHT_LIGHT, WEIGHT_HEAVY, WEIGHT_EXTRABOLD, WEIGHT_EXTRA_LIGHT ..more than what you would require to set the weight on a chart title. You can try out all these options against your chart to see how it works.


JFreechart - Chart foreground and background colors on Title 


This one is interesting. You can change the chart title foreground and background colors by using FOREGROUND and BACKGROUND properties of TextAttribute class respectively. As an example, we are providing a code that sets chart title background to red, and foreground to white color.  The color you pass into the chart is of type java.awt.Paint. You can virtually set any color as your foreground and background.Code fragment and output is provided below:
Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
map.put(TextAttribute.BACKGROUND, Color.RED);
map.put(TextAttribute.FOREGROUND, Color.WHITE);
TextTitle my_Chart_title=new TextTitle("Chart Foreground and Background Title - Example", new Font (map));
BarChartObject.setTitle(my_Chart_title);


JFreeChart - Chart Title Foreground and background colors - Example
JFreeChart - Chart Foreground and background colors - Example

JFreeChart – chart posture example


You can change the chart title posture by using POSTURE property in TextAttribute class. Here is a code fragment that sets the posture to OBLIQUE, a sample output is also provided for this example.

Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
map.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
TextTitle my_Chart_title=new TextTitle("Chart Title Posture Example", new Font (map));
BarChartObject.setTitle(my_Chart_title);

JFreeChart - Chart Title Posture Example
JFreeChart - Chart Title Posture Example

That completes our third set of formatting options tutorial. We covered making chart title bold (with different options), changing chart foreground and background colors and changing chart posture in this post. We will cover some more formatting options in the next post. Stay connected.

No comments:

Post a Comment