JfreeCharts intigration with struts

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
rajesh_bhadu
Posts: 6
Joined: Thu Sep 28, 2006 11:36 am

JfreeCharts intigration with struts

Post by rajesh_bhadu » Tue Oct 03, 2006 7:32 am

Hi,
I am new to jfreechart.
I want to draw a chart as jpeg image and want to populate it in jsp by using struts.
can any body help me.........please provide some example.
thanx in advance
Rajesh :oops: :oops:

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Post by matinh » Tue Oct 03, 2006 11:56 am

Have a look at cewolf:
http://cewolf.sourceforge.net/

It provides a servlet and a tag library which allows easy integration of charts into JSPs.

hth,
- martin

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Oct 03, 2006 12:02 pm

I can't add anything to what matinh said - I know almost nothing about Struts.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

rajesh_bhadu
Posts: 6
Joined: Thu Sep 28, 2006 11:36 am

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

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Post by matinh » Thu Oct 05, 2006 1:56 pm

The code you just posted may be working, but why didn't you use cewolf which does all this for you? Just write your JSP and a dataset producer as explained on the above mentione homepage.

- martin

rajesh_bhadu
Posts: 6
Joined: Thu Sep 28, 2006 11:36 am

Post by rajesh_bhadu » Fri Oct 06, 2006 6:36 am

Actually in home page of cewolf they give all the example for servlets.
I am using struts.
If you can give me some example for integration of cewolf with struts,it'll be glad for me.
I am waitting for your reply.

register_anand
Posts: 3
Joined: Thu Sep 28, 2006 7:52 am

Post by register_anand » Fri Oct 06, 2006 6:50 am

Cewolf is a very handy and easy tag lib for rendering charts. But the problem is it doesnot give flexibilty to change look and feel of the charts beyond certain points. Secondly it is not in sync with latest versions of jfree charts.

regards,

Anand.

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Post by matinh » Fri Oct 06, 2006 7:18 am

@register_anand: Cewolf gives you complete control over the chart. Just use a ChartPostProcessor. You can write a ChartPostProcessor class which gets a chart object and has full access to it.

An regarding the jfree chart version: you don't have to use an older version, just replace the jfreechart.jar file with a newer one and it works (at least for me).

@rajesh_bhadu: Struts is just another servlet as well. Where is the problem with using different servlets? Just add another entry in your web.xml descriptor. At the moment I'm working with OpenCms, struts and JFreeChart/Cewolf which are 3 servlets interacting fine with each other.

Struts makes use of action-classes which perform whatever work has to be done. Each action-class forwards to a JSP at the end of its work. In this JSP you can use cewolf.

Don't know what else I could tell you. If you come up with any concrete questions I'd gladly answer them.

- martin

rajesh_bhadu
Posts: 6
Joined: Thu Sep 28, 2006 11:36 am

Post by rajesh_bhadu » Fri Oct 06, 2006 8:29 am

Ya you r right i have done all the stuff using cwolf.
But can you tell me how can i draw a piechart and barchart using this tag lib mean cwolf.

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Post by matinh » Fri Oct 06, 2006 8:48 am

I'm afraid piecharts are not yet supported by cewolf, even if I think adding support wouldn't be that hard.

A barchart can be drawn by using the type "verticalbar" in your JSP. Something like this:

Code: Select all

<cewolf:chart 
    id="bar" 
    title="Page View Statistics" 
    type="verticalbar"                       <-- this determines the chart type
    xaxislabel="Page" 
    yaxislabel="Views">
    <cewolf:data>
        <cewolf:producer id="producer"/>
    </cewolf:data>
</cewolf:chart>
For a list of supported type see the documentation in the file cewolf.tld for the corresponding tag or have a look at the sources (de.laures.cewolf.taglib.PlotTypes).

hth,
- martin

rajesh_bhadu
Posts: 6
Joined: Thu Sep 28, 2006 11:36 am

Post by rajesh_bhadu » Fri Oct 06, 2006 9:03 am

