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,
},
};
}
}
}
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(),
},
};
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>();
cartesianChart1.Series = new SeriesCollection
{
new LineSeries
{
Title = "Series 1",
Values = new ChartValues<double>(yvals)
},
};
来源:https://stackoverflow.com/questions/42954967/convert-listdouble-to-livecharts-ichartvalues