java实现各种数据统计图.docx

上传人:b****7 文档编号:15615694 上传时间:2023-07-06 格式:DOCX 页数:34 大小:652.41KB
下载 相关 举报
java实现各种数据统计图.docx_第1页
第1页 / 共34页
java实现各种数据统计图.docx_第2页
第2页 / 共34页
java实现各种数据统计图.docx_第3页
第3页 / 共34页
java实现各种数据统计图.docx_第4页
第4页 / 共34页
java实现各种数据统计图.docx_第5页
第5页 / 共34页
java实现各种数据统计图.docx_第6页
第6页 / 共34页
java实现各种数据统计图.docx_第7页
第7页 / 共34页
java实现各种数据统计图.docx_第8页
第8页 / 共34页
java实现各种数据统计图.docx_第9页
第9页 / 共34页
java实现各种数据统计图.docx_第10页
第10页 / 共34页
java实现各种数据统计图.docx_第11页
第11页 / 共34页
java实现各种数据统计图.docx_第12页
第12页 / 共34页
java实现各种数据统计图.docx_第13页
第13页 / 共34页
java实现各种数据统计图.docx_第14页
第14页 / 共34页
java实现各种数据统计图.docx_第15页
第15页 / 共34页
java实现各种数据统计图.docx_第16页
第16页 / 共34页
java实现各种数据统计图.docx_第17页
第17页 / 共34页
java实现各种数据统计图.docx_第18页
第18页 / 共34页
java实现各种数据统计图.docx_第19页
第19页 / 共34页
java实现各种数据统计图.docx_第20页
第20页 / 共34页
亲,该文档总共34页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

java实现各种数据统计图.docx

《java实现各种数据统计图.docx》由会员分享,可在线阅读,更多相关《java实现各种数据统计图.docx(34页珍藏版)》请在冰点文库上搜索。

java实现各种数据统计图.docx

java实现各种数据统计图

 

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

分类:

 Java2012-05-2422:

28 22644人阅读 评论(29) 收藏 举报

javajfreechartdatasetapplicationclassproperties

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

1.      jfreechart-1.0.13.jar

2.      jcommon-1.0.16.jar

3.      gnujaxp.jar

 

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

 

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

下载地址:

 

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

 

一,前期准备工作,也就把这三个第三方包添加进本文工程,添加过程特别简单,前面写过一篇博客,讲的是java如何读取Excel表格中的数据(有兴趣的同学可以看一看:

1, 建,立java项目,在这个项目在建立一个新的文件夹lib;

2, 将上述三个jar包,复制到lib

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

4,在左侧列表里选中JavaBuildPath,右侧选中Libraries

5,点击AddJARs

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

成功后,项目中会多一个文件夹为:

ReferencedLibraries

 

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

[plain] viewplaincopy

1.  import java.awt.Font;  

2.  

3.import org.jfree.chart.ChartFactory;  

4.import org.jfree.chart.ChartPanel;  

5.import org.jfree.chart.JFreeChart;  

6.import org.jfree.chart.axis.CategoryAxis;  

7.import org.jfree.chart.axis.ValueAxis;  

8.import org.jfree.chart.plot.CategoryPlot;  

9.import org.jfree.chart.plot.PlotOrientation;  

10.import org.jfree.data.category.CategoryDataset;  

11.import org.jfree.data.category.DefaultCategoryDataset;  

12.  

13.public class BarChart {  

14.    ChartPanel frame1;  

15.    public  BarChart(){  

16.        CategoryDataset dataset = getDataSet();  

17.        JFreeChart chart = ChartFactory.createBarChart3D(  

18.                             "水果", // 图表标题  

19.                            "水果种类", // 目录轴的显示标签  

20.                            "数量", // 数值轴的显示标签  

21.                            dataset, // 数据集  

22.                            PlotOrientation.VERTICAL, // 图表方向:

水平、垂直  

23.                            true,           // 是否显示图例(对于简单的柱状图必须是false)  

24.                            false,          // 是否生成工具  

25.                            false           // 是否生成URL链接  

26.                            );  

27.          

28.        //从这里开始  

29.        CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象  

30.        CategoryAxis domainAxis=plot.getDomainAxis();         //水平底部列表  

31.         domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));         //水平底部标题  

32.         domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题  

33.         ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状  

34.         rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));  

