List of Exercises

Using DateTime Axis in OxyPlot WPF C#

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

namespace OxyPlot_DateTime_Axis
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    // how to graph a date oxyplot
    public MainWindow()
    {
      InitializeComponent();
    }

    private void lineSeries_Click(object sender, RoutedEventArgs e)
    {
      DateTime[] x = Enumerable.Range(1, 100).Select(v => new DateTime(2000,1,1).AddMonths(v)).ToArray();
      double[] y = x.Select(v => (double)v.Month* (double)v.Month).ToArray();

      var model = new PlotModel { Title = "OxyPlot - Line Series" };
      model.Axes.Add(new LinearAxis
      {
        Position = AxisPosition.Left,
        Title = "Month",
      });
      model.Axes.Add(new DateTimeAxis
      {
        Position = AxisPosition.Bottom,
        Title = "Date",
      });
      var lineSeries = new LineSeries
      {
        MarkerType = MarkerType.Circle,
        MarkerSize = 2,
        MarkerFill = OxyColors.Red,
        StrokeThickness = 1,
      };
      for (int i = 0; i < x.Length; i++)
      {
        lineSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(x[i]), y[i]));
      }
      model.Series.Add(lineSeries);
      model.Series[model.Series.Count - 1].Title = "Month^2";
      model.Legends.Add(new OxyPlot.Legends.Legend()
      {
        LegendPosition = LegendPosition.TopLeft,
        LegendFontSize = 12
      });
      plot.Model = model;
    }
  }
}
		
	 


Download the project of Visual Studio 2022 in DropBox Download


How to add DateTime Axis in OxyPlot WPF C#


Video Thumbnail