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 | |
---|---|
|
C# | |
---|---|
|
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 | |
---|---|
|
C# | |
---|---|
|
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 | |
---|---|
|
C# | |
---|---|
|
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 | |
---|---|
|
C# | |
---|---|
|