Trottoarne
Trottoarne

Reputation: 37

JFREECHART with servlet

I'm trying to get JFREECHART to create a png for some hard coded data into a chart. I get no errors at all and the program I run through the creation part but I do not get a png.

anyone that can see what the problem is?

my servlet

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String action = request.getParameter("Action");
    if(action.equals("Home"))
        getServletContext().getRequestDispatcher(home).forward(request, response);

    else{
        session = request.getSession(true);
        user = (User) session.getAttribute("User");
        eventList = rh.getEventList(user);
        request.setAttribute("EventList", eventList);
        getServletContext().getRequestDispatcher(result).forward(request, response);

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.setValue(30, "Girls","SCIENCE CLASS");
        dataset.setValue(30,  "Boys","SCIENCE CLASS");
        dataset.setValue(10,  "Girls","ECONOMICS CLASS");
        dataset.setValue(50, "Boys","ECONOMICS CLASS");
        dataset.setValue(5, "Girls","LANGUAGE CLASS");
        dataset.setValue(55, "Boys","LANGUAGE CLASS");

        JFreeChart chart = ChartFactory.createLineChart(
                "Comparison between Girls and Boys in Science," + "Economics and Language classes",
                "Students Comparisons", "No of Students",
                dataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false);


            try {

                final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
                final File file1 = new File(getServletContext().getRealPath(".") + "/images/lineChart.png");

                ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
            } catch (Exception e) {
                System.out.println(e);

            }
    }
}

and my jsp

                <div>
                <img src="path/images/lineChart.png"
                     width="600" height="400" border="0" usemap="#chart" />
            </div>

Upvotes: 0

Views: 607

Answers (1)

Alan Hay
Alan Hay

Reputation: 23226

For this to work the Servlet would have to be called prior to the JSP being rendered. However there are various other flaws with the approach you have taken.

I would suggest that rather than writing the generated chart to a file, stream it directly to the Servlet's response stream:

http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/ChartUtilities.html#writeChartAsPNG-java.io.OutputStream-org.jfree.chart.JFreeChart-int-int-

 try {
    final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
    ChartUtilities.writeChartAsPNG(response, chart, 600, 400, info);
} catch (Exception e) {
    System.out.println(e);
}

Then you just point the <img/> tag to your Servlet:

<div>
    <img src="pathToMyServlet" width="600" height="400" border="0" usemap="#chart" />
 </div>

Upvotes: 1

Related Questions