JFreeChart Chart Title Underline StrikeThrough Example

We saw a quick introduction to chart title formatting options in the previous example. This tutorial is an extension of the previous one where we will cover more advanced chart title formatting options. We will discuss various options that are available in JFreechart for decorating your chart title with examples. It is good to learn that these customization options do exist for us on the chart, and keep these tricks in your backpack for ready usage. As per the earlier exercise, we are not going to provide full code program and you need to get it from our JDBC tutorial. The JAR files you require to run this example are unchanged from the previous one.

The base for this tutorial is from the setTitle method offered by org.jfree.chart.JFreeChart class, which can take an input of the type org.jfree.chart.title.TextTitle. You can send in a object of type java.awt.Font when you create a TextTitle object. That opens up a very wide range of formatting options that you can apply to the chart title.We will cover a few of them with code fragments in this section.

java.awt.Font - Format options

There exists a constructor in this class that can take a map of Font attributes. [ that belongs to java.awt.font.TextAttribute ] . We can set this map with the format options, that will in turn get applied to our chart. You will need to add the following import declarations to the class to get this working:
import java.awt.Font;
import java.util.Map;
import java.util.Hashtable;
import org.jfree.chart.title.TextTitle;
import java.awt.font.TextAttribute;
import java.awt.Color;

JFreeChart - How to underline chart title?

Use the following code fragment along with the main chart program, to underline the chart title.

                Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
                map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
                TextTitle my_Chart_title=new TextTitle("Chart Title - Underline Example", new Font (map));

This program produces a chart with title of the chart underlined. The output is shown below:

JFreeChart - Chart Title - Underline - Example
JFreeChart - Chart Title - Underline - Example
Different Underline Options

Instead of passing UNDERLINE_ON, you can pass the following also to get the outcome as listed below:

  • UNDERLINE_LOW_DASHED : chart underline of single pixel, dashed low.
  • UNDERLINE_LOW_DOTTED: dotted underline of chart title.
  • UNDERLINE_LOW_GRAY: Double pixel underline in the heading
You can also use UNDERLINE_LOW_ONE_PIXEL or UNDERLINE_LOW_TWO_PIXEL to get a single or double pixel solid underline output respectively.

JFreeChart - How to strike-through chart title?

If you want to strike-through the title of your chart (don't know why you want to do this), you can follow the code fragment listed below:

                Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
                map.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
                TextTitle my_Chart_title=new TextTitle("Chart Title  Strikethrough Example", new Font (map));

This code produces a chart title with strike through effect, as shown below

JFreeChart - Chart Title - Strike through Example
JFreeChart - Chart Title - Strike through Example
By default, strikethrough will be off. You can turn it off by using the code above if required.

That covers some of the formatting options available for chart title in JFreechart. In the next tutorial, we will cover some more examples of chart formatting that includes setting / changing kerning, posture, foreground color and background color. Stay connected for more on this shortly.

No comments:

Post a Comment