Skip to main content

How to measure Annotation

Annotation has many properties which controls its size and location (e.g. AnnotationXY ). Annotation can be dragged to different position on the screen and it can be resized. Also text inside of Annotation can be change at any time. While in most cases user don't need to worry and rendering will be done automatically, sometimes user may want to know size and location of Annotation. For some settings answer is very trivial (e.g. if AnnotationXYSizing == AxisValuesBoundaries). However, it is less obvious when other settings are used.

From version 12.4 Annotation. Size property can be used to access Width and Height of Annotation after latest rendering event (see Size).

Before 12.4, following method(s) will help to answer the question about size and location of Annotation, if

annotation.Sizing = AnnotationXYSizing.Automatic;
/// <summary>
/// Get Annotation's TextBox size and location on the screen [in PX]
/// </summary>
/// <param name="annotation"></param>
/// <param name="size">Size in Pixels</param>
/// <param name="location">Location in Pixels</param>
internal void GetAnnotationSizeAndLocation(AnnotationXY annotation, out Size size, out Point location)
{
float fMaxWidth = 0;
float fMaxHeight = 0;
string[] aContent = annotation.Text.Split('\n');

// Text row max Height
float fRowMax = _chart.MeasureTextPX("Åg", annotation.TextStyle.Font).Y;

for (int i = 0; i < aContent.Length; i++)
{
var point = _chart.MeasureTextPX(aContent[i], annotation.TextStyle.Font);
if (fMaxWidth < point.X)
fMaxWidth = point.X;
}

// add padding space
fMaxWidth += 2 * annotation.AutoSizePadding;
fMaxHeight = fRowMax * aContent.Length + 2 * annotation.AutoSizePadding;

size = new Size(fMaxWidth, fMaxHeight);

double dX = float.MinValue;
double dY = float.MinValue;
switch (annotation.LocationCoordinateSystem)
{
case CoordinateSystem.AxisValues:
dX = XValueToCoord(annotation.LocationAxisValues.X, annotation.AssignXAxisIndex);
dY = YValueToCoord(annotation.LocationAxisValues.Y, annotation.AssignYAxisIndex);
break;

case CoordinateSystem.RelativeCoordinatesToTarget:
switch (annotation.TargetCoordinateSystem)
{
case AnnotationTargetCoordinates.AxisValues:
dX = XValueToCoord(annotation.TargetAxisValues.X, annotation.AssignXAxisIndex);
dY = YValueToCoord(annotation.TargetAxisValues.Y, annotation.AssignYAxisIndex);
break;

case AnnotationTargetCoordinates.ScreenCoordinates:
dX = annotation.TargetScreenCoords.X;
dY = annotation.TargetScreenCoords.Y;
break;
}
dX += annotation.LocationRelativeOffset.X;
dY += annotation.LocationRelativeOffset.Y;

// take into account Anchor position as well
dX -= annotation.Anchor.X * size.Width;
dY -= annotation.Anchor.Y * size.Height;

break;

case CoordinateSystem.ScreenCoordinates:
dX = annotation.LocationScreenCoords.X;
dY = annotation.LocationScreenCoords.Y;

// take into account Anchor position as well
dX -= annotation.Anchor.X * size.Width;
dY -= annotation.Anchor.Y * size.Height;

break;
}

location = new Point(dX, dY);
}

private float XValueToCoord(double value, int xAxisIndex)
{
return _chart.ViewXY.XAxes[xAxisIndex].ValueToCoord(value, false);
}

private float YValueToCoord(double value, int yAxisIndex)
{
return _chart.ViewXY.YAxes[yAxisIndex].ValueToCoord(value, false);
}