JfreeCharts intigration with struts
-
- Posts: 1
- Joined: Sat Feb 24, 2007 2:38 am
How about generating multiple charts in a jsp page?
Hi, rajesh_bhadu,
I tried your code and it worked beautifully. But I am having trouble to generate and display multiple charts in a jsp page based upon your example. Just thought have you ever done this before? If you, can you show me how you made it?
Thank you very much!!!
westlake91361
[quote="rajesh_bhadu"]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[/quote]
I tried your code and it worked beautifully. But I am having trouble to generate and display multiple charts in a jsp page based upon your example. Just thought have you ever done this before? If you, can you show me how you made it?
Thank you very much!!!
westlake91361
[quote="rajesh_bhadu"]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[/quote]
I followed the instructions by rajesh_bhadu and I successfully generated charts (thancks a lot rajesh_bhadu!!), but now I have problems refreshing the chart to display. I mean, if I change data for the chart the first action creates the updated chart and writes the file in the temp directory, but the second action is not executed so the jsp displays the former chart. If I press the refresh button then the jsp displays the new updated chart and from the log file I see that this time the second action is executed.
What has happening?
What has happening?
I found an easier solution (no second action needed), that also solve the refresh problem!
At the end of first action put this line:
session.setAttribute("filename", System.getProperty("java.io.tmpdir") + filename);
and in the jsp that will display the chart put this line:
<logic:present name="filename" scope="session">
<html:img src="<%=filename%>" align="middle" />
</logic:present>
And that's all!
Just remember to put these lines in the action in order to remove the png
files from the temp directory:
ChartDeleter cd = new ChartDeleter();
cd.addChart(filename);
At the end of first action put this line:
session.setAttribute("filename", System.getProperty("java.io.tmpdir") + filename);
and in the jsp that will display the chart put this line:
<logic:present name="filename" scope="session">
<html:img src="<%=filename%>" align="middle" />
</logic:present>
And that's all!
Just remember to put these lines in the action in order to remove the png
files from the temp directory:
ChartDeleter cd = new ChartDeleter();
cd.addChart(filename);
Error of adding jfreechart library to Netbeans Struts
Hi all,
May i know how to add JFreeChart library to Netbeans 5.5 Struts (running on bundled Tomcat) environment as every time i add them, i will get a "Servlet action is not available" error due to actionmapping cannot be found including invoking those actions without using JFreeChart library? However, after i remove it or run it in another generic JSP/Servlet environment(Bundled Tomcat also), everything resumes to normal. For your info, my JFreeChart version is 1.0.5.
Other than that, i also added JasperReport library in the same project using guideline from ( www.netbeans.org/kb/55/vwp-reports.html ). Could it due to the ant script from that site? Kindly help if it is the real root cause as i am totally new to ant script. Thanks in advance!
May i know how to add JFreeChart library to Netbeans 5.5 Struts (running on bundled Tomcat) environment as every time i add them, i will get a "Servlet action is not available" error due to actionmapping cannot be found including invoking those actions without using JFreeChart library? However, after i remove it or run it in another generic JSP/Servlet environment(Bundled Tomcat also), everything resumes to normal. For your info, my JFreeChart version is 1.0.5.
Other than that, i also added JasperReport library in the same project using guideline from ( www.netbeans.org/kb/55/vwp-reports.html ). Could it due to the ant script from that site? Kindly help if it is the real root cause as i am totally new to ant script. Thanks in advance!
Hi JSimmon
May i know how you deal with this cache problem because i tried IE6 and Opera, both need a refresh but FireFox doesn't need?
Thanks in advance for your sharing.
May i know how you deal with this cache problem because i tried IE6 and Opera, both need a refresh but FireFox doesn't need?
Thanks in advance for your sharing.

JSimmon wrote:Hi everybody, please diregard my previous post, what rajesh_bhadu suggested was all right.
I had a problem with Internet Explorer 7 cache settings, because with Firefox everything worked well, so also the second action is needed.
JFreeChart Drill Down Feature in Struts
Hi Rajesh and all
Do you have any example of the drill-down feature implemented using Struts? If yes, could you share your experience. Thanks a lot.
What i mean is how you could use usemap attribute in <html:img>.
Do you have any example of the drill-down feature implemented using Struts? If yes, could you share your experience. Thanks a lot.
What i mean is how you could use usemap attribute in <html:img>.
rajesh_bhadu wrote: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
Last edited by Kyaw on Wed May 30, 2007 2:10 am, edited 1 time in total.
Solution to MSIE refreshing issue
Hi all
I got a new way to tackle MSIE refreshing issue brought out by JSimmon. You can put this line (response.setHeader("Cache-Control","no-cache") ) in the second Action class as in the code fragment below:
I got a new way to tackle MSIE refreshing issue brought out by JSimmon. You can put this line (response.setHeader("Cache-Control","no-cache") ) in the second Action class as in the code fragment below:
Code: Select all
response.setContentType("image/png");
response.setHeader("Cache-Control","no-cache");
ServletUtilities.sendTempFile(filename, response);
Multiple chart, any solution?
Hi, I am also interested to display multiple chart in 1 page. Have anyone solve or have any idea on how to solve it?
thanks,
Irawan.
thanks,
Irawan.