Problem with writeChartAsJPEG and ChartRenderingInfo?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Noel Rappin

Problem with writeChartAsJPEG and ChartRenderingInfo?

Post by Noel Rappin » Mon Dec 30, 2002 4:16 pm

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

bharat

Re: Problem with writeChartAsJPEG and ChartRenderingInfo?

Post by bharat » Mon Jan 06, 2003 9:30 pm

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

Bryan Sampieri

Re: Problem with writeChartAsJPEG and ChartRenderingInfo?

Post by Bryan Sampieri » Mon Jan 06, 2003 11:41 pm

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;

David Gilbert

Re: Problem with writeChartAsJPEG and ChartRenderingInfo?

Post by David Gilbert » Tue Jan 07, 2003 11:37 am

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

Noel Rappin

Re: Problem with writeChartAsJPEG and ChartRenderingInfo?

Post by Noel Rappin » Thu Jan 09, 2003 5:04 pm

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

Jeff Chaney

Re: Problem with writeChartAsJPEG and ChartRenderingInfo?

Post by Jeff Chaney » Wed Jan 22, 2003 2:17 pm

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());

Artemis

Re: Problem with writeChartAsJPEG and ChartRenderingInfo?

Post by Artemis » Mon Feb 10, 2003 7:47 am

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

Locked