Search code examples
c#livecharts

Live Charts AddRange not working with arrays or Series Collection


I am trying to create a light-as-possible real-time plotter with Live Charts Geared WPF, sampling roughly 125 points a second. Adding point-by-point regularly works just fine, except that CPU utilization is much too high.

I would like to draw several points at once, reading in values from a buffer, similar to the website example:

var temporalCv = new double[1000];

for (var i = 0; i < 1000; i++){
    temporalCv[i] = 5;
}

var cv = new ChartValues<double>();
cv.AddRange(temporalCv);

By itself, the example does not throw any issues. However, it throws a cannot convert from double[] to Systems.Collections.Generic.IEnumerable<Object> when I want to AddRange to ChartValues and GearedValues below.

SeriesCollection = new SeriesCollection
            {
                new GLineSeries
                {
                    Title = "Pressure",
                    Values = new GearedValues<double> {},

                },
                new LineSeries
                {
                    Title = "Pulse",
                    Values = new ChartValues<double> { },
                }
            };

SeriesCollection[1].Values.AddRange(temporalCv);

I have also tried changing the array to a GearedValue<double> type but it will still throw the issue when I want to add double values to the array.

Is there anyway to convert double[] or another way to add multiple points to the plot together without erasing old points?


Solution

  • welcome to Stack Overflow!

    It looks like someone else has already asked a similar question here: Convert List<double> to LiveCharts.IChartValues

    You will need to convert your double[] to an IEnumerable<double> before you can convert it to an IChartValues collection. This can be easily done with the ToList extension method from System.Linq:

    using System.Linq;
    ...
    var chartValues = temporalCv.ToList().AsChartValues();
    SeriesCollection[1].Values.AddRange(chartValues);
    

    I do not have LiveCharts so I cannot test this directly. You may only need to convert temporalCv to a list and let AddRange do the rest:

    var chartValues = temporalCv.ToList();