Convert List<double> to LiveCharts.IChartValues

[亡魂溺海] 提交于 2019-12-10 10:08:14

问题


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

回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/42954967/convert-listdouble-to-livecharts-ichartvalues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!