码迷,mamicode.com
首页 > 其他好文 > 详细

struts-JFreechart的整合

时间:2015-01-04 22:42:46      阅读:265      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!