JFreechart -servlet problem

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Joel Cohen

JFreechart -servlet problem

Post by Joel Cohen » Thu Dec 19, 2002 9:14 am

I've been able to generate charts from regular java programs, but now I'm trying to git it working for servlets. I have the two jar files in /var/tomcat4/common/lib where they belong, and I've modified the web.xml to include:

<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>com.jrefinery.chart.servlet.DisplayChart</servlet-class>
</servlet>

according to the instructions in DisplayChart.

In my program, I'm trying to generate a chart, then write it out to disk as a .jpg file.

JFreeChart chart = ChartFactory.createPieChart("Security Breakdown", data, true);

chart.setBackgroundPaint(Color.yellow);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionLabelType(PiePlot.NAME_AND_PERCENT_LABELS);
plot.setNoDataMessage("No data available");
plot.setToolTipGenerator(new StandardPieToolTipGenerator());


I get the following messages when this executes:


java.lang.InternalError: Can't connect to X11 window server using ':0' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.(X11GraphicsEnvironment.java:77)
at java.lang.Class.forName1(Native Method)
at java.lang.Class.forName(Class.java:142)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:72)
at java.awt.Font.initializeFont(Font.java:285)
at java.awt.Font.(Font.java:319)
at com.jrefinery.chart.PiePlot.(Unknown Source)
at com.jrefinery.chart.ChartFactory.createPieChart(Unknown Source)
at com.jrefinery.chart.ChartFactory.createPieChart(Unknown Source)
at appraisalReport.doGet(appraisalReport.java:455)


Any hints?

David Gilbert

Re: JFreechart -servlet problem

Post by David Gilbert » Thu Dec 19, 2002 4:45 pm

Take a look at this thread:

http://www.object-refinery.com/phorum-3 ... 783&t=2987

Regards,

DG

Joel Cohen

Re: JFreechart -servlet problem

Post by Joel Cohen » Thu Dec 19, 2002 8:27 pm

Okay. The environment is RH 8 and tomcat4. I've installed Xvfb, I started vnc (on display 2, and with the same user starting tomcat) and I've added the following lines to /etc/tomcat4.conf:

CATALINA_OPTS="-Djava.awt.headless=true"
echo $CATALINA_OPTS
echo $CATALINA_HOME
DISPLAY=:2
echo $DISPLAY

When I start tomcat, I see the following:

[root@localhost init.d]# ./tomcat4 start
-Djava.awt.headless=true
/var/tomcat4
:2

Now, when I run the app, I get the same message, but instead of display :0, I see display :2

java.lang.InternalError: Can't connect to X11 window server using ':2' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.(X11GraphicsEnvironment.java:77)
at java.lang.Class.forName1(Native Method)
at java.lang.Class.forName(Class.java:142)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:72)
at java.awt.Font.initializeFont(Font.java:285)
at java.awt.Font.(Font.java:319)
at com.jrefinery.chart.PiePlot.(Unknown Source)

Joel Cohen

Re: JFreechart -servlet problem

Post by Joel Cohen » Thu Dec 19, 2002 11:43 pm

I finally got it working. Tomcat's JAVA_HOME was pointing to an older JDK that's on the box.

Now, I have another question. I can create charts in Tomcat's TEMP directory successfully. I want to set up an image link in the page I'm building. How can I access files in the temp directory?

Joel Cohen

Please help - I'm soooo close.

Post by Joel Cohen » Fri Dec 20, 2002 5:01 am

Okay. I know I'm pretty close. I'm creating .jpeg's in the temp directory ok. I have the following two entries in web.xml:

<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>com.jrefinery.chart.servlet.DisplayChart</servlet-class>
</servlet>

and

<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart</url-pattern>
</servlet-mapping>

In my program, I have the following:

