ChartEntity problem cast to XYItemEntity jfreechart-1.0.13

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
pdvmipv
Posts: 13
Joined: Tue Mar 18, 2008 11:11 am

ChartEntity problem cast to XYItemEntity jfreechart-1.0.13

Post by pdvmipv » Fri May 08, 2009 12:38 pm

Hi

i hava problem to make cast from ChartEntity to XYItemEntity with the last release of
Jfreecharrt (1.0.13).

Following the code working fine using Jfreecharrt 1.0.12

Code: Select all

        XYItemEntity entity = (XYItemEntity) viewerPlot.getChartPanel().getEntityForPoint( 
                event.getTrigger().getX(), event.getTrigger().getY());
With the new release i have got this error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: org.jfree.chart.entity.PlotEntity cannot be cast to org.jfree.chart.entity.XYItemEntity

Can you help me to resolve this issue?

Thanks

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Re: ChartEntity problem cast to XYItemEntity jfreechart-1.0.13

Post by MitchBuell » Fri May 08, 2009 7:47 pm

There are multiple types of ChartEntities. JFreeChart 1.0.13 added some more too. You should at least make sure the ChartEntity that you get is an instance of an XYItemEntity.

Code: Select all

ChartEntity entity = viewerPlot.getChartPanel().getEntityForPoint(event.getTrigger().getX(), event.getTrigger().getY());
if (entity instanceof XYItemEntity)
{
     XYItemEntity xyEntity = (XYItemEntity) entity;
}

pdvmipv
Posts: 13
Joined: Tue Mar 18, 2008 11:11 am

Re: ChartEntity problem cast to XYItemEntity jfreechart-1.0.13

Post by pdvmipv » Mon May 11, 2009 8:31 am

ok but,
with the old version the code:

Code: Select all

viewerPlot.getChartPanel().getEntityForPoint(event.getTrigger().getX(), event.getTrigger().getY());
return a XYItemEntity, in the new version the type is PlotEntity.

What can i do?

I need to use the function entity.getDataset(), and i can only have with XYItemEntity.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: ChartEntity problem cast to XYItemEntity jfreechart-1.0.13

Post by paradoxoff » Mon May 11, 2009 12:23 pm

As MitchBuell mentioned, the PlotEntity is a new entity class in 1.0.13. This entity contains the entire data area as hot spot. If you click anywhere in the data area, you will now get a PlotEntity where you got null before. However, if an XYItemEntity is located at the point of the mouse click, you should still get the XYItemEntity since an XYItemEntity will be placed on top of the PlotEntity.
Is it possible that you have simply missed the XYItemEntity by a few pixels? Prior to 1.0.13, you would get a null entity. I assume that you handle that case, though your snippets do not indicate that. In 1.0.13, you will get a non-null entity if you click anywhere on the data area.

pdvmipv
Posts: 13
Joined: Tue Mar 18, 2008 11:11 am

Re: ChartEntity problem cast to XYItemEntity jfreechart-1.0.13

Post by pdvmipv » Mon May 11, 2009 12:53 pm

Ok resolved as coded below.
Thanks :)

Code: Select all

//        1.0.12 Version
//        XYItemEntity entity = (XYItemEntity) viewerPlot.getChartPanel().getEntityForPoint(
//                event.getTrigger().getX(), event.getTrigger().getY());
//       Integer indexOfAxis = 0;
//        if (entity != null) {
//                         indexOfAxis = viewerPlot.getIndexDataset((XYSeriesCollection) entity.getDataset());
//        }

//    1.0.13 Version
        ChartEntity entity = viewerPlot.getChartPanel().getEntityForPoint(
                event.getTrigger().getX(), event.getTrigger().getY());

        XYItemEntity xyItemEntity = null;

        Integer indexOfAxis = 0;
        if (entity != null) {
            if (entity instanceof XYItemEntity) {
                xyItemEntity = (XYItemEntity) entity;

                indexOfAxis = viewerPlot.getIndexDataset((XYSeriesCollection) xyItemEntity.getDataset());
            }
        }
]

Locked