ChartEntity nearest to mouse click

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
esword
Posts: 10
Joined: Tue Apr 05, 2005 7:53 pm
Location: Virginia

ChartEntity nearest to mouse click

Post by esword » Tue Apr 05, 2005 8:01 pm

I have seen many posts for people asking for ways to get information from a mouse click when there isn't a ChartEntity directly under the click (like for a line on an XY line chart, for example). I had a similar need and came up with this mouse handler this morning. It finds the closest ChartEntity to the point clicked (based on the center point of the bounding rect of the entity).

Hope it is useful to some people:

Code: Select all

    private class ChartMouseObserver implements ChartMouseListener
    {
        public void chartMouseClicked(ChartMouseEvent event)
        {
            ChartEntity entity = event.getEntity();
            
            if (entity == null)
            {
                //the mouse click wasn't directly on an entity, so locate the nearest one
                int x = (int) ((event.getTrigger().getX() - chartPanel.getInsets().left) / chartPanel.getScaleX());
                int y = (int) ((event.getTrigger().getY() - chartPanel.getInsets().top) / chartPanel.getScaleY());
                Point2D point2d = new Point2D.Double(x, y);
                double minDistance = Integer.MAX_VALUE;
                                
                Collection entities = chartPanel.getChartRenderingInfo().getEntityCollection().getEntities();
                for (Iterator iter = entities.iterator(); iter.hasNext();)
                {
                    ChartEntity element = (ChartEntity) iter.next();
                    
                    if (isDataEntity(element))
                    {
                        Rectangle rect = element.getArea().getBounds();
                        Point2D centerPoint = new Point2D.Double(rect.getCenterX(), rect.getCenterY());
                        
                        if (point2d.distance(centerPoint) < minDistance)
                        {
                            minDistance = point2d.distance(centerPoint);
                            entity = element;
                        }                        
                    }
                }                
            }

            if (entity != null)
            {
                 //TODO - put your logic here
            }

        }
        
        protected boolean isDataEntity(ChartEntity entity)
        {
            return ((entity instanceof XYItemEntity) || (entity instanceof CategoryItemEntity) ||
                    (entity instanceof ContourEntity) || (entity instanceof PieSectionEntity));
        }

        public void chartMouseMoved(ChartMouseEvent event)
        {
        }
        
    }

Guest

Post by Guest » Fri May 20, 2005 9:51 pm

Thanks!

This helped a lot. Aprpeciate it.

deepika
Posts: 13
Joined: Fri May 13, 2005 2:00 pm

Post by deepika » Tue Oct 24, 2006 2:44 pm

Hi esword, This is great. thanks a lot.

But I want things to work slightly different. The below example works great when you move the mouse horizontally. But when I move the mouse vertically say, I move the mouse in 2nd series on a StackedAreaChart then ChartEntity showed sometimes corresponds to 1st series. I want to ChartEntity to correspond to 2nd series as long as I am in 2nd series. Is there a way to do this.

I wish there was a simple way to handle this type of thing as this seems to be very common functionality.
Thanks & Regards,
Deepika

Locked