For the examples in this tutorial, we are going to create two charts, one Bar chart and one Pie chart. Before continuing to the rest of the tutorial, you will need to add the JFreeChart library (jfreechart-1.0.16.jar) and JCommons library (jcommon-1.0.21.jar) to the Java Build Path of Eclipse (Project->Properties->Java Build Path->Libraries->Add External Jars...).
When you create a Bar chart or Pie chart, there are three steps that should be undertaken. The first step is creating the dataset for the chart. The second step creates the chart object that wraps the dataset and the last step shows the chart on the frame window. For the Bar chart, you can construct the dataset by using the DefaultCategoryDataset. This class has a method called setValue that can be used to set the values, row keys, and column keys to the chart.
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
To create a Bar chart, you will use the createBarChart or createBarChart3D method of the ChartFactory class. The createBarChart method allows you to create a simple Bar chart with 2D effect. If you want the Bar chart with 3D effect, you will use the createBarChart3D method. With these methods, you can specify the chart title, category label, value label, and dataset.
JFreeChart barchart=ChartFactory.createBarChart("People used Facebook in Cambodia", "Year", "Number", dataset);
After the chart object is created, it is ready to show on the frame window. It is easy. The ChartFrame class allows you to create a frame window. You need to provide the frame title, and the chart object that you created in the second step. Then call the pack and setVisible methods to show the window.
JFrame frame = new ChartFrame("Bar chart", barchart);
Here is the complete program to construct a Bar chart with.
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
public class PieChart {
public static void main(String[] args) {
createBarChart();
}
public static void createBarChart(){
//create dataset for Bar chart
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
dataset.setValue(new Integer(1000),"Men","2000");
dataset.setValue(new Integer(3000),"Women","2000");
dataset.setValue(new Integer(7000),"Men","2001");
dataset.setValue(new Integer(15000),"Women","2001");
//create Bar chart
JFreeChart barchart=ChartFactory.createBarChart("People used Facebook in Cambodia", "Year", "Number", dataset);
//show the chart
JFrame frame = new ChartFrame("Bar chart", barchart);
frame.pack();
frame.setVisible(true);
}
}
For a Pie chart, you will use the DefaultPieDataset to create the dataset object for the Pie chart. This class also has the setValue method that you can set the value for each category of the chart. The createPieChart method of the ChartFactory class allows you to create a Pie chart with 2D effect. If you like 3D effect, you can use the createPieChart3D instead.
public static void createPieChart(){
// create a dataset for pie chart
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Men", new Integer(1200));
dataset.setValue("Women", new Integer(500));
dataset.setValue("Children", new Integer(1000));
// create a pie chart by using ChartFactory
JFreeChart piechart = ChartFactory.createPieChart3D("Pie chart", dataset,true,true,false);
// create and display a frame...
JFrame frame = new ChartFrame("Test", piechart);
frame.pack();
frame.setVisible(true);
}
The JFreeChart library allows you to save the chart objects in PNG image format, print them to the default printer, set chart properties (title, font, color, ...) or copy the chart to other applications easily without writing a single line of code. You just right-click on the chart then select a task from the popup menu.
If you want to save the chart in PDF file format, you will do more code. Don't worrry. It is not hard. The JFreeChart class allows you create BufferedImage object from the chart object by using its createBufferedImage method. When you get the BufferedImage object, you can convert it to an image object and write this image object to the PDF output file by using the iText library. You need add the iText library to the Java Build Path as you did with the JFreeChart library.
public static void saveToPDF(JFreeChart chart){
Document doc=new Document();
try {
PdfWriter.getInstance(doc,new FileOutputStream("d:/chart.pdf"));
doc.open();
//store the image data in memory
ByteArrayOutputStream bas=new ByteArrayOutputStream();
ImageIO.write(chart.createBufferedImage(500, 400), "png", bas);
//create image from the image data stored in memory
Image img=Image.getInstance(bas.toByteArray());
//add the chart image on the pdf page
doc.add(img);
} catch (Exception e) {
e.printStackTrace();
} finally{
if(doc!=null)
doc.close();
}
}
I like your articles but for me too advance tips...
ReplyDeleteI am glad to read this, very succintly and up to the point. Great work! I am waiting for your new posts!
ReplyDeleteTry some great templates http://www.pptstar.com/diagrams/pie-charts/ for your charts! I use them to make these pie charts for my blog.
ReplyDeleteNice post. I was checking constantly this blog and I'm impressed! Very useful info specially the last part :) I care for such information a lot. I was looking for this certain information for a very long time. Thank you and best of luck. credible web designer firms
ReplyDeleteWonderful blog! I found it while browsing on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Many thanks. grow a business
ReplyDelete
ReplyDeleteI'm still learning from you, as I'm trying to reach my goals. I certainly love reading everything that is posted on your site.Keep the information coming. I enjoyed it! buy energy drink online