在servlet中动态生成JFreeChart [英] Dynamically generate JFreeChart in servlet

查看:102
本文介绍了在servlet中动态生成JFreeChart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JFreeChart动态生成图形,这是用户选择的一些复选框的结果,但我无法弄清楚如何最好地将生成的数据集转换为图表形式(我的代码可以生成这些图表,但需要生成pngs)并进入JSP视图。目前,我只能考虑将数据集发送到JSP,但不能想到从那里做什么...我如何做到这一点:用户将表单提交给servlet,servlet生成数据集,从数据集生成的图表,pngs从图表和最后的pngs派遣到jsp?或者其他类似的东西。

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);
}

更新:

通过让doPost方法从用户帖子生成数据集,然后可以将它们存储在字段中,随后RequestDispatcher将用户转发到JSP,然后在img标记中调用servlet的doGet方法,该标记使用之前存储的数据集。用于生成png的字段,然后由JSP中的HTML显示。

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.

推荐答案

让您的JSP文件包含标记其中src属性是servlet的名称。然后,您只需让servlet返回PNG图表:

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页面实际上只用于输出HTML或其他文本数据。虽然你可以强制JSP输出PNG,但这样做没有任何好处。

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.

听起来你想要创建动态页面,根据下拉菜单状态更改进行更新。为此,您需要使用在菜单更改时触发的Javascript,并更新img标记的src属性的值。然后浏览器将使用新图表从servlet重新加载图像。

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.

这篇关于在servlet中动态生成JFreeChart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