使用java实现各种数据统计图(柱形图,饼图,折线图)
最近在做数据挖掘的课程设计,需要将数据分析的结果很直观的展现给用户,这就要用到数据统计图,要实现这个功能就需要几个第三方包了:
1. jfreechart-1.0.13.jar
2. jcommon-1.0.16.jar
3. gnujaxp.jar
先来看一下,最终效果图:
主要是jfreechart-1.0.13.jar,但这三个包要齐全,我已经将所有与jfreechart有关的jar包与本文实例的工程(代码)一同压缩上传了,有兴趣的同学可以下载,
下载地址:
接下来,我们一步步来实现本程序。
一,前期准备工作,也就把这三个第三方包添加进本文工程,添加过程特别简单,前面写过一篇博客,讲的是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 = getdataset(); jfreechart chart = chartfactory.createpiechart3d("水果产量",data,true,false,false); //设置百分比 pieplot pieplot = (pieplot) chart.getplot(); decimalformat df = new decimalformat("0.00%");//获得一个decimalformat对象,主要是设置小数问题 numberformat nf = numberformat.getnumberinstance();//获得一个numberformat对象 standardpiesectionlabelgenerator sp1 = new standardpiesectionlabelgenerator("{0} {2}", nf, df);//获得standardpiesectionlabelgenerator对象 pieplot.setlabelgenerator(sp1);//设置饼图显示百分比 //没有数据的时候显示的内容 pieplot.setnodatamessage("无数据显示"); pieplot.setcircular(false); pieplot.setlabelgap(0.02d); pieplot.setignorenullvalues(true);//设置不显示空值 pieplot.setignorezerovalues(true);//设置不显示负值 frame1=new chartpanel (chart,true); chart.gettitle().setfont(new font("宋体",font.bold,20));//设置标题字体 pieplot pieplot= (pieplot) chart.getplot();//获取图表区域对象 pieplot.setlabelfont(new font("宋体",font.bold,10));//解决乱码 chart.getlegend().setitemfont(new font("黑体",font.bold,10)); } private static defaultpiedataset getdataset() { defaultpiedataset dataset = new defaultpiedataset(); dataset.setvalue("苹果",100); dataset.setvalue("梨子",200); dataset.setvalue("葡萄",300); dataset.setvalue("香蕉",400); dataset.setvalue("荔枝",500); return dataset; } public chartpanel getchartpanel(){ return frame1; } }
效果图如下:
四 实现折线图的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上应用更多,大家也可自行了解。