贤仁居 George Gong
It's never too late to learn
posts - 32,comments - 16,trackbacks - 0

以下代码用到了大多数方法(附有注释):

 private String createBarChart(String[] series,String[] categories,double[][] data,HttpSession session,PrintWriter printWriter){
         
       CategoryDataset dataset 
= DatasetUtilities.createCategoryDataset(series, categories, data);
  
    JFreeChart chart 
= ChartFactory.createBarChart3D("预算统计图""预算科目","实际执行额",
             dataset,PlotOrientation.VERTICAL,
true,true,false);
       
       chart.setBackgroundPaint(Color.WHITE);   
//设定背景色为白色
       
       CategoryPlot categoryPlot 
= chart.getCategoryPlot();  //获得 plot:CategoryPlot!!
       
       categoryPlot.setBackgroundPaint(Color.lightGray); 
//设定图表数据显示部分背景色
       
       categoryPlot.setDomainGridlinePaint(Color.white); 
//横坐标网格线白色
       categoryPlot.setDomainGridlinesVisible(true); //可见
       
       categoryPlot.setRangeGridlinePaint(Color.white); 
//纵坐标网格线白色
     
       CategoryAxis domainAxis 
= categoryPlot.getDomainAxis();
       
       
//设置 categoryAxis 垂直显示
     domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.4));
     
     
//设置哼坐标轴的标题字体,此处是“预算科目”
        domainAxis.setLabelFont(new Font("SansSerif",Font.PLAIN,13));
       
       
//设置距离图片左端距离,参数为图片的百分比
       domainAxis.setLowerMargin(0.05);
       
       
//设置距离图片右端距离
       domainAxis.setUpperMargin(0.05);
           
       
//设置横坐标的坐标值的字体
       domainAxis.setTickLabelFont(new Font("SansSerif",Font.PLAIN,12));
       
       categoryPlot.setDomainAxis(domainAxis); 
            
       ValueAxis rangeAxis 
= categoryPlot.getRangeAxis();
       
//设置最高的一个柱与图片顶端的距离
       rangeAxis.setUpperMargin(0.05);
       
       
//设置最低的一个柱与图片底端的距离
       rangeAxis.setLowerMargin(0.05);
       categoryPlot.setRangeAxis(rangeAxis); 
       
       
//设置竖坐标标签的旋转角度
       rangeAxis.setLabelAngle(0.05);
       
       
//设置竖坐标轴的坐标值字体
//       rangeAxis.setTickLabelFont(new Font("SansSerif",Font.PLAIN,12));
  
       BarRenderer3D renderer
=(BarRenderer3D) categoryPlot.getRenderer();
       
       renderer.setBaseOutlinePaint(Color.GREEN);
       
//设置 Wall 的颜色
       renderer.setWallPaint(Color.PINK);
       
       
//设置每个柱的颜色     
       GradientPaint gradientpaint = new GradientPaint(0.0F0.0F, Color.blue,
                
0.0F0.0Fnew Color(0064)); //设定特定颜色
  GradientPaint gradientpaint1 = new GradientPaint(0.0F0.0F, Color.green,
           
0.0F0.0Fnew Color(0640));
 
       renderer.setSeriesPaint(
0, gradientpaint);
       renderer.setSeriesPaint(
1, gradientpaint1);
       
       
//设置柱的 Outline 颜色
       renderer.setSeriesOutlinePaint(0, Color.BLACK);
       renderer.setSeriesOutlinePaint(
1, Color.BLACK);
       
//设置每个category所包含的平行柱的之间距离
       renderer.setItemMargin(0.1);
    
       
//显示每个柱的数值,并修改该数值的字体属性
       renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
       
       
//设置柱子上数值的字体
       renderer.setItemLabelFont(new Font("SansSerif",Font.PLAIN,13));  
       renderer.setItemLabelsVisible(
true);
     
       
//设置柱子上数据的颜色
       renderer.setItemLabelPaint(Color.RED);
       
       
//设置bar的最小宽度,以保证能显示数值
       renderer.setMinimumBarLength(0.02);
       
       
//最大宽度
       renderer.setMaximumBarWidth(0.07);
       
       
       
//设置柱子上显示的数据旋转90度,最后一个参数为旋转的角度值/3.14
       ItemLabelPosition itemLabelPosition= new ItemLabelPosition(
         ItemLabelAnchor.INSIDE12,TextAnchor.CENTER_RIGHT,
         TextAnchor.CENTER_RIGHT,
-1.57D);
       
       
//设置不能在柱子上正常显示的那些数值的显示方式,将这些数值显示在柱子外面
       ItemLabelPosition itemLabelPositionFallback=new ItemLabelPosition(
         ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT,
         TextAnchor.HALF_ASCENT_LEFT,
-1.57D);
       
       
//设置正常显示的柱子label的position
       renderer.setPositiveItemLabelPosition(itemLabelPosition);
       renderer.setNegativeItemLabelPosition(itemLabelPosition);
       
       
//设置不能正常显示的柱子label的position
       renderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);
       renderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);
       
       categoryPlot.setRenderer(renderer);
       
//设置柱子的透明度
       categoryPlot.setForegroundAlpha(0.8f);
       
       
//设置地区、销量的显示位置
//       categoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
//       categoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); 
      
       StandardEntityCollection sec 
= new StandardEntityCollection(); 
       ChartRenderingInfo info 
= new ChartRenderingInfo(sec);
             
       
try {
         ServletUtilities.setTempFilePrefix(
"zcz");
         ServletUtilities.setTempOneTimeFilePrefix(
"zcz-onetime");
        
           filename 
= ServletUtilities.saveChartAsPNG(chart, 926555, info, session);   
           chartName
=filename;
           
           System.out.println(
"图片的临时保存路径:"+System.getProperty("java.io.tmpdir"));
       }
 catch (IOException ex) {
           ex.printStackTrace();
       }

          
           String myMap
=ChartUtilities.getImageMap(filename, info);
           printWriter.println(myMap);
    
     
return filename;
    }
posted on 2007-07-23 10:47 George Gong 阅读(1333) 评论(2)  编辑  收藏 所属分类: JAVA&&J2EE

FeedBack:
# re: jfreechart 柱型图应用
2007-12-03 15:58 | 罗夕
谢谢分享  回复  更多评论
  
# re: jfreechart 柱型图应用[未登录]
2008-05-14 11:57 | a
hao  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: