首页 > 代码库 > java实现各种数据统计图(柱形图,饼图,折线图)

java实现各种数据统计图(柱形图,饼图,折线图)

近期在做数据挖掘的课程设计,须要将数据分析的结果非常直观的展现给用户,这就要用到数据统计图,要实现这个功能就须要几个第三方包了:

1.       jfreechart-1.0.13.jar

2.       jcommon-1.0.16.jar

3.       gnujaxp.jar

 

先来看一下,终于效果图:

 

主要是jfreechart-1.0.13.jar,但这三个包要齐全,我已经将全部与jfreechart有关的jar包与本文实例的project(代码)一同压缩上传了,有兴趣的同学能够下载,

下载地址:http://download.csdn.net/detail/pzhtpf/4327700

 

接下来,我们一步步来实现本程序。

 

一,前期准备工作,也就把这三个第三方包加入进本文project,加入过程特别简单,前面写过一篇博客,讲的是java怎样读取Excel表格中的数据(有兴趣的同学能够看一看:http://blog.csdn.net/pzhtpf/article/details/7506135),也要加入第三方包,加入过程一模一样,这里我们在复习一遍:

1 ,java项目,在这个项目在建立一个新的目录lib

2 将上述三个jar包,拷贝到lib

3然后右键点击这个java项目,选择Properties

4在左側列表里选中Java Build Path,右側选中Libraries

5点击Add JARs

6 然后去选择这个项目中lib目录中的三个jar,点击确定

成功后,项目中会多一个目录为:Referenced Libraries

 

二, 实现柱形图的java代码:

  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(       		                 "水果", // 图表标题                            "水果种类", // 文件夹轴的显示标签                            "数量", // 数值轴的显示标签                            dataset, // 数据集                            PlotOrientation.VERTICAL, // 图表方向:水平、垂直                            true,           // 是否显示图例(对于简单的柱状图必须是false)                            false,          // 是否生成工具                            false           // 是否生成URL链接                            );                //从这里開始        CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象        CategoryAxis domainAxis=plot.getDomainAxis();         //水平底部列表         domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));         //水平底部标题         domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题         ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状         rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));          chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));          chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体                    //到这里结束,尽管代码有点多,但仅仅为一个目的,解决汉字乱码问题                   frame1=new ChartPanel(chart,true);        //这里也能够用chartFrame,能够直接生成一个独立的Frame         	}	   private static CategoryDataset getDataSet() {           DefaultCategoryDataset dataset = new DefaultCategoryDataset();           dataset.addValue(100, "北京", "苹果");           dataset.addValue(100, "上海", "苹果");           dataset.addValue(100, "广州", "苹果");           dataset.addValue(200, "北京", "梨子");           dataset.addValue(200, "上海", "梨子");           dataset.addValue(200, "广州", "梨子");           dataset.addValue(300, "北京", "葡萄");           dataset.addValue(300, "上海", "葡萄");           dataset.addValue(300, "广州", "葡萄");           dataset.addValue(400, "北京", "香蕉");           dataset.addValue(400, "上海", "香蕉");           dataset.addValue(400, "广州", "香蕉");           dataset.addValue(500, "北京", "荔枝");           dataset.addValue(500, "上海", "荔枝");           dataset.addValue(500, "广州", "荔枝");           return dataset;}public ChartPanel getChartPanel(){	return frame1;	}}


 

效果图例如以下:

 

但我们把private static CategoryDataset getDataSet(){}方法中的数据变化一下后,又会形成还有一种效果,比方说我们改成:

private static CategoryDataset getDataSet() {           DefaultCategoryDataset dataset = new DefaultCategoryDataset();           dataset.addValue(100, "苹果", "苹果");           dataset.addValue(200, "梨子", "梨子");           dataset.addValue(300, "葡萄", "葡萄");           dataset.addValue(400, "香蕉", "香蕉");           dataset.addValue(500, "荔枝", "荔枝");           return dataset;}


 

效果图例如以下:

 

三,    实现饼状图的java代码:

 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 = http://www.mamicode.com/getDataSet();>


 

效果图例如以下:

 

四,      实现折线图的java代码:

 

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单位信托基金价格", "日期", "价格",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("黑体",Font.BOLD,14));         //水平底部标题        dateaxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题        ValueAxis rangeAxis=xyplot.getRangeAxis();//获取柱状        rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));        jfreechart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));        jfreechart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体	} 	 private static XYDataset createDataset() {  //这个数据集有点多,但都不难理解	        TimeSeries timeseries = new TimeSeries("legal & general欧洲指数信任",	                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(10, 2001), 123.2D);	        timeseries.add(new Month(11, 2001), 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);	        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英国指数信任",	                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(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;	    		    }}


 

效果图例如以下:

 

再来看一下主方法

import java.awt.GridLayout;import javax.swing.JFrame;public class mainClass {public static void main(String args[]){	JFrame frame=new JFrame("Java数据统计图");	frame.setLayout(new GridLayout(2,2,10,10));	frame.add(new BarChart().getChartPanel());           //加入柱形图	frame.add(new BarChart1().getChartPanel());          //加入柱形图的还有一种效果	frame.add(new PieChart().getChartPanel());           //加入饼状图	frame.add(new TimeSeriesChart().getChartPanel());    //加入折线图	frame.setBounds(50, 50, 800, 600);	frame.setVisible(true);}}


 

五,             总结

 

以上都是一个简单的样例去实现了,想了解更深的同学可自行查询资料,事实上以上代码都通俗易懂,仅仅要结合自己的实际情况,便可开发出属于自己的Application,大家能够看出我这里是在Application上实现的,事实上很多其它情况数据统计图在javaweb上应用很多其它,大家也可自行了解。

ps:如执行本文project项目是出现错误,请參考博文:http://blog.csdn.net/pzhtpf/article/details/7506135

 

 

 

 

 

 

 

 

java实现各种数据统计图(柱形图,饼图,折线图)