C# OxyPlot - Simple Crosshair

I couldn’t find any examples for adding a crosshair to the grid so I hacked something together using the ArrowAnnotation example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
LineAnnotation crosshair_v = new LineAnnotation();
crosshair_v.Type = LineAnnotationType.Vertical;
crosshair_v.Color = OxyColors.White;
crosshair_v.StrokeThickness = 1.05;
crosshair_v.LineStyle = LineStyle.Solid;
LineAnnotation crosshair_h = new LineAnnotation();
crosshair_h.Type = LineAnnotationType.Horizontal;
crosshair_h.Color = OxyColors.White;
crosshair_h.StrokeThickness = 1.05;
crosshair_h.LineStyle = LineStyle.Solid;
pm.Annotations.Add(crosshair_v);
pm.Annotations.Add(crosshair_h);
pm.MouseMove += (s, e) =>
{
if (crosshair_v != null && crosshair_h != null)
{
var dp = Axis.InverseTransform(e.Position, timeAxis, barAxis);
crosshair_v.X = dp.X;
crosshair_v.Y = 0;
crosshair_h.X = 0;
crosshair_h.Y = dp.Y;
pm.InvalidatePlot(false);
e.Handled = true;
}
};