How to plot DateTime v Value

Oystein Bjorke vor 10 Jahren 0
This discussion was imported from CodePlex

jessimb wrote at 2014-05-26 21:20:

I'm sorry if this is a duplicate. I am struggling to create a plot given a list of Times and Values. The examples have the times in double format, which I'm not sure where that is coming from. Thanks for your help!

Auriou wrote at 2014-05-27 17:56:

Hello, Here is a small example. The principle is to convert the date in double with the static function DateTimeAxis.ToDouble
// Objet (client) with DateTime and double
var objList = new List<Tuple<DateTime, double>>()
 {
     new Tuple<DateTime, double>(DateTime.Now, 10),
     new Tuple<DateTime, double>(DateTime.Now.AddMonths(-1), 20),
     new Tuple<DateTime, double>(DateTime.Now.AddMonths(-2), 30),
     new Tuple<DateTime, double>(DateTime.Now.AddMonths(-3), 40),
     new Tuple<DateTime, double>(DateTime.Now.AddMonths(-4), 50),
     new Tuple<DateTime, double>(DateTime.Now.AddMonths(-5), 60)
 };

// define an axis Date  
var dateTimeAxis = new DateTimeAxis()
 {
    Title = "Date",
    Position = AxisPosition.Bottom,
    IntervalType = DateTimeIntervalType.Months,
    StringFormat = "MMM yy"
 };

// Convert object client  to  DataPoint
var points = objList.Select(obj => new DataPoint(DateTimeAxis.ToDouble(obj.Item1), obj.Item2)).ToList();

// Create Line
var linearSeries = new LineSeries("DateLine")
 {
     ItemsSource = points
 };