XYImageAnnotation with SWT

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Rudi23
Posts: 7
Joined: Tue Nov 06, 2007 10:00 am

XYImageAnnotation with SWT

Post by Rudi23 » Thu Nov 29, 2007 9:15 am

Hello,

I wanted to use an XYImageAnnotation in a Chart. I'm using JFreeChart with SWT, so ChartComposite is my Container, where I put the Chart.

The problem was, that XYImageAnnotations didn't apear in the chart, while other Annotations like TextAnnotations appeared.

So after a few hours of debugging I found out, that using SWT, the object g2 in XYImageAnnotation.draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis, int rendererIndex, PlotRenderingInfo info) is of the class org.jfree.experimental.swt.SWTGraphics2D. And in this class, some Methods are just Auto-generated method stubs and not implemented. For example
public boolean drawImage(Image image, int x, int y,
ImageObserver observer) {
// TODO Auto-generated method stub
return false;
}
Thats why XYImageAnnotations with SWT don't apear.

Could anyone who has access to the sourcecode please change this methods, so that they throw something like 'NotYetImplementedException'? This would save a lot of time for other developers with the same problem.

Thanks a lot,
Rudi

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 » Thu Nov 29, 2007 3:15 pm

Sorry for the trouble, Rudi, although that is one of the perils of using code that is marked 'experimental'. The good news is that I committed a fix to the SWTGraphics2D class a day or two back, and it now draws the images (although I should go and test the image annotation class explicitly). So when JFreeChart 1.0.9 is released (probably around mid-December), this problem will be gone.
David Gilbert
JFreeChart Project Leader

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

heprom
Posts: 91
Joined: Sat May 27, 2006 4:25 am
Location: Paris

Post by heprom » Thu Nov 29, 2007 11:27 pm

Rudi, David,

I've tried the XYImageAnnotation with the latest code and it does not work.
Actually it's almost working, I've traced down the bug to be a bad x, y location generated in the draw call:

Code: Select all

 xx = xx - (float) anchorPoint.getX();
 yy = yy - (float) anchorPoint.getY();
 System.out.println("drawing at x="+xx+" and y="+yy);
 boolean b = g2.drawImage(this.image, (int) xx, (int) yy, null);
 
will generate

Code: Select all

 drawing at x=-11060.547 and y=384.78647
Hardcoding an appropriate (x,y) location draws the image successfully. It should be easy to fix but it's late and I do not want to break anything in the awt code. Will look at this more during the week end.

Regards
/heprom

heprom
Posts: 91
Joined: Sat May 27, 2006 4:25 am
Location: Paris

Post by heprom » Thu Nov 29, 2007 11:51 pm

ok here's how to fix it:
we need relative XY coordinates like with the XYTitleAnnotation class

Code: Select all

        Range xRange = domainAxis.getRange();
        Range yRange = rangeAxis.getRange();
        double anchorX = xRange.getLowerBound() + (this.x * xRange.getLength());
        double anchorY = yRange.getLowerBound() + (this.y * yRange.getLength());
//        double anchorX = domainAxis.valueToJava2D(this.x, dataArea, domainEdge);
//        double anchorY = rangeAxis.valueToJava2D(this.y, dataArea, rangeEdge);
David, I can patch this. Do we want to add a XYCoordinateType field to the class like in XYTitleAnnotation?

/heprom

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 Nov 30, 2007 10:44 am

heprom wrote:David, I can patch this. Do we want to add a XYCoordinateType field to the class like in XYTitleAnnotation?
Yes, I think that would add to the flexibility of XYImageAnnotation. Right now, you can only specify the image location using the coordinates of the axis. Being (optionally) able to place it at a relative location within the plot area would be useful if you want to show a logo at, say, the bottom right of the chart. And linking to a dataset series and item index would allow the image to "follow" a particular data point.

Having said all that, I just tested XYImageAnnotation with the latest SWTGraphics2D class and it seems to be working. What was the failure you saw?
David Gilbert
JFreeChart Project Leader

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

heprom
Posts: 91
Joined: Sat May 27, 2006 4:25 am
Location: Paris

Post by heprom » Fri Nov 30, 2007 11:52 am

Hmm, interesting, The failure I described in the above post was not due to SWT but in the XYImageAnnotation code. Apparently you do not seem to see it. With the code from svn head, the image (the anchor is set to something usual) is drawn at

Code: Select all

drawing at x=-11060.547 and y=384.78647
and thus do not appear on the chart. If you hardcode say x=100, y=100, the image appears. I'll post some demo code tonight.

heprom
Posts: 91
Joined: Sat May 27, 2006 4:25 am
Location: Paris

Post by heprom » Fri Nov 30, 2007 10:55 pm

David, I've sent you a patch to address the issue.

Locked