现在的位置: 首页 > 综合 > 正文

JFreeChart 之四:JFreeChartUtil

2013年04月05日 ⁄ 综合 ⁄ 共 6612字 ⁄ 字号 评论关闭

 

Java代码
  1. import java.awt.Color;   
  2. import java.awt.Font;   
  3. import java.text.DecimalFormat;   
  4. import java.text.NumberFormat;   
  5.   
  6. import org.jfree.chart.JFreeChart;   
  7. import org.jfree.chart.axis.CategoryAxis;   
  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.StandardCategoryItemLabelGenerator;   
  13. import org.jfree.chart.labels.StandardPieSectionLabelGenerator;   
  14. import org.jfree.chart.labels.StandardXYItemLabelGenerator;   
  15. import org.jfree.chart.plot.CategoryPlot;   
  16. import org.jfree.chart.plot.PiePlot3D;   
  17. import org.jfree.chart.plot.XYPlot;   
  18. import org.jfree.chart.renderer.category.BarRenderer3D;   
  19. import org.jfree.chart.renderer.xy.XYItemRenderer;   
  20. import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;   
  21. import org.jfree.ui.RectangleInsets;   
  22. import org.jfree.ui.TextAnchor;   
  23. import org.jfree.util.Rotation;   
  24.   
  25. public class JFreeChartUtil {   
  26.   
  27.     /* 折线图样式 */  
  28.     public static void timeSeriesStyle(JFreeChart chart) {   
  29.         XYPlot plot  = chart.getXYPlot();   
  30.         /* 设置曲线显示各数据点的值  */    
  31.         XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();   
  32.         //设置网格背景颜色  
  33.         plot.setBackgroundPaint(Color.white);   
  34.         //设置网格竖线颜色  
  35.         plot.setDomainGridlinePaint(Color.pink);   
  36.         //设置网格横线颜色  
  37.         plot.setRangeGridlinePaint(Color.pink);   
  38.         //设置曲线图与xy轴的距离  
  39.         plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));   
  40.         //设置曲线是否显示数据点  
  41.         xylineandshaperenderer.setBaseShapesVisible(true);   
  42.         //设置曲线显示各数据点的值  
  43.         XYItemRenderer xyitem = plot.getRenderer();     
  44. //      xyitem.setBaseItemLabelsVisible(true);    
  45.         xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));   
  46.         xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());   
  47.         xyitem.setBaseItemLabelFont(new Font("Dialog"114));
      
  48.         plot.setRenderer(xyitem);   
  49.            
  50.         // 中文处理乱码  
  51.         Font xfont = new Font("宋体",Font.PLAIN,12) ;// X轴    
  52.         Font yfont = new Font("宋体",Font.PLAIN,12) ;// Y轴    
  53.         // X 轴    
  54.         ValueAxis domainAxis = plot.getDomainAxis();   
  55.         domainAxis.setLabelFont(xfont);// 轴标题    
  56.         domainAxis.setTickLabelFont(xfont);// 轴数值      
  57.         domainAxis.setTickLabelPaint(Color.BLUE) ; // 字体颜色    
  58.         // Y 轴    
  59.         ValueAxis rangeAxis = plot.getRangeAxis();        
  60.         rangeAxis.setLabelFont(yfont);      
  61.         rangeAxis.setLabelPaint(Color.BLUE) ; // 字体颜色    
  62.         rangeAxis.setTickLabelFont(yfont);       
  63.              
  64.         // 底部    
  65.         chart.getLegend().setItemFont( new Font("宋体",Font.PLAIN,12));   
  66.     }   
  67.        
  68.     /* 饼图样式 */  
  69.     public static void piePlot3DStyle(JFreeChart chart) {   
  70.         PiePlot3D plot = (PiePlot3D) chart.getPlot();   
  71.            
  72.         plot.setLabelFont(new Font("宋体"012));   
  73.         // 图片中显示百分比:默认方式    
  74.         //plot.setLabelGenerator(new           StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));    
  75.         // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位    
  76.         plot.setLabelGenerator(new StandardPieSectionLabelGenerator(   
  77.                 "{0}={1}({2})", NumberFormat.getNumberInstance(),   
  78.                 new DecimalFormat("0.00%")));   
  79.         // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例                    
  80.         plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(   
  81.                 "{0}={1}({2})"));   
  82.         // 设置背景色为白色     
  83.         chart.setBackgroundPaint(Color.white);   
  84.            
  85.         // 指定图片的透明度(0.0-1.0)     
  86.         // plot.setForegroundAlpha(1.0f);  
  87.         //设置透明度,0.5F为半透明,1为不透明,0为全透明  
  88.         plot.setForegroundAlpha(0.5F);   
  89.         // 指定显示的饼图上圆形(false)还椭圆形(true)     
  90.         plot.setCircular(true);   
  91.         //设置开始角度  
  92.         plot.setStartAngle(40D);   
  93.         //设置方向为”顺时针方向“  
  94.         plot.setDirection(Rotation.CLOCKWISE);   
  95.         plot.setInteriorGap(0.0D);//[7]  
  96.         //没有数据的时候显示的内容  
  97.         plot.setNoDataMessage("无数据显示");   
  98.         plot.setNoDataMessageFont(new Font("宋体"012));
      
  99.         plot.setLabelGap(0.02D);   
  100.         // 设置饼图背景色  
  101.         plot.setBackgroundPaint(Color.white);   
  102.            
  103.     }   
  104.     /* 柱状图样式 */  
  105.     public static void barChart3DStyle(JFreeChart chart) {   
  106.         CategoryPlot plot = chart.getCategoryPlot();   
  107.         NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();    
  108.          CategoryAxis domainAxis = plot.getDomainAxis();     
  109.          /*------设置X轴坐标上的文字-----------*/     
  110. //       domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));    
  111.          domainAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 11));   
  112.          /*------设置X轴的标题文字------------*/     
  113.          domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));     
  114.   
  115.          /*------设置Y轴坐标上的文字-----------*/     
  116. //       numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));    
  117.          numberaxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));     
  118.          /*------设置Y轴的标题文字------------*/     
  119.          numberaxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));     
  120.   
  121.          /*------这句代码解决了底部汉字乱码的问题-----------*/     
  122.          chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));   
  123.   
  124.         //设置网格背景颜色  
  125.         plot.setBackgroundPaint(Color.white);   
  126.   
  127.         //设置网格竖线颜色  
  128.         plot.setDomainGridlinePaint(Color.pink);   
  129.   
  130.         //设置网格横线颜色  
  131.         plot.setRangeGridlinePaint(Color.pink);   
  132.   
  133.         //显示每个柱的数值,并修改该数值的字体属性  
  134.         BarRenderer3D renderer = new BarRenderer3D();   
  135.   
  136.         renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());   
  137.         renderer.setBaseItemLabelsVisible(true);   
  138.         //设置 底部分类 不显示  
  139.         renderer.setBaseSeriesVisibleInLegend(false);   
  140.         //默认的数字显示在柱子中,通过如下两句可调整数字的显示  
  141.         //注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题  
  142.   
  143.         renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));   
  144.   
  145.         renderer.setItemLabelAnchorOffset(10D);   
  146.   
  147.         renderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 12));   
  148.   
  149.         renderer.setItemLabelsVisible(true);   
  150.   
  151.         //设置每个地区所包含的平行柱的之间距离  
  152.   
  153.         renderer.setItemMargin(0.3);   
  154.   
  155.         plot.setRenderer(renderer);   
  156.   
  157.         //设置地区、销量的显示位置  
  158.         //将下方的“年”放到上方  
  159. //                  plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);  
  160.         //将默认放在左边的“人数”放到右方  
  161. //                  plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);  
  162.     }   
  163. }  

 

 

 

转载自http://xiaofengtoo.iteye.com/blog/1226856

抱歉!评论已关闭.