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

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

1. jfreechart-1.0.13.jar

2. jcommon-1.0.16.jar

3. gnujaxp.jar

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

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

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

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

一,前期准备工作,也就把这三个第三方包添加进本文工程,添加过程特别简单,前面写过一篇博客,讲的是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代码:

  1. import java.awt.Font;
  2. import org.jfree.chart.ChartFactory;
  3. import org.jfree.chart.ChartPanel;
  4. import org.jfree.chart.JFreeChart;
  5. import org.jfree.chart.axis.CategoryAxis;
  6. import org.jfree.chart.axis.ValueAxis;
  7. import org.jfree.chart.plot.CategoryPlot;
  8. import org.jfree.chart.plot.PlotOrientation;
  9. import org.jfree.data.category.CategoryDataset;
  10. import org.jfree.data.category.DefaultCategoryDataset;
  11. public class BarChart {
  12. ChartPanel frame1;
  13. public BarChart(){
  14. CategoryDataset dataset = getDataSet();
  15. JFreeChart chart = ChartFactory.createBarChart3D(
  16. "水果", // 图表标题
  17. "水果种类", // 目录轴的显示标签
  18. "数量", // 数值轴的显示标签
  19. dataset, // 数据集
  20. PlotOrientation.VERTICAL, // 图表方向:水平、垂直
  21. true, // 是否显示图例(对于简单的柱状图必须是false)
  22. false, // 是否生成工具
  23. false // 是否生成URL链接
  24. );
  25. //从这里开始
  26. CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
  27. CategoryAxis domainAxis=plot.getDomainAxis(); //水平底部列表
  28. domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14)); //水平底部标题
  29. domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12)); //垂直标题
  30. ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
  31. rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
  32. chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
  33. chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体
  34. //到这里结束,虽然代码有点多,但只为一个目的,解决汉字乱码问题
  35. frame1=new ChartPanel(chart,true); //这里也可以用chartFrame,可以直接生成一个独立的Frame
  36. }
  37. private static CategoryDataset getDataSet() {
  38. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  39. dataset.addValue(100, "北京", "苹果");
  40. dataset.addValue(100, "上海", "苹果");
  41. dataset.addValue(100, "广州", "苹果");
  42. dataset.addValue(200, "北京", "梨子");
  43. dataset.addValue(200, "上海", "梨子");
  44. dataset.addValue(200, "广州", "梨子");
  45. dataset.addValue(300, "北京", "葡萄");
  46. dataset.addValue(300, "上海", "葡萄");
  47. dataset.addValue(300, "广州", "葡萄");
  48. dataset.addValue(400, "北京", "香蕉");
  49. dataset.addValue(400, "上海", "香蕉");
  50. dataset.addValue(400, "广州", "香蕉");
  51. dataset.addValue(500, "北京", "荔枝");
  52. dataset.addValue(500, "上海", "荔枝");
  53. dataset.addValue(500, "广州", "荔枝");
  54. return dataset;
  55. }
  56. public ChartPanel getChartPanel(){
  57. return frame1;
  58. }
  59. }


效果图如下:

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

  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代码:

  1. package com.njue.testJFreeChart;
  2. import java.awt.Font;
  3. import java.text.DecimalFormat;
  4. import java.text.NumberFormat;
  5. import javax.swing.JPanel;
  6. import org.jfree.chart.ChartFactory;
  7. import org.jfree.chart.ChartPanel;
  8. import org.jfree.chart.JFreeChart;
  9. import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
  10. import org.jfree.chart.plot.PiePlot;
  11. import org.jfree.data.general.DefaultPieDataset;
  12. public class PieChart {
  13. ChartPanel frame1;
  14. public PieChart(){
  15. DefaultPieDataset data = getDataSet();
  16. JFreeChart chart = ChartFactory.createPieChart3D("水果产量",data,true,false,false);
  17. //设置百分比
  18. PiePlot pieplot = (PiePlot) chart.getPlot();
  19. DecimalFormat df = new DecimalFormat("0.00%");//获得一个DecimalFormat对象,主要是设置小数问题
  20. NumberFormat nf = NumberFormat.getNumberInstance();//获得一个NumberFormat对象
  21. StandardPieSectionLabelGenerator sp1 = new StandardPieSectionLabelGenerator("{0} {2}", nf, df);//获得StandardPieSectionLabelGenerator对象
  22. pieplot.setLabelGenerator(sp1);//设置饼图显示百分比
  23. //没有数据的时候显示的内容
  24. pieplot.setNoDataMessage("无数据显示");
  25. pieplot.setCircular(false);
  26. pieplot.setLabelGap(0.02D);
  27. pieplot.setIgnoreNullValues(true);//设置不显示空值
  28. pieplot.setIgnoreZeroValues(true);//设置不显示负值
  29. frame1=new ChartPanel (chart,true);
  30. chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体
  31. PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象
  32. piePlot.setLabelFont(new Font("宋体",Font.BOLD,10));//解决乱码
  33. chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,10));
  34. }
  35. private static DefaultPieDataset getDataSet() {
  36. DefaultPieDataset dataset = new DefaultPieDataset();
  37. dataset.setValue("苹果",100);
  38. dataset.setValue("梨子",200);
  39. dataset.setValue("葡萄",300);
  40. dataset.setValue("香蕉",400);
  41. dataset.setValue("荔枝",500);
  42. return dataset;
  43. }
  44. public ChartPanel getChartPanel(){
  45. return frame1;
  46. }
  47. }


