Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert List<double> to LiveCharts.IChartValues

I want to plot my double values in a graph with LiveCharts. But I can't convert my values. I get the error:

Cannot convert source type 'System.Collections.Generic.List<double>
to target type 'LiveCharts.IChartValues'

This is my code (maybe not needed):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media;
using LiveCharts;
using LiveCharts.Wpf;
using Brushes = System.Windows.Media.Brushes;

namespace LiveAnalysis
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            var xvals = new List<DateTime>();
            var yvals = new List<double>();

            using (var db = new SystemtestFunctions.TestingContext())
            {
                var lttResults = db.LttResults;
                var par1 = 0.0;
                foreach (var data in lttResults)
                {
                    if (data.GatewayEventType == 41)
                        par1 = data.FloatValue;
                    if (data.GatewayEventType != 42) continue;

                    var par2 = data.FloatValue;
                    var diff = Math.Round(par1 - par2, 3);
                    yvals.Add(diff);
                    xvals.Add(data.DateTime);
                }
            }

            cartesianChart1.Series = new SeriesCollection
            {
                new LineSeries
                {
                    Title = "Series 1",
                    Values = yvals,
                },
            };
        }
    }
}
like image 614
kame Avatar asked Jan 04 '23 07:01

kame


2 Answers

Additionally to @Pikoh's answer, you can also convert any IEnumerable to a ChartValues instance, using AsChartValues() extention:

cartesianChart1.Series = new SeriesCollection
        {
            new LineSeries
            {
                Title = "Series 1",
                Values = yvals.AsChartValues(),
            },
        };
like image 159
bto.rdz Avatar answered Jan 12 '23 06:01

bto.rdz


LiveCharts LineSeries expects in its Values property a variable of type ChartValues. So in this case you should change:

var yvals = new List<double>();

into:

var yvals = new ChartValues<double>();
like image 35
5 revs, 4 users 77%SuperG Avatar answered Jan 12 '23 05:01

5 revs, 4 users 77%SuperG