jfreechart使用柱状图重叠

来源:互联网 发布:建站abc教程 编辑:程序博客网 时间:2024/03/29 16:55


 

 

此篇主要用到重叠效果

关键代码:

[java] view plain copy print?
  1.  CategoryPlot plot = chart.getCategoryPlot();//获得图表区域对象  
  2.   
  3.          //设置图表的纵轴和横轴org.jfree.chart.axis.CategoryAxis  
  4.   
  5.          org.jfree.chart.axis.CategoryAxis domainAxis = plot.getDomainAxis();  
  6.   
  7.          domainAxis.setLowerMargin(0.1);//设置距离图片左端距离此时为10%  
  8.   
  9.          domainAxis.setUpperMargin(0.1);//设置距离图片右端距离此时为百分之10  
  10.   
  11.          domainAxis.setCategoryLabelPositionOffset(10);//图表横轴与标签的距离(10像素)  
  12.   
  13.          domainAxis.setCategoryMargin(0.2);//横轴标签之间的距离20%  
  14.   
  15.          domainAxis.setMaximumCategoryLabelLines(1);  
  16.   
  17.          domainAxis.setMaximumCategoryLabelWidthRatio(0);  
  18.   
  19.   
  20.   
  21.          //设定柱子的属性  
  22.   
  23. //         org.jfree.chart.axis.ValueAxis rangeAxis = plot.getRangeAxis();  
  24. //  
  25. //         rangeAxis.setUpperMargin(0.1);//设置最高的一个柱与图片顶端的距离(最高柱的10%)  
  26. //  
  27. //  
  28. //  
  29. //         //设置图表的颜色  
  30. //  
  31.          org.jfree.chart.renderer.category.BarRenderer renderer;  
  32.   
  33.          renderer = new org.jfree.chart.renderer.category.BarRenderer();  
  34. //  
  35. //         renderer.setBaseOutlinePaint(Color.red);  
  36. //  
  37. //         renderer.setSeriesPaint(0, new Color(0, 255, 255));//计划柱子的颜色为青色  
  38. //  
  39. //         renderer.setSeriesOutlinePaint(0,Color.BLACK);//边框为黑色  
  40. //  
  41. //         renderer.setSeriesPaint(1, new Color(0, 255, 0));//实报柱子的颜色为绿色  
  42. //  
  43. //         renderer.setSeriesOutlinePaint(1,Color.red);//边框为红色  
  44. //  
  45.             renderer.setItemMargin(-0.1);//组内柱子间隔为组宽的10%  
  46. //  
  47. //         //显示每个柱的数值,并修改该数值的字体属性  
  48. //  
  49.          renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());  
  50. //  
  51.          renderer.setItemLabelFont(new Font("黑体",Font.BOLD,12));//12号黑体加粗  
  52. //  
  53.          renderer.setItemLabelPaint(Color.black);//字体为黑色  
  54. //  
  55.          renderer.setItemLabelsVisible(true);  
  56.          NumberFormat nf= NumberFormat.getNumberInstance();  
  57.           StandardCategoryToolTipGenerator generator=new StandardCategoryToolTipGenerator(StandardCategoryToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT_STRING,nf);  
  58.           renderer.setToolTipGenerator(generator);  
  59.          plot.setRenderer(renderer);//使用我们设计的效果  


 

 

整个代码:

