Search code examples
c#wpfoxyplot

How to draw rectangle over a plot?


Is it possible to draw rectangle using OxyPlot? Something like enter image description here:

or does OxyPlot not support this?


Solution

  • You can indeed.

    There's a couple of ways you can do it. I would prefer to use an annotation as this will also give you a highlighted background and give you the option to add click events on this area. There's probably a way to remove the highlight if you want to make it look like the image you've provided.

    var Event = new PolygonAnnotation();
    
    Event.Layer = AnnotationLayer.BelowAxes;
    Event.StrokeThickness = 5;
    Event.Stroke = OxyColor.FromRgb(0, 0, 255);
    Event.LineStyle = LineStyle.Automatic;
    
    Event.Points.Add(new DataPoint(X, Y));
    Event.Points.Add(new DataPoint(X, Y));            
    Event.Points.Add(new DataPoint(X, Y));
    Event.Points.Add(new DataPoint(X, Y));
    

    Another more simplistic way is to create a line series and give it the corners of your rectangle.