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.

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;
    }
};
csharp Related
Comments