When building custom interactions with the data series chart, it is important to be able to transition between unity’s local space and the chart space. In this way , you can convert a point on the chart to a point on the canvas and vise versa.
What are the chart space and local space
- Local Space – are coordinates in the data series chart’s RectTransform component. the coordinates can be converted to canvas space by calling transform.transformPoint(localSpace)
- Chart Space – are coordinates within the chart axis system.
Conversion methods
// converts a chart space coordinate into local space coordinate
public Vector3 ChartSpaceToLocalSpace(DoubleVector3 chartSpace)
//converts local space coordinate into chart space coordinate
public DoubleVector3 LocalSpaceToChartSpace(Vector3 localSpace)
You can call these methods in the following way
// covert the vector (1,1) in chart units to local space
Vector3 localSpace = chart.Axis.ChartSpaceToLocalSpace(new DoubleVector3(1,1,0));
// convert the vector in local space back to chart space
DoubleVector3 chartSpace = chart.Axis.LocalSpaceToChartSpace(localSpace);
Checking if the coordinates are within bounds
Once you have converted the coordinates , you may want to check if they are within the bounds of the visible local/chart space. Have a look at the following calls:
Vector3 localSpace = chart.Axis.ChartSpaceToLocalSpace(new DoubleVector3(1,1,0));
// returns true if local point is inside the data series chart's RectTransform
Chart.Axis.LocalViewContains(localPoint);
USe case example
In the indicator interaction (CanvasIndicatorInteraction.cs) places an interactive indicator on top of the chart. Here are some points of interest in this interaction
// this method finds the point at index i and returns it's local space position
public Vector3 GetPointLocalSpace(string category,int index,out DoubleVector3 chartSpace)
{
chartSpace = Chart.DataSource.GetCategory(category).Data.GetPointAt(index);
return Chart.Axis.ChartSpaceToLocalSpace(chartSpace);
}
.
.
.
// the indicator is placed at the local space point
Vector3 localPoint = GetPointLocalSpace(category, index,out chartPoint);
Indicator.anchoredPosition = localPoint;
.
.
.
// if the point is outside the bounds of the chart, the indicator is disabled
if (Chart.Axis.LocalViewContains(localPoint))
Indicator.gameObject.SetActive(true);
else
Indicator.gameObject.SetActive(false);