Adding image/logo to a chart.

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
benoitx
Posts: 40
Joined: Fri Apr 11, 2003 5:29 pm
Location: London, yes England (where else?)
Contact:

Adding image/logo to a chart.

Post by benoitx » Tue Feb 24, 2004 7:37 pm

Hi,

Could anyone show me how to add a logo/image (gif or jpg) in a corner of a chart? I've found how to do a background but would be interested in simply adding a logo on some charts.

Many thanks

Benoit.

benoitx
Posts: 40
Joined: Fri Apr 11, 2003 5:29 pm
Location: London, yes England (where else?)
Contact:

More to the point... XYImageAnnotation?

Post by benoitx » Wed Feb 25, 2004 12:59 am

Hi,

Looking a bit further...I've come accross XYImageAnnotation. Unless I am mistaken, this class is not in the doc and no example shows how to use it. So.., I could not resist and I tried it of course.

Sorry for the blunt question but: does it work?

Having called

Code: Select all

ChartFactory.createTimeSeriesChart(....)
I tried the following:

Code: Select all

XYPlot plot = chart.getXYPlot();
XYAnnotation annotation = new XYImageAnnotation(250,250,getLogo());
plot.addAnnotation(annotation);
...
ChartUtilities.saveChartAsJPEG(file, chart, chartWidth, chartHeight);
The getLogo() returns an Image and it works as I can use it to put the image as a background.

Should I do anything different in order to use the ImageAnnotation?
Should it be called before the chart is generated? (something I am always unsure...)
Should the draw method be called on the annotation? Or is it done internally.

Any pointer/suggestion would be very welcome.

Many thanks

Benoit.

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 » Wed Feb 25, 2004 11:22 am

The code you posted should draw the image centered on the point (250, 250) on your chart (where the x and y coordinates are measured against the axes). So the thing to check is that (250, 250) falls within the ranges on your axes.
David Gilbert
JFreeChart Project Leader

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

benoitx
Posts: 40
Joined: Fri Apr 11, 2003 5:29 pm
Location: London, yes England (where else?)
Contact:

Re:Adding image/logo to a chart.

Post by benoitx » Wed Feb 25, 2004 12:59 pm

Hi David,

Many thanks for your prompt reply. It works!

It was not clear to me that the points were referring to values of the X Axis ie in my case of a TimeSeriesChart, the X value should be related to the java Date.getTime() value... which is a 'big' number for say Today Feb 25 2004. and same for the Y Axis; So (10,10) is not the absolute co-ordinates but the one where X=10 and Y=10.

Is there a way to:
1/ Use absolute co-ordinates regardless of values of the x and y axis? Say given a chart of 640 by 480, I want to see a logo centered around 320/240?
2/ put a logo outside the plot area, say in a corner of the whole chart?

many thanks

best regards,
Benoit.

richard_atkinson
Posts: 115
Joined: Fri Mar 14, 2003 3:13 pm
Location: London, England
Contact:

London, where?

Post by richard_atkinson » Wed Feb 25, 2004 6:34 pm

London, Ontario?
http://www.city.london.on.ca/Mapphoto/globe.htm

P.S. Does the X stand for xhenseval?

krheinwald
Posts: 15
Joined: Thu Mar 27, 2003 10:06 am

Re:Adding image/logo to a chart.

Post by krheinwald » Thu Feb 26, 2004 10:52 am

benoitx wrote:2/ put a logo outside the plot area, say in a corner of the whole chart?
Have a look at JFreeChart.setBackgroundImage() and JFreeChart.setBackgroundImageAlignment().

Here is an example.

HTH,
Klaus

benoitx
Posts: 40
Joined: Fri Apr 11, 2003 5:29 pm
Location: London, yes England (where else?)
Contact:

Re:Adding image/logo to a chart

Post by benoitx » Thu Feb 26, 2004 11:31 am

Hi Klaus,
Thanks for your suggestion & example. perfect!
Regards

Benoit.

Locked