How can I display a JFreeChart in web browser with Stripes Framework

前端 未结 3 1534
[愿得一人]
[愿得一人] 2021-01-19 09:30

This is the situation: My \'metrics.jsp\' page submits a couple variables that are needed to create the chart. The \'ProjectActionBean.java\' calls down to a few other java

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-19 09:39

    You need to write a servlet which writes image (byte stream) into the output stream to the client. There are no need for creating files. Basically something like this should work:

    public class ChartServlet extends HttpServlet {
      @Override
      protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    
            JFreeChart chart = .. // create your chart
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ChartUtilities.writeChartAsPNG(bos, chart, width, height);
    
                response.setContentType("image/png");
                OutputStream out = new BufferedOutputStream(response.getOutputStream());
                out.write(bos.toByteArray());
                out.flush();
                out.close();
      }
    }
    

    Then map it to some url in your web.xml and use from "img" tag in HTML/JSP. Obviously you can pass parameters to it etc.

提交回复
热议问题