crammer
crammer

Reputation: 21

Oxyplot- WPF: Adding data from a List of double to DataPoint

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; }
    }
}

Upvotes: 0

Views: 1401

Answers (1)

Mikitori
Mikitori

Reputation: 617

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;

Upvotes: 1

Related Questions