JFreeChart Pie Chart - JFreeChart

What is the use of pie chart in JFreecharts?

In a pie chart, arc length of each sector is proportional to the quantity which it represents. This chapter will explain you how to use JFreeChart for creating Pie Chart from a given set of business data.

Business data

Below example depicts mobile sale with the help of a pie chart. Below is a list of different mobile brands and their sale (units per day).

S.No

Mobile Brands

Sales (UNITS per day)

1

Iphone 5S

20

2

Samsung Grand

20

3

MOTO G

40

4

Nokia Lumia

10

AWT Based Application

Below is the code for creating a Pie Chart by using the above given information This code will help you to embed a pie chart in any AWT based application.

Let’s place the above Java code in PieChart_AWT.java file, and then compile and run it from the command prompted as

If everything works fine, it will compile and run to generate below Pie Graph

pie_chart.jpg

If you don’t want to embed your chart in any application, then you can create chart images in the command prompt. JFreeChart allows saving chart images in either JPG or PNG formats.

JPEG Image Creation

Let’s re-write the above example to generate a JPEG image from a command line. Below are the two APIs provided by JFreeChart library, which can be used for generating either PNG or JPEG image based on your requirement.

  • saveChartAsPNG() − API to save image in PNG format.
  • saveChartAsJPEG() − API to save image in JPEG format.

keep the above Java code in PieChart.java file, and then compile and run it from the command prompted as

If everything is working fine, it will compile and run to create a JPEG image file named PieChart.jpeg in your current directory.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JFreeChart Topics