Java 如何在jsp中使用JFreeChart显示折线图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/665222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:37:55  来源:igfitidea点击:

How to display line graph using JFreeChart in jsp?

javajfreechartlinegraph

提问by

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 and also tell me how to diplay the line graph in html page using below code.

大家好:
我正在使用以下内容来显示折线图。当我运行下面的代码时,我得到了窗口,但它是空白的,没有显示图形。请帮助我,并告诉我如何使用以下代码在 html 页面中显示折线图。

import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.*;

public class xyLine {

    public static void main(String arg[]) {
        XYSeries series = new XYSeries("Average Weight");
        series.add(20.0, 20.0);
        series.add(40.0, 25.0);
        series.add(55.0, 50.0);
        series.add(70.0, 65.0);
        XYDataset xyDataset = new XYSeriesCollection(series);
        JFreeChart chart = ChartFactory.createXYLineChart(
            "XYLine Chart using JFreeChart", "Age", "Weight",
            xyDataset, PlotOrientation.VERTICAL, true, true, false);
        ChartFrame frame1 = new ChartFrame("XYLine Chart", chart);
        frame1.setVisible(true);
        frame1.setSize(300, 300);
    }
}

回答by Thorbj?rn Ravn Andersen

You are using a swing approach which does not work in a web setting. You must generate an Image, and flatten it to e.g. a JPEG byte stream, and return THAT as a response from your servlet with a correct MIME type.

您正在使用一种在网络环境中不起作用的摆动方法。您必须生成一个图像,并将其展平为例如 JPEG 字节流,然后将 THAT 作为来自具有正确 MIME 类型的 servlet 的响应返回。

I did this many moons ago but do not have the code anymore.

我很多个月前做了这个,但不再有代码了。

回答by Martin Lazar

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:

正如 Thorbj?rn Ravn Andersen 所说,您必须有一个 servlet 来生成图像而不是网页。这意味着您的 servlet 的 processRequest 方法如下所示:

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..

然后你可以使用这个 servlet 作为其他页面中的图像源,例如这样..

<img src="/ChartDrawerServlet?chart=myDesiredChart1&width=..and other processed parameters" ..>

And you're done :)

你完成了:)