Example one, pie chart, simple example:
Import jar, code file:
Operation Result:
Code:
Import Org.jfree.chart.ChartFactory;
Import Org.jfree.chart.ChartFrame;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.data.general.DefaultPieDataset;
public class Jfreecharttest
{
public static void Main (string[] args)
{
Defaultpiedataset dpd=new Defaultpiedataset (); Create a default pie chart
Dpd.setvalue ("Management personnel", 25); Input data
Dpd.setvalue ("Market personnel", 25);
Dpd.setvalue ("Developer", 45);
Dpd.setvalue ("Other personnel", 10);
Jfreechart Chart=chartfactory.createpiechart ("a company personnel organization data Graph", dpd,true,true,false);
You can check the specific API documentation, the first parameter is the title, the second parameter is a dataset, the third parameter indicates whether legend is displayed, the fourth parameter indicates whether a hint is displayed, and the fifth parameter indicates whether a URL exists in the graph
Chartframe chartframe=new Chartframe ("a company personnel organization Data Graph", chart);
The chart is placed in the Java container component and Chartframe inherits from the Java JFrame class. The data for the first parameter is placed in the upper-left corner of the window, not in the middle of the title.
Chartframe.pack (); Show graphics at the right size
Chartframe.setvisible (TRUE);//Whether the graphic is visible
}
}
Example two, histogram/histograms
Operation Result:
Code:
Package Com.test.jfreechart;
Import Java.awt.Font;
Import Javax.swing.JPanel;
Import Org.jfree.chart.ChartFactory;
Import Org.jfree.chart.ChartPanel;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.axis.CategoryAxis;
Import Org.jfree.chart.plot.CategoryPlot;
Import org.jfree.chart.plot.PlotOrientation;
Import Org.jfree.chart.title.TextTitle;
Import Org.jfree.data.category.CategoryDataset;
Import Org.jfree.data.category.DefaultCategoryDataset;
Import Org.jfree.ui.ApplicationFrame;
public class JFreeChartTest2 extends Applicationframe
{
Public JFreeChartTest2 (String title)
{
Super (title);
This.setcontentpane (Createpanel ()); The Panel panel that automatically creates Java in the constructor
}
public static Categorydataset CreateDataSet ()//create histogram data set
{
Defaultcategorydataset dataset=new Defaultcategorydataset ();
Dataset.setvalue ("A", "management Staff");
Dataset.setvalue ("B", "market personnel");
Dataset.setvalue (+, "C", "Developer");
Dataset.setvalue ("D", "other personnel");
return dataset;
}
public static Jfreechart Createchart (Categorydataset DataSet)//Create a chart with a dataset
{
Jfreechart chart=chartfactory.createbarchart ("HI", "Personnel distribution",
"Number of personnel", DataSet, Plotorientation.vertical, True, true, false); Create a Jfreechart
Chart.settitle (New Texttitle ("Company Organization Chart", New Font ("Arial", font.bold+font.italic,20));//You can reset the title to replace the "HI" heading
Categoryplot plot= (Categoryplot) Chart.getplot ();//Get the middle part of the icon, which is plot
Categoryaxis Categoryaxis=plot.getdomainaxis ();//Get the horizontal axis
Categoryaxis.setlabelfont (New Font ("Microsoft Jas Black", font.bold,12));//Set Horizontal axis font
return chart;
}
public static JPanel Createpanel ()
{
Jfreechart Chart =createchart (CreateDataSet ());
return new Chartpanel (chart); Put the chart object in the panel pane and the Chartpanel class has inherited JPanel
}
public static void Main (string[] args)
{
JFreeChartTest2 chart=new JFreeChartTest2 ("A company organizational chart");
Chart.pack ();//display at the right size
Chart.setvisible (TRUE);
}
}
Example three, histogram/histograms, save picture file
Code:
Import Java.awt.Font;
Import Java.io.FileOutputStream;
Import Java.io.OutputStream;
Import Org.jfree.chart.ChartFactory;
Import org.jfree.chart.ChartUtilities;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.plot.PiePlot;
Import Org.jfree.chart.title.LegendTitle;
Import Org.jfree.chart.title.TextTitle;
Import Org.jfree.data.general.DefaultPieDataset;
public class JFreeChartTest3
{
public static void Main (string[] args) throws Exception
{
Jfreechart Chart=chartfactory.createpiechart ("a company personnel organization data Graph", GetDataSet (), true,true,false);
Chart.settitle (New Texttitle ("Company Organization Chart", New Font ("Arial", font.bold+font.italic,20));
Legendtitle legend=chart.getlegend (0);//Settings legend
Legend.setitemfont (New Font ("Arial", font.bold,14));
Pieplot plot= (Pieplot) Chart.getplot ();//Set plot
Plot.setlabelfont (New Font ("script", font.bold,16));
OutputStream OS = new FileOutputStream ("Company.jpeg");//Picture is a file format, so use FileOutputStream for output.
Chartutilities.writechartasjpeg (OS, Chart, 1000, 800);
Use a tool class for application to convert the chart to a JPEG-formatted picture. The 3rd parameter is the width, and the 4th parameter is the height.
Os.close ();//Close output stream
}
private static Defaultpiedataset GetDataSet ()
{
Defaultpiedataset dpd=new Defaultpiedataset (); Create a default pie chart
Dpd.setvalue ("Management personnel", 25); Input data
Dpd.setvalue ("Market personnel", 25);
Dpd.setvalue ("Developer", 45);
Dpd.setvalue ("Other personnel", 10);
return DPD;
}
}
After running, refresh the file:
Example four, the JSP page shows the graph generated by the Jfreechart
File structure diagram:
To configure the servlet:
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>
Index.jsp Code:
<%@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030"%>
<%@ page import= "Org.jfree.data.general.defaultpiedataset,org.jfree.chart.chartfactory
, org.jfree.chart.jfreechart,org.jfree.chart.servlet.* "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>insert title here</title>
<body>
<%
Defaultpiedataset DPD = new Defaultpiedataset ();
Dpd.setvalue ("Management personnel", 25);
Dpd.setvalue ("Market personnel", 25);
Dpd.setvalue ("Developer", 45);
Dpd.setvalue ("Other personnel", 10);
Jfreechart chart = Chartfactory.createpiechart ("A company organization Chart", DPD, True, false, false);
String fileName = servletutilities.savechartaspng (chart,800,600,session);
Servletutilities is a web development-oriented tool class that returns a string file name, the file name is automatically generated, and the resulting image is automatically placed in the server (Tomcat) temp file (temp)
String URL = request.getcontextpath () + "/displaychart?filename=" + filename;
According to the file name to the temporary directory to find the image, here the/displaychart path to the configuration file in the user-defined <url-pattern> consistent
%>
</body>
Operation Result:
REF:
Http://www.open-open.com/lib/view/open1365997415828.html
Http://www.cnblogs.com/xingyun/archive/2012/02/05/2339237.html
http://www.jfree.org/jfreechart/
http://sourceforge.net/projects/jfreechart/files/
Examples of using Jfreechart