Building a Chart > Converting Data Coordinates |
The ChartGroupscollection provides methods which enable you to do the following:
· Convert from data coordinates to pixel coordinates and vice versa.
· Determine the pixel coordinates of a given data point in a series, or the closest point to a given set of pixel coordinates.
To convert from data coordinates to pixel coordinates, call the DataCoordToCoord method. For example, the following code obtains the pixel coordinates corresponding to the screen coordinates (5.1, 10.2):
Dim PixelX As Long
Dim PixelY As Long
...
With Chart2D1.ChartGroups(1)
.DataCoordToCoord 5.1, 10.2, PixelX, PixelY
End With
' PixelX and PixelY now contain the pixel coordinate value
If the chart coordinate is out of the visible range of the chart, both of the pixel X- and Y-coordinates are set to -1.
To convert from pixel coordinates to data coordinates, call CoordToDataCoord. For example, the following converts the pixel coordinates (225, 92) to their equivalent data coordinates:
Dim ScreenX As Double
Dim ScreenY As Double
Dim Region As Integer
...
With Chart2D1.ChartGroups(1)
Region =.CoordToDataCoord (225, 92, ScreenX, ScreenY)
End With
' ScreenX and ScreenY now contain the data coordinate value
CoordToDataCoord returns, as its return value, the region of the chart in which the pixel coordinates are located. This region is represented as a regionconstant.
To determine the pixel coordinates of a given data point, call DataIndexToCoord. For example, the following code obtains the pixel coordinates of the third point in the first data series:
Dim PixelX As Long
Dim PixelY As Long
...
With Chart2D1.ChartGroups(1)
.DataIndexToCoord 1, 3, PixelX, PixelY
End With
' PixelX and PixelY now contain the pixel coordinate value
To determine the closest data point to a set of pixel coordinates, call CoordToDataIndex:
Dim Series As Long
Dim Point As Long
Dim Distance As Long
Dim Region As Integer
...
With Chart2D1.ChartGroups(1)
Region =.CoordToDataIndex (225, 92, oc2dFocusXY, Series, _
Point, Distance)
End With
The third argument passed to CoordToDataIndex specifies how the nearest series and point value are determined. This argument must be a focusconstant.
CoordToDataIndex returns the series and point value corresponding to the closest data point, and also returns the distance in pixels between the pixel coordinates and the point. CoordToDataIndex returns, as its return value, the region of the chart in which the pixel coordinates are located. This region is represented as a regionconstant. When the returned region is oc2dRegionInLegend, the returned point value is also set to oc2dRegionInLegend, and the returned series is the closest element in the legend.