0

I am new to WPF and the project I'm working on requires me to plot a list of double on XY chart. I added Oxyplot to my project for the charting but I'm having challenges to get a plot.

I followed the example on Oxyplot site (see code below), but I discovered that the DataPoint can only accept double values of x and y not array or list of doubles.

How can I plot List<double> for XValues and List<double> for YValues?

namespace WpfApplication2
{
    using System.Collections.Generic;

    using OxyPlot;

    public class MainViewModel
    {
        public MainViewModel()
        {
            this.Title = "Example 2";
            this.Points = new List<DataPoint>
                              {
                                  new DataPoint(0, 4),
                                  new DataPoint(10, 13),
                                  new DataPoint(20, 15),
                                  new DataPoint(30, 16),
                                  new DataPoint(40, 12),
                                  new DataPoint(50, 12)
                              };
        }

        public string Title { get; private set; }

        public IList<DataPoint> Points { get; private set; }
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
crammer
  • 21
  • 7
  • Edited: Please how can I pass in List for XValues and List for YValues into DataPoint which only accepts single value of x and y at a time? – crammer Jun 16 '18 at 22:29
  • You need to project/merge the two lists of doubles into a single list of object (e.g. `DataPoint`). – Jan Paolo Go Jun 17 '18 at 00:13

1 Answers1

1

I really don't see why you cannot directly store a list of DataPoint... but let's say you're stucked with your 2 lists and I'm assuming that your lists have same length (if not you have an issue since all points to draw should have an X and Y value).

So I guess something like that:

List<double> XValues = new List<double> { 0, 5, 10, 22, 30 };
List<double> YValues = new List<double> { 2, 11, 4, 15, 20 };
for (int i = 0; i < XValues.Count; ++i)
{
  Points.Add(new DataPoint(XValues[i], YValues[i]));
}

It's not really elegant and if you are the one creating the lists you should merge them into a list of DataPoint like said @PaoloGo. If you prefer to use a custom object in case you don't use oxyplot, you can create a simple one like that for example:

public struct ChartPoint
{
    public double X;
    public double Y;
    public ChartPoint(double x, double y)
    {
        X = x;
        Y = y;
    }
}

And then you store this:

List<ChartPoint> points;
Mikitori
  • 589
  • 9
  • 21