Handling output streams-Represent chart but NOT in an image
Handling output streams-Represent chart but NOT in an image
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.
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.
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.
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
Jey Kumar.
Happy J2EEing

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?
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?
IllegalStateException
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
Jey Kumar.
Happy J2EEing

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
PS.Still got the "getOutputStream() has already been called" error
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

-
- Posts: 115
- Joined: Fri Mar 14, 2003 3:13 pm
- Location: London, England
- Contact:
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...
Regards,
Richard...
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:
It brings up the image, but the image map doesn't seem to be working. Any ideas?
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'>
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Have you written the image map to the HTML output?
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


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:
Methods from my ChartServlet:
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)%>
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 "";
}
}