将List <double>转换为LiveCharts.IChartValues

时间:2017-03-22 14:45:02

标签: c# charts livecharts

我想用LiveCharts在图表中绘制我的双值。 但我不能转换我的价值观。 我收到错误:

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

这是我的代码(可能不需要):

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 个答案:

答案 0 :(得分:4)

除了@ Pikoh的答案,您还可以使用IEnumerable扩展名将ChartValues转换为AsChartValues()个实例:

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

答案 1 :(得分:1)

LiveCharts LineSeries在其Values属性中需要一个ChartValues类型的变量。所以在这种情况下你应该改变:

var yvals = new List<double>();

成:

var yvals = new ChartValues<double>();

答案 2 :(得分:0)

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