将图表添加到缓冲区 [英] adding graph chart to a buffer

查看:82
本文介绍了将图表添加到缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在struts2.in中创建了web项目,我已经使用jfreechart绘制了一个图形,我在Action类中实现了它。

I have created web project in struts2.in that i have plotted a graph using jfreechart which i have implemented in my Action class.

http://www.java2s.com/Code/Java/Chart/JFreeChartTimeSeriesDemo10withperminutedata.htm

它在单独的applet窗口中显示图形我用Google搜索并找到一种方法将此图表保存为图像,以便在我的jsp文件中我可以包含此图像。
但是在我部署的最后我必须将我的项目转换为WAR文件但是如果我将项目转换为WAR我无法访问根据用户请求更改的图像(图形)。所以我想到了保存缓冲区中的图表/图像或某些东西,以便在请求新图形或用户注销后立即删除它。

which displays the graph in separate applet kind of window i googled and find a way to save this chart as image so that in my jsp file i can include this image. But at the end when i was deploying i had to convert my project into a WAR file but if i convert project to a WAR i cant access images(graph) which gets changed based on users request.So i thought of save the chart/image in a buffer or some thing so that it gets displayed later deleted as soon new graph is requested or user logs out.

那么你能不能给出一些想法至于如何做到这一点。
提前付款

So can you ppl give some idea as to how to accomplish this. Thanks in Advance

推荐答案

我写了类似于你想要做的事。我完成这个的方法是有一个第二个servlet(非常简单),它根据请求的图表接收参数,并生成图表为 PNG 。基本上,您使用所需参数调用servlet。您可以获取这些参数并构建图表。返回图表的重要部分发生在 ChartUtilities.writeChartAsPNG(out,chart,640,480)中,其中第一个参数是对调用页面的响应的输出流。第二个参数是您构建的图表。最后两个参数用于图像的大小。当你调用这个servlet时,它将位于

I have written something similar to what you are trying to do. The way I accomplished this was to have a second servlet (very simple) that takes in parameters based on the requested chart and generates the chart as a PNG. Basically, you call the servlet with the required parameters. You take those parameters and build your chart. The important part of returning the chart is happening in ChartUtilities.writeChartAsPNG(out, chart, 640, 480) where the first parameter is the output stream for the response to the calling page. The second parameter is the chart you have built. The last two parameters are used for the size of the image. When you call this servlet it would be inside of

<img src="URL_to_Servlet" />

,其中包含构建图表所需参数的URL。

with the URL containing the needed parameters to build the chart.

以下是您需要的代码,仅关注将图表作为动态构建的图像从 Servlet 返回。

Below is the code you will need, focusing solely on returning the chart as a dynamically built image from a Servlet.

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

public class ChartServlet extends HttpServlet {

    /*
     * (non-Javadoc) @see
     * javax.servlet.http.HttpServlet#doGet(
     * javax.servlet.http.HttpServletRequest,
     * javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        JFreeChart chart = this.generateLineChart();
        ServletOutputStream out = resp.getOutputStream();
        resp.setContentType("image/png");
        ChartUtilities.writeChartAsPNG(out, chart, 640, 480);
        out.close();
    }

    /**
     * Generate chart.
     *
     * @return the j free chart
     * @throws IOException Signals that an I/O exception has occurred.
     */
    private JFreeChart generateLineChart() throws IOException {

        return chart;
    }

    /*
     * (non-Javadoc) @see javax.servlet.GenericServlet#init()
     */
    @Override
    public void init() throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("Starting up charts servlet.");
    }
}

这篇关于将图表添加到缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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