Recently I am designing a data mining course. I need to display the results of data analysis to users intuitively. This requires the use of data statistics charts. To realize this function, I 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 the final rendering:
Mainly jfreechart-1.0.13.jar, but these three packages must be complete. I have compressed and uploaded all jar packages related to jfreechart together with the project (code) of the example in this article. Interested students can download it.
Download address: http://download.csdn.net/detail/pzhtpf/4327700
Next, we implement this program step by step.
1. In the preliminary preparation work, these three third-party packages are added to the project of this article. The adding process is very simple. I have written a blog before about how to read data in Excel tables with Java (interested students can Take a look: http://blog.csdn.net/pzhtpf/article/details/7506135), you also need to add third-party packages. The adding process is exactly the same. Here we will review it:
1. Create a java project and create a new folder lib in this project;
2. Copy the above three jar packages to lib
3. Then 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. Then select the three jars in the lib folder of this project and click OK
After success, there will be an additional folder in the project: Referenced Libraries
2. Java code to implement 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", // Display label of catalog axis "Quantity", // Display label of numerical axis dataset, // Data set PlotOrientation.VERTICAL, // Chart direction: horizontal, vertical true , // Whether to display the legend (must be false for a simple bar chart) false, // Whether to generate tools false // Whether to generate URL links); // Start CategoryPlot from here plot=chart.getCategoryPlot();//Get the chart area object CategoryAxis domainAxis=plot.getDomainAxis(); //Horizontal bottom list domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14)); //Horizontal Bottom title domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12)); //Vertical title ValueAxis rangeAxis=plot.getRangeAxis(); //Get 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//This ends here. Although the code is a bit much, it is only for one purpose, to solve the problem of garbled Chinese characters frame1=new ChartPanel(chart,true); //chartFrame can also be used here, which can 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", "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; } } The renderings are as follows:
But after we change the data in the private static CategoryDataset getDataSet(){} method, another effect will form. For example, we change it to:
private static CategoryDataset getDataSet() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "Apple", "Apple"); dataset.addValue(200, "Pear", "Pear"); dataset.addValue(300 , "Grape", "Grape"); dataset.addValue(400, "Banana", "Banana"); dataset.addValue(500, "Litchi", "Litchi"); return dataset; }The renderings are as follows:
Three 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 the percentage PiePlot pieplot = (PiePlot) chart.getPlot(); DecimalFormat df = new DecimalFormat("0.00%"); //Get a DecimalFormat object, mainly setting decimal issues NumberFormat nf = NumberFormat.getNumberInstance();//Get a NumberFormat object StandardPieSectionLabelGenerator sp1 = new StandardPieSectionLabelGenerator("{0} {2}", nf, df);//Get the StandardPieSectionLabelGenerator object pieplot.setLabelGenerator(sp1);//Set the pie chart display percentage //The content displayed when there is no data pieplot.setNoDataMessage("No data displayed"); pieplot.setCircular(false); pieplot .setLabelGap(0.02D); pieplot.setIgnoreNullValues(true);//Set not to display null values pieplot.setIgnoreZeroValues(true);//Set not to display negative values frame1=new ChartPanel (chart,true); chart.getTitle().setFont(new Font(" Song Dynasty",Font.BOLD,20));//Set the title font PiePlot piePlot= (PiePlot) chart.getPlot();//Get the chart area object piePlot.setLabelFont(new Font("宋体",Font.BOLD,10));//Solve garbled characters chart.getLegend().setItemFont(new Font("黑体", Font.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("lychee" ",500); return dataset; } public ChartPanel getChartPanel(){ return frame1; } } The renderings are as follows:
4. Java code to implement 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(){ ) jfreechart.getPlot(); DateAxis 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(); //Get 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() { //This data set is a bit much, but it is 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); 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(10, 2001), 123.2D); 131.80000000000001D); timeseries.add(new Month(12, 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); 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.5999999999999D); timeseries1.add(new Month(3, 2001), 123.2D); timeseries1.add(new timeseries1.add(new Month(3, 2001), 123.2D) 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(10, 2001), 101.5D); timeseries1.add(new Month(11, 2001), 106.09999999999999D ); timeseries1.add(new Month(12, 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 renderings are 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 Chart"); frame.setLayout(new GridLayout( 2,2,10,10)); frame.add(new BarChart().getChartPanel()); //Add bar chart frame.add(new BarChart1().getChartPanel()); //Add another effect of column chart frame.add(new PieChart().getChartPanel()); //Add pie chart frame.add(new TimeSeriesChart().getChartPanel ()); //Add a line chart frame.setBounds(50, 50, 800, 600); frame.setVisible(true); } } Five summary
The above is a simple example to implement. Students who want to know more can check the information by themselves. In fact, the above codes are easy to understand. As long as you combine your actual situation, you can develop your own Application. You can see that I This is implemented on Application. In fact, more statistical charts are used on JavaWeb. You can also learn about it by yourself.