How to display line graph using JFreeChart in jsp?

前端 未结 2 1203
甜味超标
甜味超标 2020-12-20 02:41

HI All:
I am using the below to diplay the line graph. when i run the below code, i am getting the window but it is blank and not displaying the graph. Please help me

2条回答
  •  伪装坚强ぢ
    2020-12-20 03:16

    I did this some time ago as well, but I also have the code, so here's the clue..

    As Thorbjørn Ravn Andersen said you have to have a servlet generating images instead of web pages. That means that your servlet's processRequest method looks something like this:

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    
            response.setContentType("image/png");
            ServletOutputStream os = response.getOutputStream();
            ImageIO.write(getChart(request), "png", os);
            os.close();
        }
    
    private RenderedImage getChart(HttpServletRequest request) {
            String chart = request.getParameter("chart");
            // also you can process other parameters like width or height here
            if (chart.equals("myDesiredChart1")) {
                JFreeChart chart = [create your chart here];
                return chart.createBufferedImage(width, height)
            }
    

    Then you can use this servlet as a source of image in other pages for example like this..

    
    

    And you're done :)

提交回复
热议问题