java中使用jfree饼图的实现

 工作需要涉及到饼图的实现,这是一个实现饼图的类,通过传入HashMap中键存放的是需要显示的项目,值为对应项的数值,用来计算并显示比例,该类的代码如下:
package  com.sjth.zdsygl.chart;

import  java.awt.Font;
import  java.io.PrintWriter;
import  java.util.HashMap;
import  java.util.Iterator;

import  javax.servlet.http.HttpSession;

import  org.jfree.chart.ChartRenderingInfo;
import  org.jfree.chart.ChartUtilities;
import  org.jfree.chart.JFreeChart;
import  org.jfree.chart.entity.StandardEntityCollection;
import  org.jfree.chart.labels.StandardPieItemLabelGenerator;
import  org.jfree.chart.plot.PiePlot3D;
import  org.jfree.chart.servlet.ServletUtilities;
import  org.jfree.chart.title.TextTitle;
import  org.jfree.data.DefaultPieDataset;


public   class  PieChart
{
                     
//这个方法用来计算显示项数据所占比例
    private double getRatio(HashMap map, Object value)
    
{
        
double result = 0.00;
        
double total = 0.00;
        String strTemp 
= "";
        Iterator iter 
= map.keySet().iterator();
        
while (iter.hasNext())
        
{
            Object key 
= iter.next();
            strTemp 
= (String) map.get(key);
            total 
+= (Double.parseDouble(strTemp));
        }

        strTemp 
= (String) map.get(value);
        result 
= Double.parseDouble(strTemp) / total;
    
//    result = (double) Math.round(result);
        return result;
    }


    
public String JCreate(HashMap map, String title, HttpSession session, int w,
            
int h)
    
{
        DefaultPieDataset piedata 
= new DefaultPieDataset();
        
/* piedata.setValue("Google", 130);//如果没有数据可以通过这一块来测试
                                            piedata.setValue("Baidu", 40.3);
        piedata.setValue("sohu", 19.6);
        piedata.setValue("msn", 29.6);
*/

            
        Iterator iter 
= map.keySet().iterator();

            
while (iter.hasNext())
        
{
            Object key 
= iter.next();
            piedata.setValue(key.toString(), getRatio(map, key));
        }


        
// 3D饼图
        PiePlot3D plot = new PiePlot3D(piedata);
        
// 饼图
        
// PiePlot plot = new PiePlot(piedata);

        
// 设定链接 ("link.jsp","section"));//section为参数,不填写则默认为category
        
// plot.setURLGenerator(new StandardPieURLGenerator("link.jsp","section"));

        
// 指定图片的透明度
        plot.setForegroundAlpha(1.0f);
        plot.setDepthFactor(
0.08);

        
// 指定显示的饼图上圆形(false)还椭圆形(true)
        plot.setCircular(false);

        
// 显示百分比
        
// plot.setLabelGenerator(new
        
// StandardPieItemLabelGenerator(StandardPieItemLabelGenerator.DEFAULT_TOOLTIP_FORMAT));
        
// plot.setLegendLabelGenerator(new StandardPieItemLabelGenerator("{0}:
        
// ({1}M, {2})"));

        JFreeChart chart 
= new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot,
                
true);
        
// 设置图片背景色
        chart.setBackgroundPaint(java.awt.Color.white);

        
// 设置图片标题属性
        Font font = new Font("黑体", Font.CENTER_BASELINE, 20);
        TextTitle _title 
= new TextTitle(title);
        _title.setFont(font);
        chart.setTitle(_title);

        plot.setToolTipGenerator(
new StandardPieItemLabelGenerator());
        
// 把生成的图片放到临时目录
        StandardEntityCollection sec = new StandardEntityCollection();
        ChartRenderingInfo info 
= new ChartRenderingInfo(sec);

        String filename 
= null;
        
try
        
{
            filename 
= ServletUtilities.saveChartAsPNG(chart, w, h, info, session);
        }

        
catch (Exception e)
        
{
            System.out.println(
"Exception - " + e.toString());
            filename 
= "public_error_500x300.png";
        }

        
return filename;
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值