jfreechart创建饼图

 /**
  * 生成饼图
  * @param keys    section的名称
  * @param values  section的值
  * @param title   饼图的标题
  * @param session 
  * @return        生成的饼图的文件名
  */
 public String createPieChart(List keys, List values ,String title, HttpSession session){
  DefaultPieDataset data = new DefaultPieDataset();
  if(keys!=null && keys.size()>0 && values!=null && values.size()>0)
  for(int i=0;i<keys.size();i++){
   Comparable key = (Comparable)keys.get(i);
   Number value = (Number)values.get(i);
   data.setValue(key, value); 
  }
  PiePlot3D pieplot3d = new PiePlot3D(data);
  JFreeChart chart = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, pieplot3d, true);

  //设置开始角度
  pieplot3d.setStartAngle(150D);
  //设置方向为”顺时针方向“
  pieplot3d.setDirection(Rotation.CLOCKWISE);
  //设置透明度,0.5F为半透明,1为不透明,0为全透明
  pieplot3d.setForegroundAlpha(0.5F);
  pieplot3d.setNoDataMessage("无数据显示");// 没有数据的时候显示的内容
  //pieplot3d.setExplodePercent(arg0, arg1);
pieplot3d.setLabelGenerator(new StandardPieSectionLabelGenerator(
    ("{0}: ({2})"), NumberFormat.getNumberInstance(),
    new DecimalFormat("0.00%")));

  chart.setBackgroundPaint(java.awt.Color.white);//可选,设置图片背景色
  
  chart.setTitle(title);//可选,设置图片标题
  
  ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
  
  String filename="";
  try {
   //500是图片长度,300是图片高度
   filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);
   //graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
  } catch (IOException e) {
   e.printStackTrace();
  }
  return filename;
 }

 

其中new StandardPieSectionLabelGenerator(
("{0}: ({1},{2})"), NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%"))
    的("{0}: ({1},{2})")是生成的格式,{0}表示section名,{1}表示section的值,{2}表示百分比。可以自定义。而new DecimalFormat("0.00%")表示小数点后保留两位。

 

在jsp页面中引用的方法:

<%

 String filename=tem.createPieChart(keys,values,"统计图",session);

 String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;

%>

 

<img src="<%= graphURL %>" width=500 height=300 border=0 >

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值