Our third example in the article covered how to use JFreeChart in a Swing-based application and rendered a bar chart for the example. In this blog, I show how easy it is to modify that example to use gradients.

The basic Swing class used to display the JFreeChart-generated bar chart is shown here:

As shown in this Swing-based class, the HrChartCreator class is used for generating the bar chart. Specifically, the createSalaryPerFinanceEmployeeBarChart method is called on that class. The code listing for that method as used in the article example is shown next:

In the example in the article, Figure 7 shows a snapshot of the Swing application with the JFreeChart-generated bar chart. The bars in the bar chart use the default first color (red) for the bars and the bars are solid in color. A gradient could be used here to make the chart a little more interesting and possible even a little more aesthetically pleasing.

Only a few additional lines are needed to add gradients to the bars in this chart. Had we been using a CategoryPlot for other purposes anyway, even fewer individual lines would be needed because we would have already had access to the CategoryPlot and possibly to the BarRenderer. The code listing below shows the method with gradients added. The newly added lines for gradient support are highlighted.