Recently in the course of data mining design, need to analyze the results of the data is very intuitive to the user, this will need to use the data map, to achieve this function requires a few Third-party packages:
1. Jfreechart-1.0.13.jar
2. Jcommon-1.0.16.jar
3. Gnujaxp.jar
Let's take a look at the final effect chart:
Mainly Jfreechart-1.0.13.jar, but these three packages to complete, I have all the Jfreechart-related jar package with the example of the project (code) compressed together upload, interested students can download,
Download Address: http://download.csdn.net/detail/pzhtpf/4327700
Next, we step into the implementation of this program.
First, the preparatory work, also put these three third-party packages into the project, add the process is particularly simple, the previous blog, is about how to read the data in the Excel table (interested students can take a look at: http://blog.csdn.net/pzhtpf/ article/details/7506135), but also to add a third party package, add the same process, here we are reviewing:
1, build a Java project in this project in the establishment of a new folder Lib;
2, copy the above three jar packages to Lib
3, then right click on this Java project, select Properties
4, select Java build Path in the left list and select libraries on the right
5, click Add JARs
6, then to select the three jars in the Lib folder in this project, click OK
After success, there will be one more folder in the project: referenced libraries
Second, the Java code to implement the column chart:
Import Java.awt.Font;
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.axis.ValueAxis;
Import Org.jfree.chart.plot.CategoryPlot;
Import org.jfree.chart.plot.PlotOrientation;
Import Org.jfree.data.category.CategoryDataset;
Import Org.jfree.data.category.DefaultCategoryDataset;
public class Barchart {Chartpanel frame1;
Public Barchart () {Categorydataset DataSet = GetDataSet ();
Jfreechart chart = Chartfactory.createbarchart3d ("fruit",//Chart title "Fruit Type",///Table of Contents axis display label
"Quantity",///Value Axis Display tab DataSet,//DataSet plotorientation.vertical,//Chart Orientation: horizontal, vertical
True,//whether to display the legend (for a simple bar chart must be false) false,//whether to generate tool false//whether to generate a URL link
); Start Here Categoryplot plot=chart.getcategoryplot ()//Get the chart area object CategoryaxisDomainaxis=plot.getdomainaxis (); Horizontal bottom list Domainaxis.setlabelfont (new Font ("bold", font.bold,14)); Horizontal bottom title Domainaxis.setticklabelfont (New Font ("Song Body", font.bold,12));
Vertical title Valueaxis Rangeaxis=plot.getrangeaxis ();//Get Columnar Rangeaxis.setlabelfont (new Font ("bold", font.bold,15));
Chart.getlegend (). Setitemfont (New Font ("bold", Font.Bold, 15)); Chart.gettitle (). SetFont (New Font ("Arial", font.bold,20))//Set title Font//end here, although the code is a little more, but only for one purpose, solve the problem of Chinese character garbled frame1=new Cha Rtpanel (chart,true); Here you can also use Chartframe to directly generate a standalone frame} private static Categorydataset GetDataSet () {Defaultcategorydataset
DataSet = new Defaultcategorydataset ();
Dataset.addvalue (100, "Beijing", "Apple");
Dataset.addvalue (100, "Shanghai", "Apple");
Dataset.addvalue (100, "Guangzhou", "Apple");
Dataset.addvalue (200, "Beijing", "pear");
Dataset.addvalue (200, "Shanghai", "pear");
Dataset.addvalue (200, "Guangzhou", "pear");
Dataset.addvalue (300, "Beijing", "grapes"); Dataset.addvaluE (300, "Shanghai", "grapes");
Dataset.addvalue (300, "Guangzhou", "Grape");
Dataset.addvalue (400, "Beijing", "banana");
Dataset.addvalue (400, "Shanghai", "banana");
Dataset.addvalue (400, "Guangzhou", "banana");
Dataset.addvalue (500, "Beijing", "Litchi");
Dataset.addvalue (500, "Shanghai", "Litchi");
Dataset.addvalue (500, "Guangzhou", "Litchi");
return dataset;
Public Chartpanel Getchartpanel () {return frame1; }
}
The effect chart is as follows:
But when we change the data in the private static Categorydataset GetDataSet () {} method, we have another effect, such as:
private static Categorydataset GetDataSet () {
Defaultcategorydataset DataSet = new Defaultcategorydataset ();
Dataset.addvalue (100, "Apple", "Apple");
Dataset.addvalue (200, "pear", "pear");
Dataset.addvalue (300, "grapes", "grapes");
Dataset.addvalue (400, "banana", "banana");
Dataset.addvalue (500, "Litchi", "Litchi");
return dataset;
The effect chart is as follows:
Three Java code to implement a pie chart:
Package Com.njue.testJFreeChart;
Import Java.awt.Font;
Import Java.text.DecimalFormat;
Import Java.text.NumberFormat;
Import Javax.swing.JPanel;
Import Org.jfree.chart.ChartFactory;
Import Org.jfree.chart.ChartPanel;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.labels.StandardPieSectionLabelGenerator;
Import Org.jfree.chart.plot.PiePlot;
Import Org.jfree.data.general.DefaultPieDataset;
public class Piechart {Chartpanel frame1;
Public Piechart () {Defaultpiedataset data = GetDataSet ();
Jfreechart chart = Chartfactory.createpiechart3d ("fruit yield", data,true,false,false);
Set percent Pieplot Pieplot = (pieplot) chart.getplot (); DecimalFormat df = new DecimalFormat ("0%");//Get a DecimalFormat object, mainly set a decimal problem numberformat NF = numberformat.getnumb Erinstance ()//Get a NumberFormat object Standardpiesectionlabelgenerator SP1 = new Standardpiesectionlabelgenerator ("{0} { 2} ", NF, df);//Get Standardpiesectionlabelgenerator object Pieplot.setlabElgenerator (SP1)//Set pie chart showing percent///No data displayed when the content pieplot.setnodatamessage ("No data display");
Pieplot.setcircular (FALSE);
Pieplot.setlabelgap (0.02D); Pieplot.setignorenullvalues (true);/setting does not display null value pieplot.setignorezerovalues (TRUE);//setting does not display negative values frame1=new Chartpanel
(chart,true); Chart.gettitle (). SetFont (New Font ("Arial", font.bold,20));//Set Title font Pieplot pieplot= (pieplot) Chart.getplot ();//Get chart area to Like Pieplot.setlabelfont (New font ("Arial", font.bold,10))//Resolve garbled chart.getlegend (). Setitemfont (New Font ("bold", Font.bo
ld,10));
private static Defaultpiedataset GetDataSet () {defaultpiedataset DataSet = new Defaultpiedataset ();
Dataset.setvalue ("Apple", 100);
Dataset.setvalue ("Pear", 200);
Dataset.setvalue ("Grapes", 300);
Dataset.setvalue ("Banana", 400);
Dataset.setvalue ("Litchi", 500);
return dataset;
Public Chartpanel Getchartpanel () {return frame1; }
}
The effect chart is as follows:
Four Java code to implement a line chart:
Package Com.njue.testJFreeChart;
Import Java.awt.Font;
Import Java.text.SimpleDateFormat;
Import Org.jfree.chart.ChartFactory;
Import Org.jfree.chart.ChartPanel;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.axis.DateAxis;
Import Org.jfree.chart.axis.ValueAxis;
Import Org.jfree.chart.plot.XYPlot;
Import Org.jfree.data.time.Month;
Import org.jfree.data.time.TimeSeries;
Import org.jfree.data.time.TimeSeriesCollection;
Import Org.jfree.data.xy.XYDataset;
public class Timeserieschart {Chartpanel frame1;
Public Timeserieschart () {Xydataset xydataset = CreateDataSet (); Jfreechart Jfreechart = Chartfactory.createtimeserieschart ("Legal & General Unit Trust Fund price", "date", "Price", Xydataset, True,
True, true);
Xyplot Xyplot = (xyplot) jfreechart.getplot ();
Dateaxis Dateaxis = (dateaxis) xyplot.getdomainaxis ();
Dateaxis.setdateformatoverride (New SimpleDateFormat ("mmm-yyyy"));
Frame1=new Chartpanel (jfreechart,true); Dateaxis.setlabelfont (nEW Font ("Blackbody", font.bold,14)); Horizontal bottom title Dateaxis.setticklabelfont (New Font ("Song Body", font.bold,12));
Vertical title Valueaxis Rangeaxis=xyplot.getrangeaxis ();//Get Columnar Rangeaxis.setlabelfont (new Font ("bold", font.bold,15));
Jfreechart.getlegend (). Setitemfont (New Font ("bold", Font.Bold, 15)); Jfreechart.gettitle (). SetFont (New Font ("Arial", font.bold,20));//set title font} private static Xydataset CreateDataSet () {/ /This dataset is a bit much, but it's not difficult to understand timeseries timeseries = new TimeSeries ("Legal & General European Index Trust", Org.jfree.data.time .
Month.class);
Timeseries.add (New Month (2, 2001), 181.80000000000001D);
Timeseries.add (New Month (3, 2001), 167.30000000000001D);
Timeseries.add (New Month (4, 2001), 153.80000000000001D);
Timeseries.add (New Month (5, 2001), 167.59999999999999D);
Timeseries.add (New Month (6, 2001), 158.80000000000001D);
Timeseries.add (New Month (7, 2001), 148.30000000000001D); Timeseries.add (New Month (8, 2001), 153.90000000000001D);
Timeseries.add (New Month (9, 2001), 142.69999999999999D);
Timeseries.add (New Month (2001), 123.2D);
Timeseries.add (New Month (One, 2001), 131.80000000000001D);
Timeseries.add (New Month (2001), 139.59999999999999D);
Timeseries.add (New Month (1, 2002), 142.90000000000001D);
Timeseries.add (New Month (2, 2002), 138.69999999999999D);
Timeseries.add (New Month (3, 2002), 137.30000000000001D);
Timeseries.add (New Month (4, 2002), 143.90000000000001D);
Timeseries.add (New Month (5, 2002), 139.80000000000001D);
Timeseries.add (New Month (6, 2002), 137D);
Timeseries.add (New Month (7, 2002), 132.80000000000001D);
TimeSeries timeseries1 = new TimeSeries ("Legal & General UK Index Trust", Org.jfree.data.time.Month.class);
Timeseries1.add (New Month (2, 2001), 129.59999999999999D);
Timeseries1.add (New Month (3, 2001), 123.2D);
Timeseries1.add (New Month (4, 2001), 117.2D); Timeseries1.add (NEW Month (5, 2001), 124.09999999999999D);
Timeseries1.add (New Month (6, 2001), 122.59999999999999D);
Timeseries1.add (New Month (7, 2001), 119.2D);
Timeseries1.add (New Month (8, 2001), 116.5D);
Timeseries1.add (New Month (9, 2001), 112.7D);
Timeseries1.add (New Month (2001), 101.5D);
Timeseries1.add (New Month (One, 2001), 106.09999999999999D);
Timeseries1.add (New Month (2001), 110.3D);
Timeseries1.add (New Month (1, 2002), 111.7D);
Timeseries1.add (New Month (2, 2002), 111D);
Timeseries1.add (New Month (3, 2002), 109.59999999999999D);
Timeseries1.add (New Month (4, 2002), 113.2D);
Timeseries1.add (New Month (5, 2002), 111.59999999999999D);
Timeseries1.add (New Month (6, 2002), 108.8D);
Timeseries1.add (New Month (7, 2002), 101.59999999999999D);
Timeseriescollection timeseriescollection = new Timeseriescollection ();
Timeseriescollection.addseries (timeseries); Timeseriescollection.addserieS (timeseries1);
return timeseriescollection;
Public Chartpanel Getchartpanel () {return frame1; }
}
The effect chart is as follows:
Take another look at the Main method:
Import Java.awt.GridLayout;
Import Javax.swing.JFrame;
public class MainClass {public
static void Main (String args[]) {
JFrame frame=new JFrame ("Java Data Statistics");
Frame.setlayout (New GridLayout (2,2,10,10));
Frame.add (New Barchart (). Getchartpanel ()); Add Column Chart
frame.add (new BarChart1 (). Getchartpanel ()); Add another effect to the column chart
frame.add (new Piechart (). Getchartpanel ()); Add pie chart
frame.add (new Timeserieschart (). Getchartpanel ()); Add Line chart
frame.setbounds (m, M, N);
Frame.setvisible (true);
Five summary
Above all is a simple example to realize, want to understand deeper students can query data, in fact, the above code is easy to understand, as long as the combination of their actual situation, you can develop their own application, you can see that I am here on the application to achieve, As a matter of fact, more data statistics are used in javaweb and we can understand them by ourselves.