How to find out which graph item is located at particular coordinates

Questions & Answers

Summary

This article shows how to determine which particular item of the graph can be found at given coordinates.

Description

The com.yworks.canvas.CanvasComponent offers a method which returns an Iterable over the display objects which can be found at a given location: getCanvasObjects(worldX:Number, worldY:Number):Iterable. If one wants to find out which graph item is hit by given coordinates, the most convenient way is to iterate over these objects. The following method does so and returns a node, if there is one found among the display objects' user objects (otherwise, it returns null): private function findHitNode( graphCanvas:CanvasComponent, hitX:Number, hitY:Number ):INode { var it:Iterator = graphCanvas.getCanvasObjects( hitX, hitY ).iterator(); while( it.hasNext() ) { var hitObj:ICanvasObject = ICanvasObject( it.next() ); var hitNode:INode = hitObj.userObject as INode; if( null != hitNode ) { return hitNode; } } return null; } Note, that if there is more than one node located at the given coordinates, this method will only find the one which is created first, which usually, but not necessarily, is the topmost node.

Categories this article belongs to:
yFiles FLEX > Displaying and Editing Graphs > User Interaction
Applies to:
yFiles FLEX: 1.4, 1.5, 1.6, 1.7, 1.8
Keywords:
display objects - coordinates - hit objects