JFreeChart实例,带详细注释

最近用到了JFreeChart,现将实例代码贴出来,大家可以参考一下,代码中如有错误或可以改进的地方,还请大家指正。

通过下面的代码,可以很清晰地看出JFreeChart的结构,核心即为chart, plot, XXXAxis, renderer,了解了它们的常用方法后,
会发现其实JFreeChart使用起来是很简单方便的。废话不多说了,还是直接看示例吧。
 
1.柱状图

  1 /** */ /**
  2      * 生成图像文件
  3      * 
  4      *  @param  session
  5      *            httpsession
  6      *  @param  w
  7      *            生成的图的宽度
  8      *  @param  h
  9      *            生成的图的高度
 10      *  @return  
 11      *              生成的图像文件的路径
 12       */

 13      public  String createBarChart(HttpSession session, 
 14                                  String title,  //  图片标题
 15                                   int  w,  //  图片宽度
 16                                   int  h,  //  图片高度
 17                                  CategoryDataset dataset  // 用于出图的数据集
 18                                  )  {
 19         
 20          // 通过ChartFactory的静态方法获取JFreeChart对象
 21         JFreeChart chart  =  ChartFactory.createBarChart(title,       // 图片标题
 22                                                           null ,      // 横坐标标题
 23                                                           null ,      // 纵坐标标题
 24                                                          dataset,   // 用于出图的数据集
 25                                                          PlotOrientation.VERTICAL,   // 柱子方向
 26                                                           true ,      // 是否包含Legend
 27                                                           false ,     // 是否包含tooltips
 28                                                           false );    // 是否包含urls
 29         chart.setBackgroundPaint(Color.WHITE);
 30
 31          //  设置标题字体
 32         chart.getTitle().setFont( new  Font( " 宋体 " , Font.CENTER_BASELINE,  12 ));
 33
 34         CategoryPlot plot  =  chart.getCategoryPlot();
 35
 36          /**/ /* //没有数据时显示的消息
 37         plot.setNoDataMessage("数据还未录入!");
 38         plot.setNoDataMessageFont(new Font("宋体", Font.CENTER_BASELINE, 15)); */

 39         
 40          // 横坐标设置
 41         CategoryAxis domainAxis  =  plot.getDomainAxis();
 42          // 设置横坐标上显示各个业务子项的字体 
 43         domainAxis.setTickLabelFont( new  Font( " 宋体 " , Font.PLAIN,  9 ));
 44         plot.setDomainAxis(domainAxis);
 45
 46          // 纵坐标设置
 47         ValueAxis rangeAxis  =  (ValueAxis)plot.getRangeAxis();
 48          //  设置最高的一个 Item 与图片顶端的距离
 49         rangeAxis.setUpperMargin( 0.15 );
 50          //  设置最低的一个 Item 与图片底端的距离
 51         rangeAxis.setLowerMargin( 0.15 );
 52         plot.setRangeAxis(rangeAxis);
 53
 54         BarRenderer renderer  =   new  BarRenderer();
 55         renderer.setBaseOutlinePaint(Color.BLACK);
 56          /**/ /* // 设置图上的文字
 57         renderer.setSeriesItemLabelFont(0, font);
 58         renderer.setSeriesItemLabelFont(1, font); */

 59          //  设置legend的字体
 60         renderer.setBaseLegendTextFont( new  Font( " 宋体 " , Font.LAYOUT_RIGHT_TO_LEFT,  10 ));
 61          //  设置 Wall 的颜色
 62          // renderer.setWallPaint(Color.gray);
 63          //  设置每种柱的颜色
 64         renderer.setSeriesPaint( 0 new  Color( 133 , 210 , 38 ));
 65         renderer.setSeriesPaint( 1 new  Color( 0 , 131 , 249 ));
 66          // 设置柱的 Outline 颜色
 67         renderer.setSeriesOutlinePaint( 0 , Color.BLACK);
 68         renderer.setSeriesOutlinePaint( 1 , Color.BLACK);
 69          //  设置每个平行柱的之间距离
 70         renderer.setItemMargin( 0.03 );
 71         
 72          //  显示每个柱的数值,并修改该数值的字体属性
 73         renderer.setBaseItemLabelGenerator( new  StandardCategoryItemLabelGenerator());
 74          // 注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题 
 75         renderer.setBasePositiveItemLabelPosition( new  ItemLabelPosition( 
 76         ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); 
 77          //  设置柱形图上的文字偏离值 
 78         renderer.setItemLabelAnchorOffset(10D);
 79         renderer.setBaseItemLabelsVisible( true );
 80         renderer.setBaseItemLabelFont( new  Font( " Times New Roman " , Font.PLAIN,  9 ));
 81         
 82         plot.setRenderer(renderer);
 83
 84          //  设置柱的透明度
 85         plot.setForegroundAlpha( 0.6f );
 86          //  设置横坐标和纵坐标的显示位置
 87         plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
 88         plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
 89
 90          //  获取图片路径
 91          //  图片存于临时文件夹下
 92         String filename  =   null ;
 93          try   {
 94             filename  =  ServletUtilities.saveChartAsPNG(chart, w, h,  null ,
 95                     session);
 96         }
  catch  (IOException e)  {
 97             System.out.println( " Exception -  "   +  e.toString());
 98             e.printStackTrace(System.out);
 99             filename  =   " public_error_500x300.png " ;
100         }

101          return  filename;
102     }



2.曲线图

 1      /** */ /**
 2      * 生成图像文件
 3      * 
 4      *  @param  session
 5      *            httpsession
 6      *  @param  data1
 7      *            IntervalXYDataset
 8      *  @param  w
 9      *            生成的图的宽度
10      *  @param  h
11      *            生成的图的高度
12      *  @return
13      *                  生成的图像文件的路径
14       */

15      public  String createStepLineChart(
16             HttpSession session,
17             String title,  //  图片标题
18             IntervalXYDataset data1, // 数据集
19              int  w,  //  图片宽度
20              int  h  //  图片高度
21             )  {
22
23          /**/ /*
24          * 任何类型的图表的最终表现形式都是在JFreeChart对象进行一些属性的定制。
25          * JFreeChart引擎本身提供了一个工厂类用于创建不同类型的图表对象
26           */

27         JFreeChart chart  =  ChartFactory.createXYStepChart(title,  //  chart title
28                                                            null
29                                                            null
30                                                           data1,  //  data
31                                                           PlotOrientation.VERTICAL, 
32                                                            true //  include legend
33                                                            false
34                                                            false );
35
36          //  设置背景颜色为白色
37         chart.setBackgroundPaint(Color.white);
38
39          //  设置标题字体
40         chart.getTitle().setFont( new  Font( " 宋体 " , Font.CENTER_BASELINE,  12 ));
41         XYPlot plot  =  chart.getXYPlot();
42         
43         plot.setBackgroundPaint(Color.lightGray);
44         plot.setDomainGridlinePaint(Color.white);
45         plot.setRangeGridlinePaint(Color.white);
46
47         DateAxis domainAxis  =   new  DateAxis();
48         domainAxis.setDateFormatOverride( new  SimpleDateFormat( " MM-dd " ));
49         domainAxis.setTickMarkPosition(DateTickMarkPosition.START);
50         domainAxis.setLowerMargin( 0 );
51         domainAxis.setUpperMargin( 0 );
52         plot.setDomainAxis(domainAxis);
53
54         NumberAxis rangeAxis  =  (NumberAxis)plot.getRangeAxis();
55          //  设置最高的一个 Item 与图片顶端的距离
56         rangeAxis.setUpperMargin( 0.15 );
57          //  设置最低的一个 Item 与图片底端的距离
58         rangeAxis.setLowerMargin( 0.15 );
59         plot.setRangeAxis(rangeAxis);
60
61          /**/ /*
62          * XXXRender:负责如何显示一个图表对象
63           */

64         XYStepRenderer renderer  =  (XYStepRenderer)plot.getRenderer();
65          /**/ /*
66          * 设置线条的颜色
67           */

68         renderer.setSeriesPaint( 0 new  Color( 0 , 150 , 240 ));
69         renderer.setSeriesPaint( 1 new  Color( 244 , 109 , 19 ));
70
71          //  设置图上的文字字体
72         Font font  =   new  Font( " 宋体 " , Font.LAYOUT_RIGHT_TO_LEFT,  10 );
73         renderer.setSeriesItemLabelFont( 0 , font);
74         renderer.setSeriesItemLabelFont( 1 , font);
75          //  设置legend的字体
76         renderer.setBaseLegendTextFont(font);
77         renderer.setBaseItemLabelFont(font);
78
79         plot.setRenderer(renderer);
80
81         String filename  =  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值