Handling output streams-Represent chart but NOT in an image

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
andrio
Posts: 5
Joined: Tue Mar 30, 2004 8:46 am
Location: Thessaloniki-Greece

Handling output streams-Represent chart but NOT in an image

Post by andrio » Tue Mar 30, 2004 12:28 pm

Hi there,

I've got a server collecting/creating the datasets and the charts and a JSP client to present my results. I would like to know if there is a way to send the result chart to the outpustream and collect it in the JSP page, but i don't want to write it down on a file. Is there a way to represent the chart on the page but not heaving it in an image file? I don't want an image file on the client side and if it is possible not even have an image file on the server.

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Tue Mar 30, 2004 2:14 pm

You can use a servlet to generate an image on the fly

response.setContentType("image/png");
response.getOutputStream().write(img, 0, img.length);

jk2004
Posts: 2
Joined: Thu Mar 04, 2004 4:43 pm
Location: Cincinnati, OH
Contact:

Post by jk2004 » Tue Mar 30, 2004 5:43 pm

The way I do in my application is as follows:

JSP
----
<img src="/servlet/GraphServlet">

GraphServlet
----------------
ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 0.75f, chart, 600, 300, null/*chartRenderingInfo*/);

It works very good. Thanks to JFree.org

I have multiple grpahs displayed in my JSP with multiple <img> tag.
Thanks,
Jey Kumar.
Happy J2EEing :-)

andrio
Posts: 5
Joined: Tue Mar 30, 2004 8:46 am
Location: Thessaloniki-Greece

Post by andrio » Wed Mar 31, 2004 9:55 am

Thanks for your replies out there but still i've got this exception :
java.lang.IllegalStateException: getOutputStream() has already been called for this response.

I'm not calling getOutputStream() twice and just calling a function directly from the JSP that includes the code
ChartUtilities.writeChartAsPNG(response.getOutputStream(),chart, 500, 300);

The strange part is that i've got an image in my jsp just for the first chart and afterwards the page stops loading and throws the exception stated before. Does anyone know what is wrong?

hkollmann
Posts: 2
Joined: Tue Mar 30, 2004 3:38 pm

Post by hkollmann » Wed Mar 31, 2004 3:50 pm

Have you closed the output stream?

I use this code:
try {
response.setContentType("image/png");
OutputStream out = response.getOutputStream();
ChartUtilities.writeChartAsPNG(out, chart, data.getWidth(), data.getHeight());
}finally {
out.close();
}

cheers,
Henner

jk2004
Posts: 2
Joined: Thu Mar 04, 2004 4:43 pm
Location: Cincinnati, OH
Contact:

IllegalStateException

Post by jk2004 » Wed Mar 31, 2004 7:29 pm

IllegalStateException usually occurs when you have redirected your response to a different page and call response.getOutputStream() from the original servlet.
Thanks,
Jey Kumar.
Happy J2EEing :-)

andrio
Posts: 5
Joined: Tue Mar 30, 2004 8:46 am
Location: Thessaloniki-Greece

Post by andrio » Thu Apr 01, 2004 9:31 am

hkollmann are your sure for the code you posted? The out variable should be declared outside the try clause orelse you cannot use it in the finally clause because it cannot see it. I will post my servlet and JSP code to see what i've got.
JSP
String filename = TheFuture.Pie(session, request, response);
String graphURL = request.getContextPath() +"/"+ filename;
and afterwards i'm using the graphURL variable to present my image.
Servlet

try {
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

response.setContentType("image/png");
OutputStream out = response.getOutputStream();
ChartUtilities.writeChartAsPNG(out, chart, 500, 300);

} finally {
out.close();
}

return filename;
}
my class also extends HttpServlet.

Any help would be appricated guys cuase this think already have gone too bad for my work.
Cheers

:evil: PS.Still got the "getOutputStream() has already been called" error

richard_atkinson
Posts: 115
Joined: Fri Mar 14, 2003 3:13 pm
Location: London, England
Contact:

Post by richard_atkinson » Thu Apr 01, 2004 8:51 pm

Check out the source for the DisplayChart servlet to see an example of a chart being streamed to the browser - http://cvs.sourceforge.net/viewcvs.py/j ... &view=auto.

Regards,
Richard...

TheHaas
Posts: 21
Joined: Sat Feb 07, 2004 9:14 pm
Contact:

Post by TheHaas » Thu Apr 01, 2004 11:02 pm

I decided to do this a route since my clustering question was answered.

hkollmann's code worked great!

My only problem is that I want an image map. Presently, I have my img tag as:

Code: Select all

<img src="/wpConsole/ChartServlet" usemap="/wpConsole/ChartServlet" border='0' align='center'>
It brings up the image, but the image map doesn't seem to be working. Any ideas?

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Apr 02, 2004 5:51 am

Have you written the image map to the HTML output?
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

TheHaas
Posts: 21
Joined: Sat Feb 07, 2004 9:14 pm
Contact:

Post by TheHaas » Fri Apr 02, 2004 9:26 pm

I've been trying to write the HTML out, but have had no luck.

When I try, I get an exception when trying to do a ChartUtilities.getImageMap.

Here is info from my JSP:

Code: Select all

<img src="/wpConsole/ChartServlet" usemap="#chart" border='0' align='center'>
<%=ChartServlet.getImageMap(sConsoleCookie)%>
Methods from my ChartServlet:

Code: Select all

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
					
		Cookie[] cookies = request.getCookies();
		String cookieVal = null;
		for (int i= 0; i<cookies.length; i++) {
			 Cookie cookie = cookies[i];
			 if (WpConsole.WPCONSOLE_COOKIE_NAME.equals(cookie.getName()) ){
				 cookieVal = cookie.getValue();
				 break;
			 }
		 }
		response.setContentType("image/png"); 
		OutputStream out = response.getOutputStream();
		JFreeChart chart = (JFreeChart) chartTable.get(cookieVal);
		ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
		
		try {
			ChartUtilities.writeChartAsPNG(out,chart,600,400,info);
		} catch (IOException e) {
			
			logger.error("Failed creating chart image. Error was " + e);
		}			
		
		out.close();
		
		
	}
	public static String getImageMap(String cookieVal) {
		JFreeChart chart = (JFreeChart) chartTable.get(cookieVal);
		Plot rawplot  = chart.getPlot();
		
		ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
		if (info != null ){
		   return ChartUtilities.getImageMap("chart",info);
		}else{
			logger.error("Cannot get image map!");
			return "";
		}
}
		

Locked