2D Chart for WinForms | ComponentOne
End-User Interaction / Coordinate Conversion Methods / Converting Pixel Coordinates into Data Points and Vice Versa / Converting Pixel Coordinates into Data Points
In This Topic
    Converting Pixel Coordinates into Data Points
    In This Topic

    The two methods that convert pixel coordinate values into chart data coordinates and data indices are the CoordToDataCoord and CoordToDataIndex methods of the ChartGroup object. Both of these methods take a coordinate value, presumably from a MouseMove event, and return a PlotArea coordinate or the nearest data point.

    CoordToDataCoord Method

    The CoordToDataCoord method takes four parameters. The first two parameters (e.X and e.Y in the example below) are the pixel coordinates from the MouseMove event. The second two parameters are integer values that the method will fill with the X and Y Data Coordinate data. The following code is an example of this method in the MouseMove event:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim CoordXOutput As Double Dim CoordYOutput As Double 
        C1Chart1.ChartGroups.Group0.CoordToDataCoord(e.X, e.Y, CoordXOutput, CoordYOutput) 
        Debug.WriteLine("X
    Data Coordinate: " & CoordXOutput.ToString()) 
        Debug.WriteLine("Y Data Coordinate: " & CoordYOutput.ToString())
    

    To write code in C#

    C#
    Copy Code
    double CoordXOutput = 0;
        double CoordYOutput = 0;
        c1Chart1.ChartGroups.Group0.CoordToDataCoord(e.X, e.Y, CoordXOutput, CoordYOutput);
    c1Chart1.ChartGroups.Group0. CoordToDataCoord(e.X, e.Y, ref CoordXOutput, ref CoordYOutput);
        ConsoleDebug.Writeline("X Data Coordinate: " +
    CoordXOutput.ToString()); ConsoleDebug.Writeline("Y Data Coordinate: " + CoordYOutput.ToString());
    
    Note: For a complete sample using the CoordToDataCoord method, please see the StepChart or the CoordToMapping3D sample located here.

    CoordToDataIndex Method

    The CoordToDataIndex method takes six parameters. The first two parameters (e.X and e.Y in the example below) are the pixel coordinates from the MouseMove Event. The third parameter is a CoordinateFocusEnum value. This is an enumeration that specifies which axis to focus on when determining which data point is closer and its distance from the pixel coordinate. For instance, if X is selected as the focus, then it will return the nearest point that has the closest X-coordinate, regardless of the value of the Y-coordinate. The fourth and fifth parameters are integer values that the method will fill with the appropriate Series and Point indices. The sixth parameter is an integer value that the method fills with the distance from the pixel coordinate specified to the data point in pixels. The following code is an example of this method in the MouseMove event:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim SeriesOutput As Integer Dim PointOutput As Integer Dim DistanceOutput As Integer
        C1Chart1.ChartGroups.Group0._ CoordToDataIndex(e.X, e.Y,
    CoordinateFocusEnum.XandYCoord,_ ref SeriesOutput, ref PointOutput, ref DistanceOutput)
        Debug.WriteLine("Series Index: " & SeriesOutput.ToString())
    Debug.WriteLine("Point Index: " & PointOutput.ToString()) 
        Debug.WriteLine("Distance From Point: " & DistanceOutput.ToString())
    

    To write code in C#

    C#
    Copy Code
    int SeriesOutput = 0; 
        int PointOutput = 0; 
        int DistanceOutput = 0; 
        c1Chart1.ChartGroups.Group0.CoordToDataIndex(e.X, e.Y, CoordinateFocusEnum.XandYCoord,
    ref SeriesOutput, ref PointOutput, ref DistanceOutput); 
        ConsoleDebug.Writeline("Series Index: " + SeriesOutput.ToString()); 
        ConsoleDebug.Writeline("Point
    Index: " + PointOutput.ToString()); 
        ConsoleDebug.Writeline("Distance From Point: " + DistanceOutput.ToString());
    
    Note: For a complete sample using the CoordToDataIndex method, see DataStyl, PieStuff, StepChart, or Scatter samples located here.
    See Also