采用工具类ChartUtil和DisplayChart(jfreechart的servlet)输出
将javabean里的生成图片的方法写的一个工具类ChartUtil里面。
1 .添加工具类ChartUtil
public class ChartUtil {
// 产生时间序列图,返回图片名称
代码
- public static String generatePieChart(DefaultPieDataset dataset,String title,int width,int height,HttpSession session, PrintWriter pw) {
-
- String filename = null;
- try {
- if (session != null)
- {
- ChartDeleter deleter = (ChartDeleter)session.getAttribute("JFreeChart_Deleter");
- session.removeAttribute("JFreeChart_Deleter");
- session.setAttribute("JFreeChart_Deleter", deleter);
- }
- JFreeChart chart = ChartFactory.createPieChart3D(
- title,
- dataset,
- true,
- true,
- false );
-
- ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
-
-
- filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);
-
- ChartUtilities.writeImageMap(pw, filename, info,true);
- pw.flush();
- } catch (Exception e) {
- System.out.println("Exception - " + e.toString());
- e.printStackTrace(System.out);
- filename = "picture_error.png"; }
- return filename;
- }
- }
2、在action里统计数据,设置好数据集dataset。传到页面
3、 在页面里取出
代码
- DefaultPieDataset piedataset=(DefaultPieDataset)request.getAttribute("piedataset");
- String p = ChartUtil.generatePieChart(piedataset," 项目收支线图",500,300,null, new PrintWriter(out));
- String p1 = request.getContextPath() + "/servlet/DisplayChart?filename=" + p;
-
- <td><img src="<%= p1 %>" width=500 height=300 border=0 usemap="#<%= p %>"></td>
4、在web.xml中添加
代码
- <servlet>
- <servlet-name>DisplayChart</servlet-name>
- <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>DisplayChart</servlet-name>
- <url-pattern>/servlet/DisplayChart</url-pattern>
- </servlet-mapping>
posted on 2009-10-14 14:16
I LOVE JAVA 阅读(610)
评论(0) 编辑 收藏 所属分类:
java基础知识 、
java高端技术