程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 在Struts中用JFreeChart生成圖表

在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"/>" >

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved