当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《如何在jsp中使用JFreeChart显示折线图?》经验,为你挑选了1个好方法。

全部:
我使用下面的图表来显示线图.当我运行以下代码时,我正在获取窗口,但它是空白的,不显示图形.请帮助我,并告诉我如何使用下面的代码在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);
    }
}

Martin Lazar.. 5

我之前也做过这个,但我也有代码,所以这里有线索..

正如ThorbjørnRavnAndersen所说,你必须有一个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)
        }

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

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

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



    
推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有