JFreeChart p60 servlet example not working

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

JFreeChart p60 servlet example not working

Post by Kenny Dubuisson » Tue Oct 01, 2002 8:54 pm

I'm finally purchased the docs to help me and I'm trying to get the servlet example on page 60 of the manual to work. I can't get the servlet to compile; it give me the following error:

#javac ChartTest.java
ChartTest.java:17: cannot resolve symbol
symbol : variable DemoDatasetFactory
location: class ChartTest
CategoryDataset data = DemoDatasetFactory.createCategoryDataset();
^
1 error

The code is:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.ChartUtilities;
import com.jrefinery.data.CategoryDataset;

public class ChartTest extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
OutputStream out = response.getOutputStream();
try
{
CategoryDataset data = DemoDatasetFactory.createCategoryDataset();
JFreeChart chart = ChartFactory.createVerticalBarChart("Bar Chart",
"Category", "Value", data, true);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
}
catch (Exception e)
{
System.err.println(e.toString());
}
finally
{
out.close();
}
}

Thanks,
Kenny

David Gilbert

Re: JFreeChart p60 servlet example not working

Post by David Gilbert » Tue Oct 01, 2002 11:09 pm

Hi Kenny,

You need to give the Java compiler some clues about where to find the DemoDatasetFactory class, by (a) specifying a classpath and (b) adding an import statement to your code.

The DemoDatasetFactory class is part of the com.jrefinery.chart.demo package, and is located in the jfreechart-0.9.3-demo.jar file. So you need to use:

javac -classpath path/to/jfreechart-0.9.3-demo.jar ChartTest.java

...to compile your sample code. Also, if your ChartTest class is in another package, then you need to add:

import com.jrefinery.chart.demo.DemoDatasetFactory;

...to your source file.

Regards,

DG

Kenny Dubuisson

Re: JFreeChart p60 servlet example not working

Post by Kenny Dubuisson » Wed Oct 02, 2002 5:26 pm

Ahh...that was missing in the manual. Thanks for the info. Now I get my Java servlet to compile but now it won't display anything when I run it in my browser. No errors but no chart. The code is below:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.ChartUtilities;
import com.jrefinery.data.CategoryDataset;
import com.jrefinery.chart.demo.DemoDatasetFactory;

public class ChartTest extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
OutputStream out = response.getOutputStream();
try
{
CategoryDataset data = DemoDatasetFactory.createCategoryDataset();
JFreeChart chart = ChartFactory.createVerticalBarChart("Bar Chart",
"Category", "Value", data, true);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
}
catch (Exception e)
{
System.err.println(e.toString());
}
finally
{
out.close();
}
}

Any ideas?

Locked