使用JFreeChart构造统计图

public   class  ChartServlet  extends  HttpServlet
... {
    
// 请求处理方法
    public void doPost( HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException 
    
...{
        
// 得到用户的投票信息
        String id = request.getParameter( "id" );
        IVoteDAO dao 
= DAOFactory.getVoteDAO();
        Vote vote 
= dao.getVote( id );
        
        
// 得到JFreeChart对象的实例
        JFreeChart chart = getChart( vote );
        
// 设置输出对象的类型
        response.setContentType( "image/jpeg" );
        
// 输出图形
        ChartUtilities.writeChartAsJPEG( response.getOutputStream(),
            
100, chart, 400300null);
    }

    
    
// 请求处理方法
    public void doGet( HttpServletRequest request, HttpServletResponse response )
            
throws ServletException, IOException
    
...{
        doPost( request, response );
    }


    
// 得到JFreeChart对象的实例
    public static JFreeChart getChart( Vote vote )
    
...{
        JFreeChart chart 
= null;
        
// 判断是否为饼图
        if( vote.getPictype().indexOf( "PIE" ) == 0 )
        
...{
            
// 组织生成饼图的数据
            DefaultPieDataset data = new DefaultPieDataset();
            
            Iterator it 
= vote.getVoteitems().iterator();
            
while( it.hasNext() )
            
...{
                VoteItem vi 
= (VoteItem)it.next();
                data.setValue( vi.getTitle(), vi.getVotenum() );
            }

            
            
// 判断是否为普通饼图
            if"PIE".equals( vote.getPictype() ) )
            
...{
                chart 
= ChartFactory.createPieChart( vote.getTitle(),
                        data, 
falsefalsefalse );
            }

            
else    // 3D饼图
            ...{
                chart 
= ChartFactory.createPieChart3D( vote.getTitle(),
                        data, 
falsefalsefalse );
            }

        }

        
else
        
...{
            
// 组织柱状图的数据
            DefaultCategoryDataset data = new DefaultCategoryDataset();
            
            Iterator it 
= vote.getVoteitems().iterator();
            
while( it.hasNext() )
            
...{
                VoteItem vi 
= (VoteItem)it.next();
                data.addValue( vi.getVotenum(), 
"选项", vi.getTitle() );
            }


            
// 判断是否为普通柱状图
            if"BAR".equals( vote.getPictype() ) )
            
...{
                chart 
= ChartFactory.createBarChart( vote.getTitle(),
                    
"选项""数量", data, PlotOrientation.VERTICAL,
                    
falsefalsefalse );
            }

            
else    // 3D柱状图
            ...{
                chart 
= ChartFactory.createBarChart3D( vote.getTitle(),
                        
"选项""数量", data, PlotOrientation.VERTICAL,
                        
falsefalsefalse );
            }

        }

        
return chart;
    }

}
 

你可能感兴趣的:(DAO,jfreechart)