setBackgroundImage() not giving good results...

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
daress
Posts: 23
Joined: Tue Mar 13, 2007 4:52 pm
Contact:

setBackgroundImage() not giving good results...

Post by daress » Fri Apr 18, 2008 2:35 pm

Hmm...

I will be the first to admit I am slow to upgrade JARs in my project. I have been using jfreechart-1.0.0-rc1.jar and jcommon-1.0.0-rc1.jar for about a year successfully generating scatterplots with background images and then exporting as JPEGs.

The code is simple and uses a background image that is in PNG format:

Code: Select all

	XYSeriesCollection dataset = fetchFwdData(); // my function
	JFreeChart chart = null;
		
	try {
		chart = ChartFactory.createScatterPlot(
					null,			// chart title
					null,			// domain axis label
					null,			// range axis label
					dataset,            // data
					PlotOrientation.VERTICAL,	// orientation
					true,                 // include legend
					false,                // tooltips
					false);              // urls
			
		if( mFwdImage != null ) { // mFwdImage is a java.awt.Image loaded from a PNG file.
			chart.getXYPlot().setBackgroundImage(mFwdImage);
		}
	} catch (java.lang.Exception e ) {
		e.printStackTrace();
	} 
		
       try {
            org.jfree.chart.ChartUtilities.writeChartAsJPEG(baos, 1.0f, chart, mWidth, mHeight);
       } catch (java.io.IOException e) {
            System.out.println(e.toString());
       }

	java.awt.Image fwdJpeg = new NSData(embedComment( baos.toByteArray(), baos.size(), null ));  
       // embedcomment is my code to insert a comment about 
       // the data in the chart
With jfreechart-1.0.0-rc1.jar and jcommon-1.0.0-rc1.jar I get beautiful images with the above code.

With jfreechart-1.0.4.jar and jcommon-1.0.8.jar, the background image looses details as if a mask is placed on the background image.

With jfreechart-1.0.9.jar and jcommon-1.0.12.jar, the images are solid black, ie, no features visible, no series visible.

I have attempted changing the alpha value for the background, but that is not helping. Any suggestions as to why simply changing the JARs would cause the image to degrade as described?

For the time being I can stay with the older jars, but it bothers me to not be able to go forward with the improvements. Jfreechart has been a great tool thus far, and I am sure I am missing something, but I do not know what it is. Any and all help appreciated.

Thanks,
David

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 18, 2008 3:05 pm

The problem with JFreeChart 1.0.9 is likely to be a bug that affects the output of JPEG files. It is fixed for the upcoming 1.0.10 release. I suggest you try writing your charts as PNG files, or downgrade to 1.0.8, or wait for the 1.0.10 release (already overdue).

Regarding the difference with the 1.0.4 version, this is likely to be caused by the backgroundImageAlpha attribute in the Plot class. Try modifying this to see what effect it has.
David Gilbert
JFreeChart Project Leader

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

daress
Posts: 23
Joined: Tue Mar 13, 2007 4:52 pm
Contact:

Post by daress » Fri Apr 18, 2008 4:00 pm

david.gilbert wrote:The problem with JFreeChart 1.0.9 is likely to be a bug that affects the output of JPEG files. It is fixed for the upcoming 1.0.10 release. I suggest you try writing your charts as PNG files, or downgrade to 1.0.8, or wait for the 1.0.10 release (already overdue).
Ahh... switching to PNG works in 1.0.9 with a PNG background. I tried this with 1.0.4 (or maybe it was 1.0.0-rc1) and it produced a distorted image. But, with writeChartAsPNG() in 1.0.9, the images are no longer black rectangles, which is good.
david.gilbert wrote:Regarding the difference with the 1.0.4 version, this is likely to be caused by the backgroundImageAlpha attribute in the Plot class. Try modifying this to see what effect it has.
Hmm, setBackgroundImageAlpha is a new method to me. I tried varying setBackgroundAlpha() but that did not help. But, with backgroundImageAlpha (which defaults to 0.5) set to 1.0, the charts are looking wonderful again.

Thank you for a timely response and a great product.

David

Locked