In the recent course design of data mining, it is necessary to display the results of data analysis very intuitively to the user, which is to use the data chart, 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
Take a look first, finally:
Mainly Jfreechart-1.0.13.jar, but the three packages to complete, I have all the jfreechart -related jar package with the example of this article project (code) to compress and upload, Interested students are able to download,
: http://download.csdn.net/detail/pzhtpf/4327700
Next, we step-by-step to implement this program.
First, the preparatory work, also put these three third-party package into this article, the join process is particularly simple, the previous written a blog, is about how Java read Excel The data in the table (interested students can take a look:http://blog.csdn.net/pzhtpf/article/details/7506135), but also to join the third party package, the accession process is identical, Here we are reviewing again:
1, built the Java Project, in this project in the establishment of a new directory lib;
2, Copy the above three jar packages to Lib
3 , then right-click on this Java Project and select Properties
4 , in the left -hand list, select Java Build Path, right side selected libraries
5 , Click ADD JARs
6, then go to select three jars in the Lib directory for This project, click OK
after success, there will be one more directory in the project: Referenced Libraries
Two, Java Code to implement a 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", Folder axis display label "Quantity",//value axis display label DataSet,//DataSet Plotorientation.vertical,//Chart direction: Horizontal, vertical true,//Whether the legend is displayed (must be false for a simple histogram) False,//whether the build tool is false//generates a URL link ); Start Here Categoryplot plot=chart.getcategoryplot ();//Get the chart area object Categoryaxis domainaxis=plot.getd Omainaxis (); Horizontal bottom list Domainaxis.setlabelfont (new Font ("Blackbody", font.bold,14)); Horizontal Bottom Header Domainaxis.setticklabelfont (new Font ("Arial", font.bold,12)); Vertical caption Valueaxis Rangeaxis=plot.getrangeaxis ();//Get Columnar Rangeaxis.setlabelfont (new Font ("Blackbody", font.bold,15)); Chart.getlegend (). Setitemfont (New Font ("Blackbody", Font.Bold, 15)); Chart.gettitle (). SetFont (New Font ("Arial", font.bold,20));//Set Title Font//end here, although the code is a bit more, but only for one purpose, solve the problem of Chinese characters garbled Frame1=new Chartpanel (chart,true); You can also use Chartframe to generate a standalone frame} private static Categorydataset GetDataSet () {Defaultcategorydat Aset 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", "grape"); Dataset.addvalue (300, "Shanghai", "Grape"); 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;}}
For example, the following:
But when we change the data in the private static categorydataset getdataset () {} method, there is another effect, Let's say we change to:
private static Categorydataset GetDataSet () { Defaultcategorydataset DataSet = new Defaultcategorydataset (); Dataset.addvalue (100, "Apple", "Apple"); Dataset.addvalue (200, "Pears", "pears"); Dataset.addvalue (300, "grape", "grape"); Dataset.addvalue (400, "banana", "banana"); Dataset.addvalue (500, "Litchi", "Litchi"); return dataset;}
For example, the following:
Three, Java Code that implements the 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 percentage Pieplot Pieplot = (pieplot) chart.getplot (); DecimalFormat df = new DecimalFormat ("0%");//Get a DecimalFormat object, mostly set decimal problem numberformat NF = Numberformat.getnumbe Rinstance ();//Get a NumberFormat object Standardpiesectionlabelgenerator SP1 = new Standardpiesectionlabelgenerator ("{0} {2 } ", NF, df);//Get Standardpiesectionlabelgenerator Object Pieplot.setlabelgenerator (SP1);//Set the pie Chart display percentage//When no data is displayedContent 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 show negative values frame1=new Chartpanel (ch Art,true); Chart.gettitle (). SetFont (New Font ("Arial", font.bold,20));//Set Caption Font Pieplot pieplot= (pieplot) Chart.getplot ();// Gets the chart area object Pieplot.setlabelfont (New font ("Arial", font.bold,10));//Address garbled chart.getlegend (). Setitemfont ("bold", Fo Nt. bold,10));} private static Defaultpiedataset GetDataSet () {defaultpiedataset DataSet = new Defaultpiedataset (); Dataset.setvalue ("Apple", 100); Dataset.setvalue ("Pear", 200); Dataset.setvalue ("Grape", 300); Dataset.setvalue ("Banana", 400); Dataset.setvalue ("Litchi", 500); return dataset;} Public Chartpanel Getchartpanel () {return frame1; }}
For example, the following:
Four, to implement the Java code for 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 ();D ateaxis 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 HeaderDateaxis.setticklabelfont (New Font ("Arial", font.bold,12)); Vertical caption Valueaxis Rangeaxis=xyplot.getrangeaxis ();//Get Columnar Rangeaxis.setlabelfont (new Font ("Blackbody", font.bold,15)); Jfreechart.getlegend (). Setitemfont (New Font ("Blackbody", Font.Bold, 15)); Jfreechart.gettitle (). SetFont (New Font ("Arial", font.bold,20));//Set caption Font} private static Xydataset CreateDataSet () {// This dataset is a bit more, but it's easy to understand TimeSeries TimeSeries = new TimeSeries ("Legal & General European Index Trust", ORG.JFREE.DATA.T Ime. 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; }}
For example, the following:
Take a second 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 Chart") Frame.setlayout (new GridLayout (2,2,10,10)); Frame.add (new Barchart (). Getchartpanel ()); Added Column chart frame.add (new BarChart1 (). Getchartpanel ()); Adding a Column chart also has an effect Frame.add (new Piechart (). Getchartpanel ()); Added pie chart frame.add (new Timeserieschart (). Getchartpanel ()); Add Line chart Frame.setbounds (+, +, +); Frame.setvisible (True);}}
Five, Summary
These are a simple example to realize, want to know more students can self-query information, in fact, the above code is easy to understand, just to combine their own actual situation, you can develop their own application, we can see that I am here in Application , in fact, a lot of other cases of data statistics on the javaweb application of a lot of other, we can also know for themselves.
PS: If you are performing an error in this article project, please refer to Kauboven: http://blog.csdn.net/pzhtpf/article/details/7506135
Java implementation of various data charts (column chart, pie chart, line chart)