List of Exercises

Filling Between Two LineSeries in WPF C#

	    

using OxyPlot.Series;
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.Axes;
using OxyPlot.Legends;

namespace OxyPlot_Fill_Between_Two_Series
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
      double[][] dd = new double[3][];
      dd[0] = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };// It's X
      dd[1] = dd[0].Select(v => (v - 5) * (v - 5) / 3).ToArray();// It's Y
      dd[2] = new double[] { 0.21, 0.33, 0.35, 0.5, 0.39, 0.23, 0.28, 0.17,0.18,0.31 };//It's STD


      var model = new PlotModel { Title = "Fill Between Two Series" };
      model.Axes.Add(new LinearAxis
      {
        Position = AxisPosition.Left,
        Title = "Y",
      });
      model.Axes.Add(new LinearAxis
      {
        Position = AxisPosition.Bottom,
        Title = "Number of items",
      });
      var up = dd[1].Zip(dd[2], (u, v) => u + 3 * v).ToArray();
      var dw = dd[1].Zip(dd[2], (u, v) => u - 3 * v).ToArray();
      model = addLine(model, dd[0], dd[1], 2, OxyColors.Black);
      model.Series[model.Series.Count - 1].Title = "Model";
      model = addLine(model, dd[0], up, 3, OxyColors.Red);
      model.Series[model.Series.Count - 1].Title = "3Std Up";
      model = addLine(model, dd[0], dw, 3, OxyColors.Green);
      model.Series[model.Series.Count - 1].Title = "3Std Down";

      model.Legends.Add(new OxyPlot.Legends.Legend()
      {
        LegendPosition = LegendPosition.TopLeft,
        LegendFontSize = 12
      });
      model = addAnnot(model, dd[0], dd[1], dw, OxyColors.ForestGreen);
      model = addAnnot(model, dd[0], dd[1], up, OxyColors.DarkOrange);
      plot.Model = model;
    }
    private PlotModel addAnnot(PlotModel model, double[] x, double[] y1, double[] y2, OxyColor color)
    {
      AreaAnnotation an = new AreaAnnotation();
      an.x = x;
      an.y1 = y1;
      an.y2 = y2;
      an.Fill = color;
      model.Annotations.Add(an);
      return 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;
    }

  }
}
===================AreaAnnotation.cs=====================
using OxyPlot;
using OxyPlot.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OxyPlot_Fill_Between_Two_Series
{
  public class AreaAnnotation : Annotation
  {
    public OxyColor Fill { get; set; }
    public OxyColor Stroke { get; set; }
    public double StrokeThickness { get; set; }
    public string Text { get; set; }
    public double[] x { get; set; }
    public double[] y1 { get; set; }
    public double[] y2 { get; set; }

    public AreaAnnotation()
    {
      this.Fill = OxyColors.LightGray;
      this.Stroke = OxyColors.Black;
      this.StrokeThickness = 1;
      this.Text = null;
    }

    public override void Render(IRenderContext rc)
    {
      List<OxyPlot.DataPoint> pp = makePoints();
      var actualPoints = new ScreenPoint[pp.Count];
      for (int i = 0; i < actualPoints.Length; i++)
        actualPoints[i] = this.Transform(pp[i]);
      rc.DrawPolygon(actualPoints.ToList(), Fill, Stroke, StrokeThickness, this.EdgeRenderingMode);
    }

    private List<DataPoint> makePoints()
    {
      List<DataPoint> p = new List<DataPoint>();
      for (int i = 0; i < x.Length; i++)
      {
        p.Add(new DataPoint(x[i], y1[i]));
      }
      for (int i = 0; i < x.Length; i++)
      {
        p.Add(new DataPoint(x[x.Length - i - 1], y2[x.Length - i - 1]));
      }
      p.Add(new DataPoint(x[0], y1[0]));
      return p;
    }
  }

}
		
	 


Download the project of Visual Studio 2022 in DropBox Download


How to Fill Between Two Line Series in C# WPF using OxyPlot


Video Thumbnail