在Struts顶用JFreeChart生成图表[Java编程]
本文“在Struts顶用JFreeChart生成图表[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
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"/>" >
以上是“在Struts顶用JFreeChart生成图表[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |