When creating an image the title does not appear

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

When creating an image the title does not appear

Post by Bob Raker » Sat Nov 04, 2000 7:28 pm

I am trying to create an image in JPEG file that I can then place on a web page with a servlet. Right now I am trying to get the image created and am doing pretty well except I can not get the title or legend to show. I have stolen pieces from the demo just to figure out how it works. Below is my code, this function is called by main. The panel that is shown on the screen does include the title, but the JPEG doesn't. Anyone have any ideas.

public MakeChart()
{
super();

// stuff I stole
XYDataSource xyData1 = createTestXYDataSource();
JFreeChart chart = JFreeChart.createTimeSeriesChart(xyData1);
StandardTitle title = (StandardTitle)chart.getTitle();
title.setTitle("Value of GBP");
//chart.setChartBackgroundPaint(new GradientPaint(0, 0, Color.white,0, 1000, Color.blue));
Plot myPlot = chart.getPlot();
Axis myVerticalAxis = myPlot.getAxis(Plot.VERTICAL_AXIS);
myVerticalAxis.setLabel("USD per GBP");


// My stuff
BufferedImage image = new BufferedImage( 300, 200, BufferedImage.TYPE_INT_RGB );
Graphics2D g = image.createGraphics();
g.setColor( Color.white );
g.fillRect(0, 0, 300, 200 );
g.setColor( Color.orange );
g.drawRect(0, 0, 290, 190 );
//g.setBackground( Color.lightGray );
/*g.setColor( Color.blue );
g.fillRect(100, 50, 400, 400 );
g.setColor( Color.red );
g.drawOval( 150, 100, 300, 100 );*/

Rectangle drawArea = new Rectangle( 300, 200 );
myPlot.draw( g, drawArea );

try {
FileOutputStream fos = new FileOutputStream( "c:\java\test.jpeg" );
BufferedOutputStream bos = new BufferedOutputStream( fos );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( bos ) ;
encoder.encode( image );
bos.close();
} catch( FileNotFoundException e ) {
System.out.println( "file not found " + e.getMessage() );
System.exit(1);
} catch( IOException e ) {
System.out.println( "IOException " + e.getMessage() );
System.exit(1);
}

System.out.println( "Done" );
// more of their stuff
JFreeChartPanel chartPanel5 = new JFreeChartPanel(chart);
chartPanel5.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(4, 4, 4, 4),
BorderFactory.createLineBorder(Color.darkGray, 1)));



getContentPane().add(chartPanel5);
}

David Gilbert

RE: When creating an image the title does not appe

Post by David Gilbert » Sun Nov 05, 2000 9:47 am

Hi Bob,

Your code is calling the draw() method in the XYPlot class. But if you look in the source code for JFreeChartPanel you will see that it calls the draw() method in the JFreeChart class.

The latter method takes the available drawing area, draws the title and the legend, then works out the remaining area and passes that to the draw() method of whatever Plot subclass is being used.

Just call chart.draw() instead of myPlot.draw() and I think your code should work.

Regards,

DG.

Locked