Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically generate JFreeChart in servlet

I'm trying to generate graphs dynamically using JFreeChart as a result of some checkboxes the user selects, but I can't figure out how best to get the generated datasets into chart form (I have code that makes charts from these, but need to produce pngs) and into the JSP view. Currently, I can only think of sending the Datasets to the JSP, but can't think of what to do from there... How do I make it so that: user submits form to servlet, servlet generates datasets, charts produced from datasets, pngs from charts and finally pngs dispatched to jsp? Or something along those lines.

public void doPost(HttpServletRequest request,
                   HttpServletResponse response)
                    throws IOException, ServletException{

    String[] metrics     = request.getParameterValues("metrics");
    String[] fileNames   = request.getParameterValues("files");

    List<CategoryDataset> results = new ArrayList<CategoryDataset>();
    DMCalc calculator = new DMCalc(metrics, fileNames);  
    calculator.calculateResults();
    results.add(calculator.getEditDistanceDataset());
    results.add(calculator.getSimilarityDataset());
    results.add(calculator.getTimeChartDataset());

    request.setAttribute("results", results);
    RequestDispatcher view = request.getRequestDispatcher("metricResult.jsp");

    view.forward(request, response);
}

UPDATE:

By having the doPost method generate the datasets from the user post, they can then be stored in fields, subsequently the RequestDispatcher forwards the user to the JSP which then calls the servlet's doGet method in an img tag, which uses the datasets stored earlier in the fields to produce a png and that is then displayed by the HTML in the JSP.

like image 687
Robert Avatar asked Aug 10 '09 15:08

Robert


2 Answers

I would suggest that you use the ServletUtilities class. It saves in the java tempdir AND cleans up when the session is invalidated. :) Another hint for then displaying the file is to use the DisplayChart servlet to get your images. This goes in web.xml

      <servlet>
    <servlet-name>DisplayChart</servlet-name>
    <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
  </servlet>
   <servlet-mapping>
        <servlet-name>DisplayChart</servlet-name>
        <url-pattern>/servlet/DisplayChart</url-pattern>
    </servlet-mapping>

This is then how you display the image using jstl:

<img src="<c:url value='/servlet/DisplayChart?'><c:param name='filename' value='${yourFileNameHERE}' /></c:url>" alt=""/>
like image 91
Adam Avatar answered Sep 28 '22 02:09

Adam


Have your JSP file include an tag where the src attribute is the name of your servlet. Then, you simply have the servlet return the PNG chart:

    OutputStream out = response.getOutputStream();
    response.setContentType("image/png");
    ChartUtilities.writeChartAsPNG(out, chart, width, height);

JSP pages are really only intended to output HTML or other text data. Although you could force the JSP to output the PNG, there is no benefit to doing it that way.

It sounds like you want to create a dynamic page that updates based on a drop-down menu state change. For this, you need to use Javascript that triggers when the menu changes, and updates the value of the img tag's src attribute. Then the browser will reload the image from your servlet with a new chart.

like image 40
Kevin Panko Avatar answered Sep 28 '22 02:09

Kevin Panko