This was kind of strange. I have a servlet that sends out a chart using the ChartUtilities.writeChartAsJPEG method. It works great.
However, then, I added a ChartRenderingInfo object to the call, looking like this:
JFreeChart chartToDraw = graph.generateChart();
response.setContentType("image/jpeg");
OutputStream out = response.getOutputStream();
ChartRenderingInfo cri = new ChartRenderingInfo();
ChartUtilities.writeChartAsJPEG(out, chartToDraw, graph.getWidth(),
graph.getHeight(), cri);
And now I get the error message in my browser "The image <...> cannot be displayed, because it contains errors" (Mozilla -- in IE, it just shows up as a broken image link).
If I use saveChartAsJPEG to a file, then the graph draws fine -- do I need to switch to that, or is there some way to get this to work with the write method?
JFreeChart 0.94, Java 1.4.1, Servlet engine is JRun 3.1.
Thanks,
Noel Rappin
Problem with writeChartAsJPEG and ChartRenderingInfo?
Re: Problem with writeChartAsJPEG and ChartRenderingInfo?
hai,
you have to save the jpeg and get jpeg from that location into the browser.
use fileoutputstream and use output stream in writeasjpeg method
use this code if it is useful to u
FileOutputStream fileout = new FileOutputStream("c:/Test.jpeg");
ChartUtilities.writeChartAsJPEG(fileout, chart, 500, 300);
bye
Bharat
you have to save the jpeg and get jpeg from that location into the browser.
use fileoutputstream and use output stream in writeasjpeg method
use this code if it is useful to u
FileOutputStream fileout = new FileOutputStream("c:/Test.jpeg");
ChartUtilities.writeChartAsJPEG(fileout, chart, 500, 300);
bye
Bharat
Re: Problem with writeChartAsJPEG and ChartRenderingInfo?
It probably just needs a content length header. Try this instead.... (change PNG to JPEG, if needed)
// create a 50 KB (should be enough) buffer to write to and write to it...
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream(1024*50);
ChartUtilities.writeChartAsPNG(outBuffer, chart, width, height);
// get buffer's data
byte[] buf = outBuffer.toByteArray();
// set response information
response.setContentType("image/png");
response.setContentLength(buf.length);
// write the chart data
OutputStream out = response.getOutputStream();
out.write(buf, 0, buf.length);
// clean up
outBuffer.close();
outBuffer = null;
buf = null;
// create a 50 KB (should be enough) buffer to write to and write to it...
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream(1024*50);
ChartUtilities.writeChartAsPNG(outBuffer, chart, width, height);
// get buffer's data
byte[] buf = outBuffer.toByteArray();
// set response information
response.setContentType("image/png");
response.setContentLength(buf.length);
// write the chart data
OutputStream out = response.getOutputStream();
out.write(buf, 0, buf.length);
// clean up
outBuffer.close();
outBuffer = null;
buf = null;
Re: Problem with writeChartAsJPEG and ChartRenderingInfo?
Hi Noel,
The ChartRenderingInfo object just collects information, so it shouldn't be messing up the image...but perhaps there is a bug causing some trouble.
One thing to check is that you are closing the output stream, I didn't see that in your code above. And something to try is writeChartAsPNG(...) to see if it is a JPEG specific problem (unlikely though, I guess).
Regards,
Dave Gilbert
The ChartRenderingInfo object just collects information, so it shouldn't be messing up the image...but perhaps there is a bug causing some trouble.
One thing to check is that you are closing the output stream, I didn't see that in your code above. And something to try is writeChartAsPNG(...) to see if it is a JPEG specific problem (unlikely though, I guess).
Regards,
Dave Gilbert
Re: Problem with writeChartAsJPEG and ChartRenderingInfo?
Weirdly, it works fine if I change to PNG (closing the stream had no effect). I haven't tried the file or byt streams yet.
Thanks,
Noel
Thanks,
Noel
Re: Problem with writeChartAsJPEG and ChartRenderingInfo?
I just ran into this same problem and was searching the forum for a possible solution.
I was using 0.9.3 without a problem. When I upgraded to 0.9.4 to get my Overlaid charts working the ChartUtilities.writeChartAsJPEG() stopped working. Like Noel I was able to switch to ChartUtilities.writeChartAsPNG().
I am actually writing the chart to a byte array and the array ends up having a length of zero.
Writing the JPEG to a file is not a solution for me.
It's not a show stopper because PNG is fine for what I am doing but it is weird.
Here's the portion of my code that writes the image.
ChartRenderingInfo renderingInfo = new ChartRenderingInfo(new StandardEntityCollection());
ByteArrayOutputStream image = new ByteArrayOutputStream();
// ChartUtilities.writeChartAsJPEG(image,chart, chartWidth, chartHeight, renderingInfo );
ChartUtilities.writeChartAsPNG(image, chart, chartWidth, chartHeight, renderingInfo );
System.out.println("Setting Image on ChartInfo Size: " + image.size());
chartInfo.setImage(image.toByteArray());
I was using 0.9.3 without a problem. When I upgraded to 0.9.4 to get my Overlaid charts working the ChartUtilities.writeChartAsJPEG() stopped working. Like Noel I was able to switch to ChartUtilities.writeChartAsPNG().
I am actually writing the chart to a byte array and the array ends up having a length of zero.
Writing the JPEG to a file is not a solution for me.
It's not a show stopper because PNG is fine for what I am doing but it is weird.
Here's the portion of my code that writes the image.
ChartRenderingInfo renderingInfo = new ChartRenderingInfo(new StandardEntityCollection());
ByteArrayOutputStream image = new ByteArrayOutputStream();
// ChartUtilities.writeChartAsJPEG(image,chart, chartWidth, chartHeight, renderingInfo );
ChartUtilities.writeChartAsPNG(image, chart, chartWidth, chartHeight, renderingInfo );
System.out.println("Setting Image on ChartInfo Size: " + image.size());
chartInfo.setImage(image.toByteArray());
Re: Problem with writeChartAsJPEG and ChartRenderingInfo?
I similarly need the code above to develop a servlet-applet application,
so how can I read back the chart object in the applet ?
Thanks in advance
so how can I read back the chart object in the applet ?
Thanks in advance