效果图如下:

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

  1. package com.njue.testJFreeChart;
  2. import java.awt.Font;
  3. import java.text.SimpleDateFormat;
  4. import org.jfree.chart.ChartFactory;
  5. import org.jfree.chart.ChartPanel;
  6. import org.jfree.chart.JFreeChart;
  7. import org.jfree.chart.axis.DateAxis;
  8. import org.jfree.chart.axis.ValueAxis;
  9. import org.jfree.chart.plot.XYPlot;
  10. import org.jfree.data.time.Month;
  11. import org.jfree.data.time.TimeSeries;
  12. import org.jfree.data.time.TimeSeriesCollection;
  13. import org.jfree.data.xy.XYDataset;
  14. public class TimeSeriesChart {
  15. ChartPanel frame1;
  16. public TimeSeriesChart(){
  17. XYDataset xydataset = createDataset();
  18. JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General单位信托基金价格", "日期", "价格",xydataset, true, true, true);
  19. XYPlot xyplot = (XYPlot) jfreechart.getPlot();
  20. DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
  21. dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
  22. frame1=new ChartPanel(jfreechart,true);
  23. dateaxis.setLabelFont(new Font("黑体",Font.BOLD,14)); //水平底部标题
  24. dateaxis.setTickLabelFont(new Font("宋体",Font.BOLD,12)); //垂直标题
  25. ValueAxis rangeAxis=xyplot.getRangeAxis();//获取柱状
  26. rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
  27. jfreechart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
  28. jfreechart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体
  29. }
  30. private static XYDataset createDataset() { //这个数据集有点多,但都不难理解
  31. TimeSeries timeseries = new TimeSeries("legal & general欧洲指数信任",
  32. org.jfree.data.time.Month.class);
  33. timeseries.add(new Month(2, 2001), 181.80000000000001D);
  34. timeseries.add(new Month(3, 2001), 167.30000000000001D);
  35. timeseries.add(new Month(4, 2001), 153.80000000000001D);
  36. timeseries.add(new Month(5, 2001), 167.59999999999999D);
  37. timeseries.add(new Month(6, 2001), 158.80000000000001D);
  38. timeseries.add(new Month(7, 2001), 148.30000000000001D);
  39. timeseries.add(new Month(8, 2001), 153.90000000000001D);
  40. timeseries.add(new Month(9, 2001), 142.69999999999999D);
  41. timeseries.add(new Month(10, 2001), 123.2D);
  42. timeseries.add(new Month(11, 2001), 131.80000000000001D);
  43. timeseries.add(new Month(12, 2001), 139.59999999999999D);
  44. timeseries.add(new Month(1, 2002), 142.90000000000001D);
  45. timeseries.add(new Month(2, 2002), 138.69999999999999D);
  46. timeseries.add(new Month(3, 2002), 137.30000000000001D);
  47. timeseries.add(new Month(4, 2002), 143.90000000000001D);
  48. timeseries.add(new Month(5, 2002), 139.80000000000001D);
  49. timeseries.add(new Month(6, 2002), 137D);
  50. timeseries.add(new Month(7, 2002), 132.80000000000001D);
  51. TimeSeries timeseries1 = new TimeSeries("legal & general英国指数信任",
  52. org.jfree.data.time.Month.class);
  53. timeseries1.add(new Month(2, 2001), 129.59999999999999D);
  54. timeseries1.add(new Month(3, 2001), 123.2D);
  55. timeseries1.add(new Month(4, 2001), 117.2D);
  56. timeseries1.add(new Month(5, 2001), 124.09999999999999D);
  57. timeseries1.add(new Month(6, 2001), 122.59999999999999D);
  58. timeseries1.add(new Month(7, 2001), 119.2D);
  59. timeseries1.add(new Month(8, 2001), 116.5D);
  60. timeseries1.add(new Month(9, 2001), 112.7D);
  61. timeseries1.add(new Month(10, 2001), 101.5D);
  62. timeseries1.add(new Month(11, 2001), 106.09999999999999D);
  63. timeseries1.add(new Month(12, 2001), 110.3D);
  64. timeseries1.add(new Month(1, 2002), 111.7D);
  65. timeseries1.add(new Month(2, 2002), 111D);
  66. timeseries1.add(new Month(3, 2002), 109.59999999999999D);
  67. timeseries1.add(new Month(4, 2002), 113.2D);
  68. timeseries1.add(new Month(5, 2002), 111.59999999999999D);
  69. timeseries1.add(new Month(6, 2002), 108.8D);
  70. timeseries1.add(new Month(7, 2002), 101.59999999999999D);
  71. TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
  72. timeseriescollection.addSeries(timeseries);
  73. timeseriescollection.addSeries(timeseries1);
  74. return timeseriescollection;
  75. }
  76. public ChartPanel getChartPanel(){
  77. return frame1;
  78. }
  79. }


效果图如下:

再来看一下主方法

  1. import java.awt.GridLayout;
  2. import javax.swing.JFrame;
  3. public class mainClass {
  4. public static void main(String args[]){
  5. JFrame frame=new JFrame("Java数据统计图");
  6. frame.setLayout(new GridLayout(2,2,10,10));
  7. frame.add(new BarChart().getChartPanel()); //添加柱形图
  8. frame.add(new BarChart1().getChartPanel()); //添加柱形图的另一种效果
  9. frame.add(new PieChart().getChartPanel()); //添加饼状图
  10. frame.add(new TimeSeriesChart().getChartPanel()); //添加折线图
  11. frame.setBounds(50, 50, 800, 600);
  12. frame.setVisible(true);
  13. }
  14. }


五, 总结

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

ps:如运行本文工程项目是出现错误,请参考博文:http://blog.csdn.net/pzhtpf/article/details/7506135

posted @ 2013-06-19 18:04  改变你我  阅读(1661)  评论(0编辑  收藏  举报