Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic WPF LiveCharts DateTime example not working

I've followed the Live Charts TimeDate basic example as closely as I can but can't seem to get the X axis to display properly.

https://lvcharts.net/App/examples/v1/wpf/Date%20Time

My MainWindow code

public partial class MainWindow : Window
{
    public Func<double, string> Formatter { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        var dayConfig = Mappers.Xy<DateModel>()
          .X(dateModel => dateModel.DateTime.Ticks / TimeSpan.FromDays(1).Ticks)
          .Y(dateModel => dateModel.Value);

        SeriesCollection Series = new SeriesCollection(dayConfig)
        {
            new LineSeries
            {
                Title = "Google Rank",

                Values = new ChartValues<DateModel>
                {
                    new Wpf.CartesianChart.Using_DateTime.DateModel
                    {
                        DateTime    = System.DateTime.UtcNow,
                        Value       = 5
                    },
                    new Wpf.CartesianChart.Using_DateTime.DateModel
                    {
                        DateTime    = System.DateTime.UtcNow.AddDays(1),
                        Value       = 9
                    },
                    new Wpf.CartesianChart.Using_DateTime.DateModel
                    {
                        DateTime    = System.DateTime.UtcNow.AddDays(2),
                        Value       = 4
                    }
                },

                Fill = Brushes.Transparent,

            },
        };

        Formatter = value => new System.DateTime((long)(value * TimeSpan.FromDays(1).Ticks)).ToString("t");

        RankGraph.Series = Series;
    }
}

My XAML on my MainWindow

<Grid>
    <lvc:CartesianChart x:Name="RankGraph" Series="{Binding Series}">
        <lvc:CartesianChart.AxisX>
            <lvc:Axis LabelFormatter="{Binding Formatter}"></lvc:Axis>
        </lvc:CartesianChart.AxisX>
    </lvc:CartesianChart>
</Grid>

Date Model Object

namespace Wpf.CartesianChart.Using_DateTime
{
    public class DateModel
    {
        public DateTime DateTime { get; set; }
        public double Value { get; set; }
    }
}

This produces the following with the dates messed up...

enter image description here

like image 789
Mr J Avatar asked May 15 '17 17:05

Mr J


Video Answer


1 Answers

You forgot to set your data context:

DataContext = this;

enter image description here

like image 90
jsanalytics Avatar answered Oct 13 '22 00:10

jsanalytics