3

I'm a bit of a c# n00b and a complete Live Charts n00b. I'm trying to create a simple graph that spans a number of days. The graph displays the data but the date label does not format correctly on the X Axis. I believe its because of how I'm using the Live Charts Formatter.

XAML in MainWindow

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

Function that creates chart.

MainWindow _MainWindow = (MainWindow)Application.Current.MainWindow;

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

Func<double, string> Formatter = value => new DateTime((long)(value * TimeSpan.FromDays(1).Ticks)).ToString("d");

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

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

        Fill = Brushes.Transparent
    },

};

Application.Current.Dispatcher.Invoke((Action)delegate
{
    if (_MainWindow.ct.IsCancellationRequested) return;

    _MainWindow.RankGraph.Series = Series;

});

I'm also setting the Formatter in mainwindow.cs

public Func<double, string> Formatter   { get; set; }           = value => new DateTime((long)(value * TimeSpan.FromDays(1).Ticks)).ToString("d");

I've tried declaring the Formatter pretty much everywhere but it doesn't look like the XAML in using it. The code above produces the graph below.

enter image description here

How do I correctly use the Formatter option when creating the graph from a simple method rather than in a UserControl class wrapper as all the examples show.

Mr J
  • 2,655
  • 4
  • 37
  • 58

2 Answers2

1

For some reason, the bindings in XAML don't work sometimes. You need to name your LiveCharts controls in XAML (to anything):

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

and then bind both Series and LabelFormatter in your code behind:

RankGraph.Series = Series;
RankGraphAxisX.LabelFormatter = Formatter;
Daulet Kshibekov
  • 181
  • 2
  • 2
  • 8
1

I noticed this also, but I'm using MVVM and it's not working even if I move the Formatter = value => value.ToString("N"); before setting the values to ChartValues() and SeriesCollection. What I did is

  1. Name the axis:
<lvc:CartesianChart.AxisY>
    <lvc:Axis x:Name="SalesGraphAxisY" Title="Sold Apps"></lvc:Axis>
</lvc:CartesianChart.AxisY>
  1. In the code-behind.
public MerchandiseAnalysisByCategory()
{
      InitializeComponent();

      Formatter = value => value.ToString("N");
      SalesGraphAxisY.LabelFormatter = Formatter;
}

public Func<double, string> Formatter { get; set; }
  1. Make sure that Formatter = value => value.ToString("N"); comes first.
user3856437
  • 2,071
  • 4
  • 22
  • 27