Java implements various data statistical charts (column chart, pie chart, and line chart)

Source: Internet
Author: User
Tags pears

Recently, in the Course Design of Data Mining, We need to present the data analysis results to users intuitively. This requires data statistics graphs. To achieve this function, we need several third-party packages:

1. jfreechart-1.0.13.jar

2. jcommon-1.0.16.jar

3. gnujaxp. Jar

 

Let's take a look at it and finally:

 

MainlyJfreechart-1.0.13.jar, But these three packages must be complete, and I have added allJfreechartThe jar package is compressed and uploaded together with the Project (CODE) of the example in this article. If you are interested, you can download it,

: Http://download.csdn.net/detail/pzhtpf/4327700

 

Next, we will implement this program step by step.

 

I. preparations in the early stage, the three third-party packages will be added to the project in this article. The process of adding these three third-party packages is very simple. I wrote a blog earlier, java is about how to read data in the Excel table (interested students can take a look: http://blog.csdn.net/pzhtpf/article/details/7506135), also want to add a third-party package, add process exactly the same, here we review it again:

1. Create and set up a Java project, and create a new folder Lib in this project;

2. Copy the preceding three jar packages to the Lib

3. Right-click the Java project and select Properties.

4. Select Java build path in the list on the left and libraries on the right.

5. Click Add jars.

6. Select the Three jar files in the Lib folder of the project and click OK.

After the project succeeds, there will be one more Folder: referenced Libraries

 

2. Java code for implementing the bar 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", // display label "quantity" of the Directory axis, // display label dataset of the value axis, // dataset plotorientation. vertical, // chart direction: horizontal, vertical true, // whether to display the legend (for a simple column chart, it must be false) false, // whether to generate the tool false // whether to generate the URL link); // from here, categoryplot plot = chart. getcategoryplot (); // obtain the chart region object categoryaxis domainaxis = plot. getdomainaxis (); // list of horizontal bottom domainaxis. setlabelfont (new font ("", Font. bold, 14); // horizontal bottom title domainaxis. setticklabelfont (new font ("", Font. bold, 12); // vertical title valueaxis rangeaxis = plot. getrangeaxis (); // obtain the columnar rangeaxis. setlabelfont (new font ("", Font. bold, 15); chart. getlegend (). setitemfont (new font ("", Font. bold, 15); chart. gettitle (). setfont (new font ("", Font. bold, 20); // set the title Font // It ends here. Although the Code is a bit large, it is only for one purpose. frame1 = new chartpanel (chart, true); // chartframe can also be used here to directly generate an independent 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", "Pears"); dataset. addvalue (200, "Shanghai", "Pears"); dataset. addvalue (200, "Guangzhou", "Pears"); 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 ;}}

 

As follows:

 

HoweverPrivate StaticAfter the data in the categorydataset getdataset () {} method changes, another effect will be formed. For example, we change it:

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 ;}

 

As follows:

 

3. Java code for implementing 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 () {defapipiedataset DATA = getdataset (); jfreechart chart = chartfactory. createpiechart3d ("fruit yield", Data, true, false, false); // set the percentage pieplot = (pieplot) chart. getplot (); decimalformat df = new decimalformat ("0.00%"); // obtain a decimalformat object, which is mainly used to set the numberformat NF = numberformat. getnumberinstance (); // obtain a numberformat object standardpiesectionlabelgenerator SP1 = new standardpiesectionlabelgenerator ("{0} {2}", NF, DF); // obtain the pieplot object. setlabelgenerator (SP1); // set the percentage of the pie chart to display. // The pieplot content displayed when no data exists. setnodatamessage ("no data display"); pieplot. setcircular (false); pieplot. setlabelgap (0.02d); pieplot. setignorenullvalues (true); // The setting does not show the null pieplot. setignorezerovalues (true); // set not to display negative value frame1 = new chartpanel (chart, true); chart. gettitle (). setfont (new font ("", Font. bold, 20); // set the title Font pieplot = (pieplot) chart. getplot (); // obtain the chart area object pieplot. setlabelfont (new font ("", Font. bold, 10); // solves the garbled chart. getlegend (). setitemfont (new font ("", Font. bold, 10);} Private Static defaultpiedataset getdataset () {defapipiedataset dataset = new defaultpiedataset (); dataset. setvalue ("apple", 100); dataset. setvalue ("Pears", 200); dataset. setvalue ("grape", 300); dataset. setvalue ("banana", 400); dataset. setvalue ("litchi", 500); Return dataset;} public chartpanel getchartpanel () {return frame1 ;}}

 

As follows:

 

4. Java code for line chart implementation:

 

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 = createdataset (); jfreechart = chartfactory. createtimeserieschart ("Legal & General Unit Trust Fund Price", "date", "price", xydataset, true); xyplot = (xyplot) jfreechart. getplot (); dateaxis = (dateaxis) xyplot. getdomainaxis (); dateaxis. setdateformatoverride (New simpledateformat ("Mmm-YYYY"); frame1 = new chartpanel (jfreechart, true); dateaxis. setlabelfont (new font ("", Font. bold, 14); // horizontal bottom title dateaxis. setticklabelfont (new font ("", Font. bold, 12); // vertical title valueaxis rangeaxis = xyplot. getrangeaxis (); // obtain the columnar rangeaxis. setlabelfont (new font ("", Font. bold, 15); jfreechart. getlegend (). setitemfont (new font ("", Font. bold, 15); jfreechart. gettitle (). setfont (new font ("", Font. bold, 20); // set the title Font} Private Static xydataset createdataset () {// there are a lot of datasets, but it is not hard to understand 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.300100000000001d); Timeseries. add (new month (8, 2001), 153.90000000000001d); Timeseries. add (new month (9, 2001), 142.69999999999999d); Timeseries. add (new month (10,200 1), 123.2d); Timeseries. add (new month (11,200 1), 131.80000000000001d); Timeseries. add (new month (12,200 1), 139.59999999999999d); Timeseries. add (new month (1, 2002), 142.900100000000001d); Timeseries. add (new month (2, 2002), 138.69999999999999d); Timeseries. add (new month (3, 2002), 137.300100000000001d); Timeseries. add (new month (4, 2002), 143.900100000000001d); 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), week); 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 (10,200 1 ), 101.5d); timeseries1.add (new month (11,200 1), 106.099999999999d); timeseries1.add (new month (12,200 1), 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), week ); timeseriescollection = new timeseriescollection (); timeseriescollection. addseries (Timeseries); timeseriescollection. addseries (timeseries1); Return timeseriescollection;} public chartpanel getchartpanel () {return frame1 ;}}

 

As follows:

 

Let's take a 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 graph"); frame. setlayout (New gridlayout (2, 2, 10, 10); frame. add (New barchart (). getchartpanel (); // Add a bar comment frame. add (New barchart1 (). getchartpanel (); // Add another effect frame of the bar chart. add (New piechart (). getchartpanel (); // Add a pie chart frame. add (New timeserieschart (). getchartpanel (); // Add a line chart frame. setbounds (50, 50,800,600); frame. setvisible (true );}}

 

V. Summary

 

The above is a simple example. If you want to know more about it, You can query the information on your own. In fact, the above Code is easy to understand, as long as you combine your actual situation, you can develop your own application. You can see that I have implemented the application here. In fact, more data statistics charts are applied on javaweb. You can also understand them on your own.

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

 

 

 

 

 

 

 

 

Related Article

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.