JfreeCharts intigration with struts

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
westlake91361
Posts: 1
Joined: Sat Feb 24, 2007 2:38 am

How about generating multiple charts in a jsp page?

Post by westlake91361 » Sat Feb 24, 2007 2:41 am

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]

ayoros
Posts: 3
Joined: Thu Mar 22, 2007 10:49 am

Post by ayoros » Thu Mar 22, 2007 2:07 pm

thank you so much. I owe you one week of hard work ;)

JSimmon
Posts: 3
Joined: Thu Mar 29, 2007 11:33 am

Post by JSimmon » Thu Mar 29, 2007 11:40 am

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?

JSimmon
Posts: 3
Joined: Thu Mar 29, 2007 11:33 am

Post by JSimmon » Thu Mar 29, 2007 1:24 pm

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);

JSimmon
Posts: 3
Joined: Thu Mar 29, 2007 11:33 am

Post by JSimmon » Fri Mar 30, 2007 12:12 pm

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.

Kyaw
Posts: 13
Joined: Wed May 23, 2007 10:34 am
Location: Singapore

Error of adding jfreechart library to Netbeans Struts

Post by Kyaw » Wed May 23, 2007 10:50 am

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!

Kyaw
Posts: 13
Joined: Wed May 23, 2007 10:34 am
Location: Singapore

Post by Kyaw » Fri May 25, 2007 3:53 am

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. :D

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.

Kyaw
Posts: 13
Joined: Wed May 23, 2007 10:34 am
Location: Singapore

JFreeChart Drill Down Feature in Struts

Post by Kyaw » Tue May 29, 2007 3:07 pm

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>.
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.

Kyaw
Posts: 13
Joined: Wed May 23, 2007 10:34 am
Location: Singapore

Solution to MSIE refreshing issue

Post by Kyaw » Fri Jun 15, 2007 4:39 am

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:

Code: Select all

           response.setContentType("image/png");
            response.setHeader("Cache-Control","no-cache");
            ServletUtilities.sendTempFile(filename, response);

Kyaw
Posts: 13
Joined: Wed May 23, 2007 10:34 am
Location: Singapore

Solution to drill down feature in Struts

Post by Kyaw » Fri Jun 15, 2007 4:41 am


irwn
Posts: 1
Joined: Thu May 01, 2008 6:22 pm

Multiple chart, any solution?

Post by irwn » Thu May 01, 2008 6:24 pm

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.

Harshi
Posts: 1
Joined: Thu Jan 08, 2009 10:35 am

Post by Harshi » Thu Jan 08, 2009 11:30 am

hi
I want to add Cewolf to a Struts application, I am new to Cewolf as well as JFreeChart. Can somebody help ?

Thanks in advance,
Harshi

Jagadeesh
Posts: 1
Joined: Thu Feb 05, 2009 7:18 am

Post by Jagadeesh » Thu Feb 05, 2009 7:21 am

I was struggling a lot to create Charts using Struts for two days.

Thank you all for helping me in creating charts using Struts.

Regards,
Jagadeesh

Locked