35.          chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));  

36.          chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体  

37.            

38.          //到这里结束,虽然代码有点多,但只为一个目的,解决汉字乱码问题  

39.            

40.         frame1=new ChartPanel(chart,true);        //这里也可以用chartFrame,可以直接生成一个独立的Frame  

41.           

42.    }  

43.       private static CategoryDataset getDataSet() {  

44.           DefaultCategoryDataset dataset = new DefaultCategoryDataset();  

45.           dataset.addValue(100, "北京", "苹果");  

46.           dataset.addValue(100, "上海", "苹果");  

47.           dataset.addValue(100, "广州", "苹果");  

48.           dataset.addValue(200, "北京", "梨子");  

49.           dataset.addValue(200, "上海", "梨子");  

50.           dataset.addValue(200, "广州", "梨子");  

51.           dataset.addValue(300, "北京", "葡萄");  

52.           dataset.addValue(300, "上海", "葡萄");  

53.           dataset.addValue(300, "广州", "葡萄");  

54.           dataset.addValue(400, "北京", "香蕉");  

55.           dataset.addValue(400, "上海", "香蕉");  

56.           dataset.addValue(400, "广州", "香蕉");  

57.           dataset.addValue(500, "北京", "荔枝");  

58.           dataset.addValue(500, "上海", "荔枝");  

59.           dataset.addValue(500, "广州", "荔枝");  

60.           return dataset;  

61.}  

62.public ChartPanel getChartPanel(){  

63.    return frame1;  

64.      

65.}  

66.}  

 

效果图如下:

 

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

[plain] viewplaincopy

1.private static CategoryDataset getDataSet() {  

2.           DefaultCategoryDataset dataset = new DefaultCategoryDataset();  

3.           dataset.addValue(100, "苹果", "苹果");  

4.           dataset.addValue(200, "梨子", "梨子");  

5.           dataset.addValue(300, "葡萄", "葡萄");  

6.           dataset.addValue(400, "香蕉", "香蕉");  

7.           dataset.addValue(500, "荔枝", "荔枝");  

8.           return dataset;  

9.}  

 

效果图如下:

 

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

[plain] viewplaincopy

1. package com.njue.testJFreeChart;  

2.  

3.import java.awt.Font;  

4.import java.text.DecimalFormat;  

5.import java.text.NumberFormat;  

6.  

7.import javax.swing.JPanel;  

8.  

9.import org.jfree.chart.ChartFactory;  

10.import org.jfree.chart.ChartPanel;  

11.import org.jfree.chart.JFreeChart;  

12.import org.jfree.chart.labels.StandardPieSectionLabelGenerator;  

13.import org.jfree.chart.plot.PiePlot;  

14.import org.jfree.data.general.DefaultPieDataset;  

15.  

16.public class PieChart {  

17.    ChartPanel frame1;  

18.    public PieChart(){  

19.          DefaultPieDataset data = getDataSet();  

20.          JFreeChart chart = ChartFactory.createPieChart3D("水果产量",data,true,false,false);  

21.        //设置百分比  

22.          PiePlot pieplot = (PiePlot) chart.getPlot();  

23.          DecimalFormat df = new DecimalFormat("0.00%");//获得一个DecimalFormat对象,主要是设置小数问题  

24.          NumberFormat nf = NumberFormat.getNumberInstance();//获得一个NumberFormat对象  

25.          StandardPieSectionLabelGenerator sp1 = new StandardPieSectionLabelGenerator("{0}  {2}", nf, df);//获得StandardPieSectionLabelGenerator对象  

26.          pieplot.setLabelGenerator(sp1);//设置饼图显示百分比  

27.        

28.      //没有数据的时候显示的内容  

29.          pieplot.setNoDataMessage("无数据显示");  

30.          pieplot.setCircular(false);  

31.          pieplot.setLabelGap(0.02D);  

32.        

33.          pieplot.setIgnoreNullValues(true);//设置不显示空值  

34.          pieplot.setIgnoreZeroValues(true);//设置不显示负值  

35.         frame1=new ChartPanel (chart,true);  

36.          chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体  

37.          PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象  

38.          piePlot.setLabelFont(new Font("宋体",Font.BOLD,10));//解决乱码  

39.          chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,10));  