JFreeChart chart = ChartFactory.createPieChart("Security Breakdown", data, true);
HttpSession session = request.getSession();
out.println(rb.getString("sessions.id") + " " + session.getId());
chart.setBackgroundPaint(Color.yellow);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionLabelType(PiePlot.NAME_AND_PERCENT_LABELS);
plot.setNoDataMessage("No data available");
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
String filename = "";
try {
filename = ServletUtilities.saveChartAsJPEG(chart, 600, 300, null, session);
DisplayChart dc = new DisplayChart();
String nf = "/ " + filename; // Note there is a blank after the slash

dc.init();

out.println("<img src=\"/servlet/DisplayChart?filename=" + nf + "\" width=500 height=300 border=0 usemap=#" + nf + ">");

The page comes up ok, but the image is missing. When I right-click on it to do a "save Image", I get the following message:

javax.servlet.ServletException: File '/var/tomcat4/temp/ jfreechart-8320.jpeg' does not exist

There is a blank space before jfreechart....

I've tried every conceivable combination of characters here. What am I doing wrong?

Richard Atkinson

Re: JFreechart -servlet problem

Post by Richard Atkinson » Fri Dec 20, 2002 10:23 am

Why are you putting a blank space before the filename? It seems like you have gone to some extra effort to break your code. Take a look at the example WAR I put together if you still can't solve it. It gives examples of how to use the ServletUtilities class and DisplayChart servlet in combination. You can download it from http://homepage.ntlworld.com/richard_c_ ... freechart/.

Regards,
Richard...

Eulogio Robles

Re: JFreechart -servlet problem

Post by Eulogio Robles » Sat Dec 21, 2002 5:50 pm

I'm having the same "X11" problem, on redhat 7.3 with IPlanet 6.0 and JDK 1.4.1. I'm using Cewolf taglib.
Even the example WAR's from Cewolf and JFreeChart do not work.

I added the -Djava.awt.headless=true on the JSP/JVM options but it does not work. Is there anything else I must do?

Eulogio Robles

Re: JFreechart -servlet problem

Post by Eulogio Robles » Sat Dec 21, 2002 6:18 pm

Xvfb did the trick for now. But I'd prefer something less complicated (I had to modify IPlanet "start" script to setup the DISPLAY variable.

Joel Cohen

Re: JFreechart -servlet problem

Post by Joel Cohen » Sat Dec 21, 2002 10:31 pm

I have a couple of JDK's installed on my machine. I was (inadvertently) using an older one - 1.3. When I changed Tomcat' configuration to use the 1.4 version, the X11 problem stopped, by specifying the "headless" option

Joel Cohen

Still won't display correctly

Post by Joel Cohen » Sun Dec 22, 2002 2:06 am

Ok. I've worked your code into my program, using pie_chart._jsp.java as a (very rough) model:

JFreeChart chart = ChartFactory.createPieChart("Security Breakdown", data, true);
HttpSession session = request.getSession();
Chart.setBackgroundPaint(Color.yellow);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionLabelType(PiePlot.NAME_AND_PERCENT_LABELS);
plot.setNoDataMessage("No data available");
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
String filename = "";
try {
filename = ServletUtilities.saveChartAsPNG(chart, 600, 300, null, session);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
out.write("<img src=\"");
out.print( graphURL );
out.write("\" width=600 height=300 border=0 ");
out.write(">\r\n\r\n\t");


The program compiles ok, and the chart is written out to the temp directory successfuly.

However, when the page displays, the chart isn't on it. In the page source, I get:

<img src="/portfolio/servlet/DisplayChart?filename=jfreechart-45050.png" width=600 height=300 border=0 >

Which looks pretty good (the docroot is /portfolio);

But, if I try to view the chart by right-clicking on it, I get the following message:

HTTP Status 404 - /portfolio/servlet/DisplayChart

type Status report

message /portfolio/servlet/DisplayChart

description The requested resource (/portfolio/servlet/DisplayChart) is not available.

Ok. Some more. I downloaded yout jfreechart-sample.war and installed it and it works just fine. (nice piece of work). I copied the two .jar files into portfolio/WEB-INF/lib

Joel Cohen

Re: JFreechart -servlet problem

Post by Joel Cohen » Sun Dec 22, 2002 2:34 am

I finally got it. My web.xml wasn't set up correctly (I'm still learning Tomcat). But that's how you learn.

Thanks for the help. Your example was especially helpful.

Locked