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