Xceed Chart for WinForms v4.4 Documentation
Converting Chart Coordinates

Welcome to Xceed Chart for WinForms v4.4 > User Guide > Converting Chart Coordinates

It is a common task to convert between rom different chart coordinates: for example, if you want to locate a data point which is closest to the mouse cursor or to position a watermark over an axis value. This topic shows how to convert from scale to model, from model to viewport and vice versa. 

The first thing you should know about coordinate transformations is that you can use them only after the chart is rendered or during rendering itself. This ensures that the axes and control viewport have been properly calculated. The examples shipped with the control perform coordinate transformations in the chart's BeforeDraw and OnMouseDown events.

Converting from scale to model coordinates

Scale coordinates are the coordinates of the data points relative to the chart axes. Each data point translates to [x, y, z] scale coordinate which are then scaled from the chart axes to model coordinates. Therefore, in order to convert a scale coordinate to a model coordinate, you need to know the scale axis. The ConvertScaleToModelCoordinate method of the axis converts a scale coordinate to a model coordinate. It accepts two parameters: the first one specifies whether to clamp the scale coordinate to the axis ruler, and the second one is the scale coordinate itself. The following code locates the model coordinates corresponding to the origin of the scale coordinate system:

VB.NET  

Dim chart As Chart = chartControl.Charts(0)

Dim vecModelPoint As New Vector

vecModelPoint.x = chart.Axis(StandardAxis.PrimaryX).ConvertScaleToModelCoordinate(False, 0)

vecModelPoint.y = chart.Axis(StandardAxis.PrimaryY).ConvertScaleToModelCoordinate(False, 0)

vecModelPoint.z = chart.Axis(StandardAxis.Depth).ConvertScaleToModelCoordinate(False, 0)

C#  

Chart chart = chartControl.Charts[0];

Vector vecModelPoint = new Vector();

vecModelPoint.x = chart.Axis(StandardAxis.PrimaryX).ConvertScaleToModelCoordinate(false, 0);

vecModelPoint.y = chart.Axis(StandardAxis.PrimaryY).ConvertScaleToModelCoordinate(false, 0);

vecModelPoint.z = chart.Axis(StandardAxis.Depth).ConvertScaleToModelCoordinate(false, 0);

Converting from model to viewport and control client coordinates

These are probably among the most useful conversions because they allow you to position custom drawings in the control canvas based on chart model coordinates. The control client coordinates are coordinates relative to the control origin in the form. The control viewport coordinates are coordinates relative to the control origin in the form, but with an offset applied that depends on the current background frame. You use the ConvertModelToClientCoordinate and ConvertModelToViewportCoordinate methods of the Chart object to perform these conversions, respectively. The following code shows how to place a watermark at the origin of the control's model coordinate system:

VB.NET  

Dim watermark As Watermark = m_ChartControl.Watermarks(0)

Dim chart As Chart = m_ChartControl.Charts(0)

Dim vecModelPoint As New Vector

If (chart.ConvertModelToViewportCoordinate(vecModelPoint, vecViewPoint) = True) Then

Dim rcViewport As RectangleF = m_ChartControl.GetControlViewport()

watermark.HorizontalMargin = 100 * vecViewPoint.x / rcViewport.Width

watermark.VerticalMargin = 100 * vecViewPoint.y / rcViewport.Height

End If

C#  

Watermark watermark = m_ChartControl.Watermarks[0];

Chart chart = m_ChartControl.Charts[0];

Vector vecModelPoint = new Vector();

if (chart.ConvertModelToViewportCoordinate(vecModelPoint, ref vecViewPoint))

{

RectangleF rcViewport = m_ChartControl.GetControlViewport();

watermark.HorizontalMargin = 100 * vecViewPoint.x / rcViewport.Width;

watermark.VerticalMargin = 100 * vecViewPoint.y / rcViewport.Height;

}

