到 Google 资讯主页   
EasyJF首页   资料   源码   软件    论坛   网站    
   使用帮助    
    该信息为本站MyRSS系统缓存内容,部分图片及附件有可能无法正常使用.easyjf.comwww.matrix.org.cn无关,不对该信息负责.通过http://www.matrix.org.cn//resource/article/43/43948_JFreeChart.html访问该信息的原始内容.
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   
在Struts中用JFreeChart生成图表
作者:bobby080910 来源:www.matrix.org.cn  发布时间:2006-02-22 17:50:55.717

版权声明:任何获得Matrix授权的网站,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明
作者:bobby
原文:http://www.matrix.org.cn/resource/article/43/43948_JFreeChart.html
关键字:Struts;JFreeChart


1.类中方法
引入必要的包:

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.servlet.ServletUtilities;


int n = orgCourseList.size();//List的长度
int[] Num = new int[n];//存放人数
String[] orgName = new String[n];//存放机构名称
String title = "参加人数统计图";

for (int i = 0; i < n; i++) {
RepVteachStatic temp = new RepVteachStatic();//临时存放对象,视情况可变。RepVteachStatic是一个DTO(数据传输对象)
temp = (RepVteachStatic) orgCourseList.get(i);//取得List中的对象
int personNum = repVteachStaticService.getOrgCoursePersonNum(temp);
//查询出参加人数
Num[i] = personNum;
orgName[i] = temp.getOrg_name();
}
getOrgCoursePieChart(Num, orgName, title);//用三个参数调用图表生成方法


图表生成方法:

public void getOrgCoursePieChart(int[] Num, String[] orgName, String title) {

        DefaultPieDataset data = new DefaultPieDataset();

        for (int i = 0, n = Num.length; i < n; i++) {
            data.setValue(orgName[i], Num[i]);
        }

        JFreeChart chart = ChartFactory.createPieChart3D(title, data, true,false, false);
        chart.setBackgroundPaint(java.awt.Color.green);
        ChartRenderingInfo info = new ChartRenderingInfo(
                new StandardEntityCollection());
        try {        
HttpSession session = ActionContext.getActionContext().getRequest()
.getSession();//取session对象,视情况取法会有变化
              HttpServletRequest request = ActionContext.getActionContext().getRequest();
//取request对象,视情况取法会有变化
              String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,info, session);

            graphURL = request.getContextPath()
                    + "/servlet/DisplayChart?filename=" + filename;
            }catch (Exception e) {}
}


2.页面
<img src="<bean:writename="repVteachStaticBean" property="graphURL"/>" >


资源
·Matrix-Java开发者社区:http://www.matrix.org.cn/

 
相关文章
 
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   


EasyJF.com 2006 隐私政策 使用EasyJF前必读