Convert list <double> to LiveCharts.IChartValues

I want to plot my double values ​​in a graph using LiveCharts. But I cannot transform my values. I am getting 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,
                },
            };
        }
    }
}

      

+3


source to share


3 answers


Apart from @Pikoh's answer, you can also convert any IEnumerable

to an instance ChartValues

using AsChartValues()

extention:



cartesianChart1.Series = new SeriesCollection
        {
            new LineSeries
            {
                Title = "Series 1",
                Values = yvals.AsChartValues(),
            },
        };

      

+3


source


LiveCharts LineSeries

expects a Values

variable of type in the property ChartValues

. Therefore, in this case, you must change:

var yvals = new List<double>();

      



in

var yvals = new ChartValues<double>();

      

+1


source


cartesianChart1.Series = new SeriesCollection
    {
        new LineSeries
        {
            Title = "Series 1",
            Values = new ChartValues<double>(yvals)
        },
    };

      

0


source







All Articles