kame
kame

Reputation: 21960

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,
                },
            };
        }
    }
}

Upvotes: 4

Views: 8118

Answers (4)

S At
S At

Reputation: 467

Additionally answers

        var temp1 = new List<double> { 10, 12, 8, 9, 11, 5, 14, 8, 3, 18, 15 };
        var temp2 = new List<double> { 12, 10, 18, 19, 21, 15, 4, 10, 13, 11, 22 };

        var tem1 = new ChartValues<double>();
        var tem2 = new ChartValues<double>();

        for (int i = 0; i < temp1.Count; i++)
        {
            tem1.Add(temp1[i]);
            tem2.Add(temp2[i]);
        }

Upvotes: 0

esc
esc

Reputation: 228

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

Upvotes: 0

bto.rdz
bto.rdz

Reputation: 6720

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(),
            },
        };

Upvotes: 8

Pikoh
Pikoh

Reputation: 7703

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>();

Upvotes: 1

Related Questions