JFreechart chart setBorderPaint Java Example

Up until now, we know how to create a JFreechart bar chart or pie chart. We got the basic examples right. In this tutorial, we will see how to customize the bar chart we created. A chart not only contains the core diagram, it also has legend, border, data table to name a few. And you may have requirements to customize them to suit to your project needs. In this tutorial we will focus specifically on chart borders. I have split the example into three parts, and will use the bar chart created earlier for this exercise.

Note: The base code for this tutorial is taken from our JDBC Bar Chart Example tutorial. We will provide only the sections you need to change to make the examples below to work. Make sure you grab a copy of the code first before testing the examples below.

Working with Chart Borders

JFreeChart offers quite a lot of methods to work with chart borders. We can functionally categorize them into the following groups:

Control border visibility on a Chart
Specify a chart border color
Specify a chart border stroke

These functionalities can be achieved by the methods provided in org.jfree.chart.JFreeChart class. We will discuss each of these methods now, with suitable examples.

Controlling the chart border color

You can use the method setBorderVisible to control the border of the chart. By default, the chart does not have any borders. You can turn the border on for the chart by passing “true” to this method. I found that just setting a value of “true” to this method does not really create any difference, so you need to use this method along with different methods that control border on the chart. As an example, we will use setBorderPaint method after setting the border to a blue color.  The example code that sets the border color to blue is provided below:
                JFreeChart BarChartObject=ChartFactory.createBarChart("JFreeChart - Chart Border Example","Subject","Marks",my_bar_chart_dataset,PlotOrientation.VERTICAL,true,true,false);
                Boolean chart_border_visible=true;
                BarChartObject.setBorderVisible(chart_border_visible); // sets the border to visible
                BarChartObject.setBorderPaint(Color.BLUE); //sets border color to BLUE

This example produces the following output: ( I have not shown the full chart, but you can notice that the chart border is turned to blue color now)
JFreeChart - Chart Border Color - Example
JFreeChart - Chart Border Color - Example

Specify a RGB value as border color


Note that we have used java.awt.Color method, to specify the color for our chart border. That opens up the possibility for us to use any color as a chart border, apart from the standard color. If you know the RGB values for the chart color, you can provide it to create your border. An example code for this is shown below: (sets color to RED)
                BarChartObject.setBorderPaint(new Color(255,0,0)); //sets border color to RED


Check if border is visible or not


If you are modifying an existing chart and needs to know if the border is available or not, you can use the method isBorderVisible to find out if the border is visible or not. This methods returns true if border is visible, false otherwise. An example code is provided below:
                System.out.println(BarChartObject.isBorderVisible()); //Is the chart border visible?

Specify a Gradient / Texture Border for the Chart

This does not end here, if you want to specify a Gradient color for chart border, you can use the class java.awt.GradientPaint and pass this information into the setBorderPaint method. You can also specify a texture as the border for your chart by using java.awt.TexturePaint class. As the border we have for the chart is slim, we cannot see the Gradient and Texture patterns in live. Once we cover the chart border stroke feature, we can see how to set these options. Stay connected for the next part of this tutorial.

No comments:

Post a Comment