How to use DateTimePoint in LiveCharts, line chart

121 Views Asked by At

I am using LiveCharts 2. I'm new to Live Charts/charting like this in general.

I want to use a line chart with the y-axis as 'Temperature' and the x-axis as 'Time'.

Using Live Charts, I can generate a line chart by using coordinates (int) (seen in the commented-out code below), but I am struggling to change the x-axis to 'Time'.

The Live Chart documentation always seems to focus on ints for the x/y.

How can I use Time from TempList?

public partial class ExampleViewModel : ObservableObject
    {
        private readonly ObservableCollection<ObservableValue> _observableValues;
      
        public ExampleViewModel()
        {
            using var streamReader = new StreamReader("data.json");
            
            var samples = JsonSerializer.Deserialize<TempList[]>(streamReader.ReadToEnd());
            
            var dateTimePoints = samples.Select(x => new DateTimePoint(x.Time, x.Temperature));

            
            Series = new ObservableCollection<ISeries>
            {
                new LineSeries<DateTimePoint>
                {
                    Values = dateTimePoints,
                    Mapping = (dateTimePoints, chartPoint) =>
                    {
                        chartPoint.x = dateTimePoints.DateTime.Hour; //no such x property
                        chartPoint.y = dateTimePoints.Value; //no such y property
                    },
                    Fill = null
                }
            };

            // working with int values

            //Series = new ObservableCollection<ISeries>
            //{
            //    new LineSeries<TempList>
            //    {
            //        Values = samples,
            //        Mapping = (sample, chartPoint) =>
            //        {
            //            chartPoint.Coordinate = new(sample.TimeInt, sample.Temperature);
            //        },
            //        Fill = null
            //    }
            //};


        }

        public ObservableCollection<ISeries> Series { get; set; }

        public class TempList
        { 
            public int Temperature { get; set; }
            public int TimeInt { get; set; }
            public DateTime Time { get; set; }
        }
       
    }
0

There are 0 best solutions below