Thanx martin.
Cwolf support pie chat also i have done this work.
thanx again now i m able to do all the stuff with cwolf.

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Post by matinh » Fri Oct 06, 2006 9:20 am

If you patched cewolf, maybe you can post the patch somewhere on cewolf's homepage so others can take advantage of this, too. Or if it's not possible (I'm afraid cewolf's maintainer is not actively maintaing anymore) post the patch here, so at least it is available for people sumbling over the same problem.

- martin

rajesh_bhadu
Posts: 6
Joined: Thu Sep 28, 2006 11:36 am

Post by rajesh_bhadu » Fri Oct 06, 2006 10:33 am

Hi,
I am getting such type of exception with cewolf.

IOException while loading persisted sessions: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: de.laures.cewolf.util.RenderedImage
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: de.laures.cewolf.util.RenderedImage
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1278)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1845)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)
at org.apache.catalina.session.StandardSession.readObject(StandardSession.java:1357)
at org.apache.catalina.session.StandardSession.readObjectData(StandardSession.java:835)
at org.apache.catalina.session.StandardManager.load(StandardManager.java:393)
at org.apache.catalina.session.StandardManager.start(StandardManager.java:608)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3526)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:774)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:760)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:548)
at org.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:260)
at org.apache.catalina.core.StandardHost.install(StandardHost.java:741)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:512)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:354)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:671)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1149)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:707)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1141)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:316)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:2143)
at org.apache.catalina.startup.Catalina.start(Catalina.java:463)
at org.apache.catalina.startup.Catalina.execute(Catalina.java:350)
at org.apache.catalina.startup.Catalina.process(Catalina.java:129)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:156)
Caused by: java.io.NotSerializableException: de.laures.cewolf.util.RenderedImage
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1054)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1330)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1302)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1245)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1052)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
at org.apache.catalina.session.StandardSession.writeObject(StandardSession.java:1429)
at org.apache.catalina.session.StandardSession.writeObjectData(StandardSession.java:852)
at org.apache.catalina.session.StandardManager.unload(StandardManager.java:489)
at org.apache.catalina.session.StandardManager.stop(StandardManager.java:644)
at org.apache.catalina.core.StandardContext.stop(StandardContext.java:3625)
at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:989)
at org.apache.catalina.core.StandardHostDeployer.remove(StandardHostDeployer.java:423)
at org.apache.catalina.core.StandardHost.remove(StandardHost.java:821)
at org.apache.catalina.startup.HostConfig.undeployApps(HostConfig.java:711)
at org.apache.catalina.startup.HostConfig.stop(HostConfig.java:691)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:313)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.stop(ContainerBase.java:1174)
at org.apache.catalina.core.ContainerBase.stop(ContainerBase.java:1186)
at org.apache.catalina.core.StandardService.stop(StandardService.java:507)
at org.apache.catalina.core.StandardServer.stop(StandardServer.java:2178)
at org.apache.catalina.startup.Catalina.start(Catalina.java:494)
... 7 more



above exception is not effecting my output image is coming(chart) but in log file this exception is logged. Can you tell me why this is coming.

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Post by matinh » Fri Oct 06, 2006 10:46 am

This seems like a bug to me. RenderedImage is not serializable but another class (ChartImageDefinition) which is serializable has an attribute of this type.

Don't know what to do, never got this exception.

- martin

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Post by matinh » Mon Oct 16, 2006 9:48 am

Oops, I also get this exception, just missed/ignored it as it only happens when restarting tomcat. However, I found this posting in a cewolf forum: http://sourceforge.net/forum/forum.php? ... _id=192228

Even if I don't use a clustered environemnt at the moment, changing the cewolf-servlet's init param 'storage' to the following fixed my problem:

Code: Select all

    <init-param>
        <param-name>storage</param-name>
        <param-value>de.laures.cewolf.storage.ClusterableSessionStorage</param-value> 
    </init-param>
Originally, the value of 'storage' was set to be 'de.laures.cewolf.storage.TransientSessionStorage', as described in cewolf's documentation. Unfortunately cewolf development seems pretty inactive and questions in the forum stay mostly unanswered :-(

hth,
- martin

Locked