The point is that you need 2 HTTP requests to get the picture AND the imagemap because of the way a browser parses HTML.
1 for the html => from the JSP
1 for the picture => from servlet
The picture has to be send by a servlet because a JSP CANNOT send anything else than text.
So, in your JSP you :
- produce the graph, and store it in the session or in a singleton in your server or anywhere you want, in as much as you can get it in anthoer context.
- produce the image map from that graph
- call the image from the servlet
in your servlet you :
- get the graph from the session or whereever it is stored and send it.
I really prefer store the JFreeChart instance rather than a BufferedImage, because with it, you can generate what you want when you need it :
- you can send the size as parameter to your servelt and generate a picture the size you need
- you can generated PNG, JPG, PDF, etc ... from it
JSP :
Code: Select all
-- in the java code part :
// produce chart ....
// store it the way you want : here in session
<%@ page session="true" %>
session.setAttribute( "chart", chart );
// get ImageMap
ChartRenderingInfo info = new ChartRenderingInfo();
// populate the info
chart.createBufferedImage(640, 400, info);
String imageMap = ChartUtilities.getImageMap( "map", info );
-- in the HTML part
// include the map
<%= imageMap%>
// include the call for the image
<IMG src="chartviewer" usemap="#map">
Code: Select all
public class ChartViewer extends HttpServlet
{
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
// get the chart from storage
JFreeChart chart = (JFreeChart) session.getAttribute( "chart" );
// set the content type so the browser can see this as it is
response.setContentType( "image/png" );
// send the picture
BufferedImage buf = chart.createBufferedImage(640, 400, null);
PngEncoder encoder = new PngEncoder( buf, false, 0, 9 );
response.getOutputStream().write( encoder.pngEncode() );
}
}
Code: Select all
public class ChartViewer extends HttpServlet
{
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
// get the chart from storage
JFreeChart chart = (JFreeChart) session.getAttribute( "chart" );
// set the content type so the browser can see this as it is
response.setContentType( "image/jpeg" );
// send the picture
BufferedImage buf = chart.createBufferedImage(640, 400, null);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( response.getOutputStream() );
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam( buf );
param.setQuality( 0.75f, true );
encoder.encode( buf, param );
}
}
for PDF, it a bit different :
Code: Select all
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
// get the chart from storage
JFreeChart chart = (JFreeChart) session.getAttribute( "chart" );
// set the content type so the browser can see this as it is
response.setContentType( "application/pdf" );
int width = 640;
int height = 480;
Rectangle pagesize = new Rectangle( width, height );
Document document = new Document( pagesize, 50, 50, 50, 50 );
OutputStream os = new BufferedOutputStream( out );
PdfWriter writer = PdfWriter.getInstance( document, out );
// document.addAuthor("JFreeChart");
// document.addSubject("Demonstration");
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate( width, height );
Graphics2D g2 = tp.createGraphics( width, height, new DefaultFontMapper() );
Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height );
chart.draw(g2, r2D);
g2.dispose();
cb.addTemplate(tp, 0, 0);
document.close();
}