首页 > 其他 > 详细

struts-JFreechart的整合

时间:2015-01-04 22:41:03      阅读:325      评论:0      收藏:0      [点我收藏+]

1:导入架包jfreechart-1.0.9.jar

2:写action

package actions;



import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import bean.Vote;
import bean.VoteOption;

import com.opensymphony.xwork2.ActionSupport;

import dao.VoteDAO;
import dao.VoteOptionDAO;
import daoFactory.VoteDAOFactory;
import daoFactory.VoteOptionDAOFactory;


public class VoteResultAction extends ActionSupport {
    private JFreeChart chart;
    private int voteID;//投票ID
    
    public int getVoteID() {
        return voteID;
    }

    public void setVoteID(int voteID) {
        this.voteID = voteID;
    }

    public JFreeChart getChart() {
        VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();//获得VoteDAO实例
        VoteOptionDAO voteOptionDAO = 
            VoteOptionDAOFactory.getVoteOptionDAOInstance();//获得voteOption实例
        //根据投票ID得到的投票
        Vote vote = voteDAO.findVoteById(voteID);
        String voteName = vote.getVoteName();    //得到投票名称
        
        //根据投票ID得到所有的投票选项
        List<VoteOption> voteOptions = voteOptionDAO.findVoteOptionByVoteID(voteID);
        
        DefaultCategoryDataset dcd = new DefaultCategoryDataset();//数据源
        
        //设置数据
        for(VoteOption voteOption : voteOptions) {
            dcd.setValue(voteOption.getTicketNum(),"",voteOption.getVoteOptionName());
        }
        //使用工厂类创建柱状图
        JFreeChart chart = ChartFactory.createBarChart3D(
                voteName,
                "投票选项",
                "投票数", 
                dcd,
                PlotOrientation.VERTICAL ,
                false, 
                true,
                false);
        
        return chart;
    }

    public void setChart(JFreeChart chart) {
        this.chart = chart;
    }
    
    public String execute() throws Exception {
        return SUCCESS;
    }
}

配置信息:

<action name="voteResult" class="actions.VoteResultAction">
             <result name="success" type="chart">
                 <param name="width">400</param>
                 <param name="height">300</param>
             </result>
         </action>

 

struts-JFreechart的整合

原文:http://www.cnblogs.com/aicpcode/p/4202313.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!