40.    }  

41.    private static DefaultPieDataset getDataSet() {  

42.        DefaultPieDataset dataset = new DefaultPieDataset();  

43.        dataset.setValue("苹果",100);  

44.        dataset.setValue("梨子",200);  

45.        dataset.setValue("葡萄",300);  

46.        dataset.setValue("香蕉",400);  

47.        dataset.setValue("荔枝",500);  

48.        return dataset;  

49.}  

50.    public ChartPanel getChartPanel(){  

51.        return frame1;  

52.          

53.    }  

54.}  

 

效果图如下:

 

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

 

[plain] viewplaincopy

1.package com.njue.testJFreeChart;  

2.  

3.import java.awt.Font;  

4.import java.text.SimpleDateFormat;  

5.  

6.import org.jfree.chart.ChartFactory;  

7.import org.jfree.chart.ChartPanel;  

8.import org.jfree.chart.JFreeChart;  

9.import org.jfree.chart.axis.DateAxis;  

10.import org.jfree.chart.axis.ValueAxis;  

11.import org.jfree.chart.plot.XYPlot;  

12.import org.jfree.data.time.Month;  

13.import org.jfree.data.time.TimeSeries;  

14.import org.jfree.data.time.TimeSeriesCollection;  

15.import org.jfree.data.xy.XYDataset;  

16.  

17.public class TimeSeriesChart {  

18.    ChartPanel frame1;  

19.    public TimeSeriesChart(){  

20.        XYDataset xydataset = createDataset();  

21.        JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General单位信托基金价格", "日期", "价格",xydataset, true, true, true);  

22.        XYPlot xyplot = (XYPlot) jfreechart.getPlot();  

23.        DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();  

24.        dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));  

25.        frame1=new ChartPanel(jfreechart,true);  

26.        dateaxis.setLabelFont(new Font("黑体",Font.BOLD,14));         //水平底部标题  

27.        dateaxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题  

28.        ValueAxis rangeAxis=xyplot.getRangeAxis();//获取柱状  

29.        rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));  

30.        jfreechart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));  

31.        jfreechart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体  

32.  

33.    }   

34.     private static XYDataset createDataset() {  //这个数据集有点多,但都不难理解  

35.            TimeSeries timeseries = new TimeSeries("legal & general欧洲指数信任",  

36.                    org.jfree.data.time.Month.class);  

37.            timeseries.add(new Month(2, 2001), 181.80000000000001D);  

38.            timeseries.add(new Month(3, 2001), 167.30000000000001D);  

39.            timeseries.add(new Month(4, 2001), 153.80000000000001D);  

40.            timeseries.add(new Month(5, 2001), 167.59999999999999D);  

41.            timeseries.add(new Month(6, 2001), 158.80000000000001D);  

42.            timeseries.add(new Month(7, 2001), 148.30000000000001D);  

43.            timeseries.add(new Month(8, 2001), 153.90000000000001D);  

44.            timeseries.add(new Month(9, 2001), 142.69999999999999D);  

45.            timeseries.add(new Month(10, 2001), 123.2D);  

46.            timeseries.add(new Month(11, 2001), 131.80000000000001D);  

47.            timeseries.add(new Month(12, 2001), 139.59999999999999D);  

48.            timeseries.add(new Month(1, 2002), 142.90000000000001D);  

49.            timeseries.add(new Month(2, 2002), 138.69999999999999D);  

50.            timeseries.add(n

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2