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

前端 未结 3 1536
[愿得一人]
[愿得一人] 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:48

    If you want to stay within the Stripes framework you can use a custom extension of the StreamingResolution, thusly:

    Create a new normal ActionBean implementation that will represent the URL of your chart (to be included in your img tag):

    @DefaultHandler
    public Resolution view() {
        JFreeChart chart = ...
        return new ChartStreamingResolution(chart);
    }
    

    The custom StreamingResolution then looks like this:

    public class ChartStreamingResolution extends StreamingResolution {
        private JFreeChart chart;
        public ChartStreamingResolution(JFreeChart chart) {
            super("image/png");
            this.chart = chart;
        }
    
        @Override
        public void stream(HttpServletResponse response) {
            try {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                ChartUtilities.writeChartAsPNG(bos, chart, 400, 200);
                OutputStream out = new BufferedOutputStream(response.getOutputStream());
                out.write(bos.toByteArray());
                out.flush();
                out.close();
            } catch (Exception e) {
                //something sensible
            }
        }
    }
    

提交回复
热议问题