Note that we used the GetControlViewport method of the chart control in order to obtain the dimensions of the control viewport, which are usually smaller than the control client area.

Converting from control client to model coordinates

Converting from 2D to 3D coordinates is somewhat more complex than 3D to 2D because you need to recover a "missing coordinate". Therefore, you need a plane in 3D which recovers the coordinate lost from the 3D to 2D conversion. You use the MapClientPointToModelPlane method of the chart to perform this conversion. It has the following definition:

VB.NET  

Public Function MapClientPointToModelPlane(ByVal horzAxis As Axis, ByVal vertAxis As Axis, ByVal depthAxis As Axis, ByVal fDepthValue As Single, ByVal ptViewPoint As Point, ByRef vecModelPoint As Vector) As Boolean

C#  

public bool MapClientPointToModelPlane(Axis horzAxis, Axis vertAxis, Axis depthAxis, float fDepthValue, Point ptViewPoint, ref Vector vecModelPoint)

The first two parameters define the plane's normal vector, which is orthogonal to the plane formed from the horzAxis and vertAxis axes. The depthAxis and fDepthValue parameters define a point in 3D which lies on the plane. The following image shows how this plane looks in the case of the primaryX, primaryY and primaryZ axes: 

Here, the depth value is set to 50. When you try to convert a client point that lies on the red area, the converted model point will have the [x, y, z] coordinates of the point which lies on the model plane.

Converting from model to scale coordinates

Finally, you can convert model coordinates to scale coordinates by using the ConvertModelToScaleCoordinate function of the axes. The following code gets the scale coordinates of the mouse cursor coordinates mapped on the XY plane at depth value 50:

VB.NET  

Private Sub OnMouseDown(ByVal sender As System.Object, ByVal e As MouseEventArgs)

Dim chart As Chart = m_ChartControl.Charts(0)

Dim ptViewPoint As New Point(e.X, e.Y)

Dim fDepthValue As Single = 50.0F

' XY plane

Dim horzAxis As Axis = chart.Axis(StandardAxis.PrimaryX)

Dim vertAxis As Axis = chart.Axis(StandardAxis.PrimaryY)

Dim depthAxis As Axis = chart.Axis(StandardAxis.Depth)

Dim vecModelPoint As New Vector

If (chart.MapClientPointToModelPlane(horzAxis, vertAxis, depthAxis, fDepthValue, ptViewPoint, vecModelPoint)) Then

Dim fX As Double = horzAxis.ConvertModelToScaleCoordinate(vecModelPoint.x, True)

Dim fY As Double = vertAxis.ConvertModelToScaleCoordinate(vecModelPoint.y, True)

Dim fZ As Double = depthAxis.ConvertModelToScaleCoordinate(vecModelPoint.z, True)

'do something with the fX, fY and fZ scale coordinates

End If

End Sub

C#  

private void OnMouseDown(object sender, MouseEventArgs e)

{

Chart chart = chartControl.Charts[0];

Point ptViewPoint = new Point(e.X, e.Y);

float fDepthValue = 50.0f;

// XY plane

Axis horzAxis = chart.Axis(StandardAxis.PrimaryX);

Axis vertAxis = chart.Axis(StandardAxis.PrimaryY);

Axis depthAxis = chart.Axis(StandardAxis.Depth);

Vector vecModelPoint = new Vector();

if (chart.MapClientPointToModelPlane(horzAxis, vertAxis, depthAxis, fDepthValue, ptViewPoint, ref vecModelPoint))

{

double fX = horzAxis.ConvertModelToScaleCoordinate(vecModelPoint.x, ClampValuesToRulerCheckBox.Checked);

double fY = vertAxis.ConvertModelToScaleCoordinate(vecModelPoint.y, ClampValuesToRulerCheckBox.Checked);

double fZ = depthAxis.ConvertModelToScaleCoordinate(vecModelPoint.z, ClampValuesToRulerCheckBox.Checked);

// do something with the fX, fY and fZ scale coordinates

}

}