JFreeChart 柱状图参数设置方法

来源:互联网 发布:身份证读卡器破解软件 编辑:程序博客网 时间:2024/05/01 07:13
Java代码
  1. import java.awt.Color;   
  2. import java.awt.Font;   
  3. import java.text.DecimalFormat;   
  4.   
  5. import org.jfree.chart.JFreeChart;   
  6. import org.jfree.chart.axis.CategoryAxis;   
  7. import org.jfree.chart.axis.CategoryLabelPositions;   
  8. import org.jfree.chart.axis.NumberAxis;   
  9. import org.jfree.chart.axis.ValueAxis;   
  10. import org.jfree.chart.labels.ItemLabelAnchor;   
  11. import org.jfree.chart.labels.ItemLabelPosition;   
  12. import org.jfree.chart.labels.StandardCategoryToolTipGenerator;   
  13. import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;   
  14. import org.jfree.chart.plot.CategoryPlot;   
  15. import org.jfree.chart.renderer.category.BarRenderer;   
  16. import org.jfree.chart.renderer.category.BarRenderer3D;   
  17. import org.jfree.chart.title.TextTitle;   
  18. import org.jfree.ui.TextAnchor;  

 

主代码:

 

Java代码
  1. /**
  2. * 格式化 JFreeChart 输出图片使用
  3. *
  4. *   
  5. * Author : 李斌
  6. * Date    : Nov 26, 2009
  7. * Time    : 11:50:41 AM
  8. * Version: 1.0
  9. */  
  10. public class FormatPic {   
  11.     /**
  12.       * 格式化纵向柱状图使用
  13.       *
  14.       * @param chart
  15.       * @returnType: void
  16.       * @author:
  17.       * @data: Nov 26, 2009
  18.       * @time: 11:51:26 AM
  19.       */  
  20.     public static void setView(JFreeChart chart){   
  21. //       初始化字体   
  22.          Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 14);   
  23.          Font noFont = new Font("SansSerif", Font.TRUETYPE_FONT, 48);   
  24. //       格式化 图片     
  25.          CategoryPlot plot = chart.getCategoryPlot();   
  26. //         没有数据是显示的消息   
  27.          plot.setNoDataMessage("没有数据!");   
  28. ////         没有数据时显示的消息字体   
  29.          plot.setNoDataMessageFont(noFont);   
  30. ////         没有数据时显示的消息颜色   
  31.          plot.setNoDataMessagePaint(Color.RED);     
  32. //      // 数据轴精度   
  33.          NumberAxis vn = (NumberAxis) plot.getRangeAxis();   
  34. //      // 数据轴数据标签的显示格式   
  35.          CategoryAxis domainAxis = plot.getDomainAxis();   
  36. //           
  37. //      // 设置刻度必须从0开始   
  38.          vn.setAutoRangeIncludesZero(true);   
  39. ////         设置纵坐标数据精度   
  40. ////         DecimalFormat df = new DecimalFormat("#0.00");   
  41. ////         vn.setNumberFormatOverride(df);   
  42. //           
  43. //           
  44. //      x轴设置   
  45.          domainAxis.setLabelFont(labelFont);// 轴标题   
  46.          domainAxis.setTickLabelFont(labelFont);// 轴数值   
  47. //      y轴设置   
  48.          ValueAxis rangeAxis = plot.getRangeAxis();   
  49.          rangeAxis.setLabelFont(labelFont);   
  50.          rangeAxis.setTickLabelFont(labelFont);   
  51. //         三维设置   
  52.          BarRenderer3D renderer = new BarRenderer3D();   
  53. //         // 设置柱子宽度   
  54.          renderer.setMaximumBarWidth(0.05);   
  55. //         // 设置柱子高度   
  56. //         renderer.setMinimumBarLength(0.2);   
  57. //         // 设置柱子边框颜色   
  58.          renderer.setBaseOutlinePaint(Color.BLACK);   
  59. //         // 设置距离图片左端距离   
  60.          domainAxis.setLowerMargin(0.01);   
  61. //         // 设置距离图片右端距离   
  62. //         domainAxis.setUpperMargin(0.2);   
  63. //           
  64. //       // 设置显示位置   
  65. ////         plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);   
  66. ////         plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);   
  67. //   
  68. //         plot.setDomainAxis(domainAxis);   
  69. //         // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)   
  70. //         plot.setBackgroundPaint(new Color(255, 255, 204));   
  71.            
  72.            
  73. //         //设置柱子上数值的字体   
  74. //         renderer.setItemLabelFont(new Font("宋体",Font.PLAIN,14));   
  75. //         renderer.setItemLabelsVisible(true);         
  76. //         //设置柱子上数据的颜色   
  77. //         renderer.setItemLabelPaint(Color.RED);   
  78.            
  79. //         指定分类的数据标签的字体   
  80.          renderer.setSeriesItemLabelFont(3,labelFont);        
  81. //         指定分类的数据标签的字体颜色   
  82.          renderer.setSeriesItemLabelPaint(3,Color.RED);     
  83.      
  84.   
  85.   
  86.            
  87. //           
  88.            
  89. //设置柱子上比例数值的显示,如果按照默认方式显示,数值为方向正常显示   
  90.            
  91.         //设置柱子上显示的数据旋转90度,最后一个参数为旋转的角度值/3.14   
  92.            ItemLabelPosition itemLabelPosition= new ItemLabelPosition(   
  93.            ItemLabelAnchor.INSIDE12,TextAnchor.CENTER_RIGHT,   
  94.            TextAnchor.CENTER_RIGHT,-1.57D);   
  95.            
  96.         //下面的设置是为了解决,当柱子的比例过小,而导致表示该柱子比例的数值无法显示的问题   
  97.              
  98.         //设置不能在柱子上正常显示的那些数值的显示方式,将这些数值显示在柱子外面   
  99.            ItemLabelPosition itemLabelPositionFallback=new ItemLabelPosition(   
  100.            ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT,   
  101.            TextAnchor.HALF_ASCENT_LEFT,-1.57D);   
  102.            
  103.         //设置正常显示的柱子label的position   
  104.          renderer.setPositiveItemLabelPosition(itemLabelPosition);   
  105.          renderer.setNegativeItemLabelPosition(itemLabelPosition);   
  106.            
  107.         //设置不能正常显示的柱子label的position   
  108.          renderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);   
  109.          renderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);   
  110.         // 显示每个柱的数值,并修改该数值的字体属性   
  111.          renderer.setIncludeBaseInRange(true);   
  112.          renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());   
  113.          renderer.setBaseItemLabelsVisible(true);   
  114.            
  115.            
  116.       //以下设置,将按照指定格式,制定内容显示每个柱的数值。可以显示柱名称,所占百分比   
  117. //         renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",new DecimalFormat("0.0%")));       
  118.   
  119.            
  120.            
  121. //            // 横轴上的label旋转90 度   
  122. //         domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);     
  123.   
  124.            
  125. //      // 设置每个平行柱之间距离   
  126.          renderer.setItemMargin(0.05);   
  127. //   
  128.            
  129.   
  130.            
  131.          plot.setRenderer(renderer);   
  132.      }  

 