[java] view plain copy print?
  1. /** 
  2.  *  
  3.  */  
  4. package com.huaxia.bank.test;  
  5.   
  6. import java.awt.Color;  
  7. import java.awt.Dimension;  
  8. import java.awt.Font;  
  9. import java.text.NumberFormat;  
  10.   
  11. import org.jfree.chart.ChartFactory;  
  12. import org.jfree.chart.ChartPanel;  
  13. import org.jfree.chart.JFreeChart;  
  14. import org.jfree.chart.axis.AxisLocation;  
  15. import org.jfree.chart.axis.CategoryAxis;  
  16. import org.jfree.chart.axis.CategoryLabelPositions;  
  17. import org.jfree.chart.axis.ValueAxis;  
  18. import org.jfree.chart.labels.CategoryToolTipGenerator;  
  19. import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;  
  20. import org.jfree.chart.labels.StandardCategoryToolTipGenerator;  
  21. import org.jfree.chart.plot.CategoryPlot;  
  22. import org.jfree.chart.plot.PlotOrientation;  
  23. import org.jfree.chart.title.TextTitle;  
  24. import org.jfree.data.category.DefaultCategoryDataset;  
  25. import org.jfree.ui.ApplicationFrame;  
  26. import org.jfree.ui.RefineryUtilities;  
  27.   
  28. /** 
  29.  * @author cuiran 
  30.  * 
  31.  */  
  32. public class BarExample5 extends ApplicationFrame {  
  33.      //字体配置方法(解决中文问题)  
  34.     private static void configFont(JFreeChart chart) {  
  35.         // 配置字体  
  36.         Font xfont = new Font("宋体", Font.PLAIN, 12);// X轴  
  37.         Font yfont = new Font("宋体", Font.PLAIN, 12);// Y轴  
  38.         Font kfont = new Font("宋体", Font.PLAIN, 12);// 底部  
  39.         Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题  
  40.         CategoryPlot plot = chart.getCategoryPlot();// 图形的绘制结构对象  
  41.   
  42.         // 图片标题  
  43.         chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));  
  44.   
  45.         // 底部  
  46.         chart.getLegend().setItemFont(kfont);  
  47.   
  48.         // X 轴  
  49.         CategoryAxis domainAxis = plot.getDomainAxis();  
  50.         domainAxis.setLabelFont(xfont);// 轴标题  
  51.         domainAxis.setTickLabelFont(xfont);// 轴数值  
  52.         domainAxis.setTickLabelPaint(Color.BLUE); // 字体颜色  
  53.         domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的label斜显示  
  54.   
  55.         // Y 轴  
  56.         ValueAxis rangeAxis = plot.getRangeAxis();  
  57.         rangeAxis.setLabelFont(yfont);  
  58.         rangeAxis.setLabelPaint(Color.BLUE); // 字体颜色  
  59.         rangeAxis.setTickLabelFont(yfont);  
  60.   
  61.     }  
  62.     public BarExample5(String title) {  
  63.         super(title);  
  64.           
  65.         DefaultCategoryDataset dataset = new DefaultCategoryDataset();  
  66.         dataset.addValue(23.0"2012年2月""机构 1");  
  67.         dataset.addValue(50.0"2012年2月""机构 2");  
  68.         dataset.addValue(30.0"2012年2月""机构 3");  
  69.         dataset.addValue(26.0"2012年3月""机构 1");  
  70.         dataset.addValue(37.0"2012年3月""机构 2");  
  71.         dataset.addValue(28.0"2012年3月""机构 3");  
  72. //      dataset.addValue(21.0, "风险 3", "机构 1");  
  73. //      dataset.addValue(67.0, "风险 3", "机构 2");  
  74. //      dataset.addValue(18.0, "风险 3", "机构 3");  
  75.         JFreeChart chart = ChartFactory.createBarChart("2012年2月-2012年3月机构风险数量统计表"// chart  
  76.                                                                             // title  
  77.                 "机构名称"// domain axis label  
  78.                 "数量"// range axis label  
  79.                 dataset, // data  
  80.                 PlotOrientation.VERTICAL, // orientation  
  81.                 true// include legend  
  82.                 true// tooltips?  
  83.                 false // URLs?  
  84.                 );  
  85.         configFont(chart);  
  86.           
  87.          CategoryPlot plot = chart.getCategoryPlot();//获得图表区域对象  
  88.   
  89.          //设置图表的纵轴和横轴org.jfree.chart.axis.CategoryAxis  
  90.   
  91.          org.jfree.chart.axis.CategoryAxis domainAxis = plot.getDomainAxis();  
  92.   
  93.          domainAxis.setLowerMargin(0.1);//设置距离图片左端距离此时为10%  
  94.   
  95.          domainAxis.setUpperMargin(0.1);//设置距离图片右端距离此时为百分之10  
  96.   
  97.          domainAxis.setCategoryLabelPositionOffset(10);//图表横轴与标签的距离(10像素)  
  98.   
  99.          domainAxis.setCategoryMargin(0.2);//横轴标签之间的距离20%  
  100.   
  101.          domainAxis.setMaximumCategoryLabelLines(1);  
  102.   
  103.          domainAxis.setMaximumCategoryLabelWidthRatio(0);  
  104.   
  105.   
  106.   
  107.          //设定柱子的属性  
  108.   
  109. //         org.jfree.chart.axis.ValueAxis rangeAxis = plot.getRangeAxis();  
  110. //  
  111. //         rangeAxis.setUpperMargin(0.1);//设置最高的一个柱与图片顶端的距离(最高柱的10%)  
  112. //  
  113. //  
  114. //  
  115. //         //设置图表的颜色  
  116. //  
  117.          org.jfree.chart.renderer.category.BarRenderer renderer;  
  118.   
  119.          renderer = new org.jfree.chart.renderer.category.BarRenderer();  
  120. //  
  121. //         renderer.setBaseOutlinePaint(Color.red);  
  122. //  
  123. //         renderer.setSeriesPaint(0, new Color(0, 255, 255));//计划柱子的颜色为青色  
  124. //  
  125. //         renderer.setSeriesOutlinePaint(0,Color.BLACK);//边框为黑色  
  126. //  
  127. //         renderer.setSeriesPaint(1, new Color(0, 255, 0));//实报柱子的颜色为绿色  
  128. //  
  129. //         renderer.setSeriesOutlinePaint(1,Color.red);//边框为红色  
  130. //  
  131.             renderer.setItemMargin(-0.1);//组内柱子间隔为组宽的10%  
  132. //  
  133. //         //显示每个柱的数值,并修改该数值的字体属性  
  134. //  
  135.          renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());  
  136. //  
  137.          renderer.setItemLabelFont(new Font("黑体",Font.BOLD,12));//12号黑体加粗  
  138. //  
  139.          renderer.setItemLabelPaint(Color.black);//字体为黑色  
  140. //  
  141.          renderer.setItemLabelsVisible(true);  
  142.          NumberFormat nf= NumberFormat.getNumberInstance();  
  143.           StandardCategoryToolTipGenerator generator=new StandardCategoryToolTipGenerator(StandardCategoryToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT_STRING,nf);  
  144.           renderer.setToolTipGenerator(generator);  
  145.          plot.setRenderer(renderer);//使用我们设计的效果  
  146. //  
  147. //  
  148. //  
  149. //         //设置纵横坐标的显示位置  
  150. //  
  151. //         plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);//学校显示在下端(柱子竖直)或左侧(柱子水平)  
  152. //  
  153. //         plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); //人数显示在下端(柱子水平)或左侧(柱子竖直)  
  154.   
  155.         ChartPanel chartPanel = new ChartPanel(chart, false);  
  156.         chartPanel.setPreferredSize(new Dimension(800570));  
  157.         setContentPane(chartPanel);  
  158.     }  
  159.   
  160.     /** 
  161.      *  
  162.      */  
  163.     private static final long serialVersionUID = 1L;  
  164.   
  165.     /** 
  166.      * @param args 
  167.      */  
  168.     public static void main(String[] args) {  
  169.         BarExample5 demo = new BarExample5("机构风险数量统计表");  
  170.         demo.pack();  
  171.         RefineryUtilities.centerFrameOnScreen(demo);  
  172.         demo.setVisible(true);  
  173.   
  174.     }  
  175.   
  176. }