Hi,
One way to detect a collision between an annotation and an axis is to set the location of the annotation using AxisValuesBoudaries, and then compare the boundaries to axis ranges inside an event.
Setting the annotation to use AxisValuesBoundaries:
Code: Select all
AnnotationXY anno = new AnnotationXY(_chart.ViewXY, _chart.ViewXY.XAxes[0], _chart.ViewXY.YAxes[0]);
anno.Style = AnnotationStyle.Callout;
anno.TargetAxisValues.SetValues(4, 300); // Where the annotation points to
anno.Sizing = AnnotationXYSizing.AxisValuesBoundaries;
anno.AxisValuesBoundaries.SetValues(3.5, 4.2, 340, 400); // Set annotation size in axis values
_chart.ViewXY.Annotations.Add(anno);
The event can be for example RangeChanged, which triggers every time the minimum and/or maximum values of the corresponding axis are changed. In this example, every time Y axis range is changed, we check if the bottom or the top of the annotation hits the bottom or the top of the visible graph area. If this happens, the annotation is moved up or down.
Code: Select all
_chart.ViewXY.YAxes[0].RangeChanged += Axis_RangeChanged;
private void Axis_RangeChanged(object sender, RangeChangedEventArgs e)
{
if (_chart.ViewXY.YAxes[0].Minimum > _chart.ViewXY.Annotations[0].AxisValuesBoundaries.YMin)
{
_chart.ViewXY.Annotations[0].AxisValuesBoundaries.SetValues(3.5, 4.2, _chart.ViewXY.Annotations[0].AxisValuesBoundaries.YMin + 150, _chart.ViewXY.Annotations[0].AxisValuesBoundaries.YMax + 150);
}
else if (_chart.ViewXY.YAxes[0].Maximum < _chart.ViewXY.Annotations[0].AxisValuesBoundaries.YMax)
{
_chart.ViewXY.Annotations[0].AxisValuesBoundaries.SetValues(3.5, 4.2, _chart.ViewXY.Annotations[0].AxisValuesBoundaries.YMin - 150, _chart.ViewXY.Annotations[0].AxisValuesBoundaries.YMax - 150);
}
}
There are also other annotation properties, that can be useful in some cases. Setting KeepVisible true makes the annotation always stay inside the graph area, no matter how much the graph is moved.
Setting ClipInsideGraph to false allows the annotation to be drawn outside the axes. This can be combined with RenderBehindAxis, which controls if the annotation should be drawn over the axis ticks and labels.