Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
316 views
Welcome To Ask or Share your Answers For Others

1 Answer

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=""/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...