Post
by rajesh_bhadu » Thu Oct 05, 2006 1:39 pm
I have done this integration. Here is my code for those who need help
1. write an action in which you want to craete a chart like this
public ActionForward execute(
ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException
{
LoginForm frm = (LoginForm) form;
JFreeChart chart = drawLineGraphProgress();
BufferedImage img = chart.createBufferedImage(508,204);
HttpSession session = request.getSession();
session.setAttribute("img",img);
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
String filename = ServletUtilities.saveChartAsPNG(chart,508,204,info,session);
session.setAttribute("filename",filename);
frm.setImage(filename);
return mapping.findForward("success");
}
private JFreeChart drawLineGraphProgress() {
TimeSeries t = new TimeSeries("Plan %",Day.class);
t.add(new Day(1,5,2006),20);
t.add(new Day(1,6,2006),30);
t.add(new Day(1,7,2006),50);
t.add(new Day(1,8,2006),80);
t.add(new Day(1,9,2006),100);
TimeSeries t2 = new TimeSeries("Actual %",Day.class);
t2.add(new Day(1,5,2006),20);
t2.add(new Day(1,6,2006),25);
t2.add(new Day(1,7,2006),40);
t2.add(new Day(1,8,2006),70);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(t);
dataset.addSeries(t2);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Project Progress",
"Date",
"Progress",
dataset,true,true,false);
return chart;
}
2. in your jsp use this type of code to display image
------------
<%@page import="com.tac.opms.form.LoginForm" %>
<jsp:useBean id="loginfrm" scope = "page" class ="com.tac.opms.form.LoginForm" />
-------------------------------------------------------
--------------------------------------------------------
<html:img page="/displayimage.do" paramName="loginfrm" paramProperty="image" paramId="image"/>
------------------------------------------------------
------------------------------------------------
3. write another action that is referred by html:image tag and put code in that action like as
public ActionForward execute(
ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException
{
LoginForm frm = (LoginForm) form;
response.reset();
HttpSession session = request.getSession();
String fname = (String) session.getAttribute("filename");
System.out.println("---------"+fname+"---------");
response.setContentType("image/png");
ServletUtilities.sendTempFile(fname,response);
return null;
}
i think above code will help all the guys who want to use struts and jfree chart........one more thing don't forget......put all the action mapping properly in struts-config.xml