C# Exercises

Home AgriMetSoft About Contact

OxyPlot Annotation in WPF C# Arrow, Polygon, Text

	    
using OxyPlot.Annotations;
using OxyPlot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OxyPlot.Series;
using OxyPlot.Axes;
using OxyPlot.Legends;
using OxyPlot.Utilities;
using System.Reflection;
using System.Windows.Resources;
using System.Drawing;
using System.IO;

namespace OxyPlot_Annotations
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    private void addArrow_Click(object sender, RoutedEventArgs e)
    {
      var arrowAnn = new ArrowAnnotation
      {
        StartPoint = new DataPoint(2, 20),
        EndPoint = new DataPoint(4, 40)
      };
      plot.Model.Annotations.Add(arrowAnn);
      plot.InvalidatePlot(true);
    }
    private void polygonAnnotation_Click(object sender, RoutedEventArgs e)
    {
      var pa = new PolygonAnnotation();
      pa.Points.Add(new DataPoint(4.5, 10));
      pa.Points.Add(new DataPoint(5.3, 20));
      pa.Points.Add(new DataPoint(7, 25));
      pa.Points.Add(new DataPoint(8, 50));
      pa.Points.Add(new DataPoint(4, 20));
      pa.Text = "Polygon 1";
      pa.Fill = OxyColors.Orange;
      plot.Model.Annotations.Add(pa);
      plot.InvalidatePlot(true);
    }
    private void textAnnotation_Click(object sender, RoutedEventArgs e)
    {
      var textAnn = new TextAnnotation
      {
        TextPosition = new DataPoint(3, 35),
        Text = "Arrow",
        TextRotation = -23,
        StrokeThickness = 0,
        TextColor = OxyColors.DarkBlue,
      };
      plot.Model.Annotations.Add(textAnn);
      plot.InvalidatePlot(true);
    }





    private void lineSeries_Click(object sender, RoutedEventArgs e)
    {
      double[] x = Enumerable.Range(1, 100).Select(v => ((double)v) / 10).ToArray();
      double[] y = x.Select(v => v * v).ToArray();
      var model = new PlotModel { Title = "OxyPlot - Line Series" };
      model.Axes.Add(new LinearAxis
      {
        Position = AxisPosition.Left,
        Title = "Y",
      });
      model.Axes.Add(new LinearAxis
      {
        Position = AxisPosition.Bottom,
        Title = "Number of items",
      });
      model = addLine(model, x, y, 2, OxyColors.Gray);
      model.Series[model.Series.Count - 1].Title = "X2";
      model.Legends.Add(new OxyPlot.Legends.Legend()
      {
        LegendPosition = LegendPosition.TopLeft,
        LegendFontSize = 12
      });
      plot.Model = model;

    }

    private PlotModel addLine(PlotModel model, double[] x, double[] y, double lineThick, OxyColor colorValue)
    {
      var lineSeries = new LineSeries { StrokeThickness = lineThick, Color = colorValue };
      for (int i = 0; i < x.Length; i++)
      {
        lineSeries.Points.Add(new DataPoint(x[i], y[i]));
      }
      model.Series.Add(lineSeries);
      return model;
    }

  }
}
		
	 


Download the project of Visual Studio 2022 in DropBox Download


How to use OxyPlot Annotation in WPF C# | Arrow Polygon Text Annotation


List of Exercises