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
JFreeChart p60 servlet example not working
Re: JFreeChart p60 servlet example not working
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
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
Re: JFreeChart p60 servlet example not working
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?
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?