Displaying JFreeChart in a web page using Struts2
Asked Answered
M

3

1

I am using Struts2. I need to display JFreeChart in a web page. Can any body help me on that?

Edit: it is getting displayed in binary format.

public String execute() throws Exception {
    System.out.println("Refresh bar Chart");
    response.setContentType("image/png");
    OutputStream outstream = response.getOutputStream();
    try {
        JFreeChart chart = getChartViewer();
        ChartUtilities.writeChartAsPNG(outstream, chart, 500, 300);
        System.out.println("Created bar Chart");
        return SUCCESS;
    } finally {
        outstream.close();
        response.flushBuffer();
    }
}
Moses answered 30/3, 2010 at 13:14 Comment(1)
I've reformatted the code from your comment; revert if incorrect.Stettin
I
1

You can convert your charts to images and include them in your HTML files.

Immaterialize answered 30/3, 2010 at 13:15 Comment(1)
public String execute() throws Exception { System.out.println("Refresh bar Chart"); response.setContentType("image/png"); OutputStream outstream = response.getOutputStream(); try { JFreeChart chart = getChartViewer(); ChartUtilities.writeChartAsPNG(outstream,chart,500,300); System.out.println("Created bar Chart"); return SUCCESS; } finally { outstream.close(); response.flushBuffer(); } } it is getting displayed in binary formateMoses
K
1

JSP/Struts2/whatever-MVC pages always ends up as HTML. To display images in HTML, you need the <img> element or whatever MVC component you'd normally to use to render a HTML <img> element. To locate an image, you need to let its src attribute point to a valid URL which returns the image. This can be a static or dynamic resource, the client doesn't see the difference.

<img src="images/foo.png">

In a JSP/Servlet environment, the normal practice is to let a Servlet listen on the particular URL using url-pattern in web.xml such as /images/* and let it create/load/find the image based on request parameters or pathinfo and write the image to the outputstream of the response along a correct set of response headers.

You can find a basic example here.

Keratinize answered 30/3, 2010 at 14:23 Comment(0)
S
1

There is a plugin: http://struts.apache.org/2.x/docs/jfreechart-plugin.html

Sommers answered 30/3, 2010 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.