Java代码

  1. /**   
  2.       * 配置字体   
  3.       * @param chart JFreeChart 对象   
  4.       */      
  5.      public static void configFont(JFreeChart chart){      
  6.          // 配置字体      
  7.           Font xfont = new Font("宋体",Font.PLAIN,14) ;// X轴      
  8.           Font yfont = new Font("宋体",Font.PLAIN,14) ;// Y轴      
  9.           Font kfont = new Font("宋体",Font.PLAIN,14) ;// 底部      
  10.           Font titleFont = new Font("隶书", Font.BOLD , 25) ; // 图片标题      
  11.           CategoryPlot plot = chart.getCategoryPlot();// 图形的绘制结构对象      
  12.               
  13.          // 图片标题      
  14.           chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));      
  15.               
  16.          // 底部      
  17.           chart.getLegend().setItemFont(kfont);      
  18.               
  19.          // X 轴      
  20.           CategoryAxis domainAxis = plot.getDomainAxis();          
  21.           domainAxis.setLabelFont(xfont);// 轴标题      
  22.           domainAxis.setTickLabelFont(xfont);// 轴数值        
  23.           domainAxis.setTickLabelPaint(Color.BLUE) ; // 字体颜色      
  24.           domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的label斜显示        
  25.               
  26.          // Y 轴      
  27.           ValueAxis rangeAxis = plot.getRangeAxis();          
  28.           rangeAxis.setLabelFont(yfont);        
  29.           rangeAxis.setLabelPaint(Color.BLUE) ; // 字体颜色      
  30.           rangeAxis.setTickLabelFont(yfont);        
  31.               
  32.       } 
原创粉丝点击