Java implementation of a variety of data statistics (column chart, pie chart, line chart)

Source: Internet
Author: User
Tags pear

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

two, to implement the column chart Java code:

  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 species
                            Class ",///Table of Contents axis display label" Quantity ",///value axis display label DataSet,//DataSet Plotorientation.vertical,//Chart Orientation: Horizontal, vertical true,//Whether the legend is displayed (for a simple bar chart must be false
  False,//whether to generate tool false//whether URL link is generated                          );          Starting from here Categoryplot Plot=chart.getcategoryplot ()//Gets the chart area object Categoryaxis Domainaxis=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 caption 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 characters garbled        Frame1=new Chartpanel (chart,true); Here you can also use Chartframe to directly generate a standalone frame} private static Categorydataset GetDataSet () {Defaultcateg
           Orydataset 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:

Third, the Java code to implement 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.getnum   Berinstance ()//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.
	bold,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, to implement the line chart Java code:

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 ("bold", font.)bold,14));  Horizontal bottom title Dateaxis.setticklabelfont (New Font ("Song Body", font.bold,12)); Vertical caption 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 The number is a bit much, but it's not difficult to understand timeseries timeseries = new TimeSeries ("Legal & General European Index Trust", Org.jfree.dat
	        A.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.

PS: If you run this project is an error, please refer to Bowen: http://blog.csdn.net/pzhtpf/article/details/7506135

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.