views:

1614

answers:

2

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.

+2  A: 

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.

Kevin Panko
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?
Robert
This servlet only does one thing -- creates the PNG image of a chart. You will still need to have a separate JSP page to hold the <img> tag. There is no RequestDispatcher needed for this.
Kevin Panko
let me phrase it differently - I cannot display the pngs without the datasets, which has to be generated by the user submitted form. I could make a servlet that only handles one thing, producing a PNG, but how would I give it the dataset to be able to do that, since it is generated by the form submitted?
Robert
Kevin Panko
I've solved it, see original post
Robert
+2  A: 

JFreeChart already has an implementation for Kevin Panko's answer in the ServletUtility class. See my answer to a similar question for more info.

Adam