将列表 <double> 转换为 LiveCharts.IChartValues
Convert List<double> to LiveCharts.IChartValues
我想用 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,
},
};
}
}
}
LiveCharts LineSeries
期望其 Values
属性 类型为 ChartValues
的变量。所以在这种情况下你应该改变:
var yvals = new List<double>();
进入:
var yvals = new ChartValues<double>();
除了@Pikoh 的回答,您还可以使用 AsChartValues()
扩展将任何 IEnumerable
转换为 ChartValues
实例:
cartesianChart1.Series = new SeriesCollection
{
new LineSeries
{
Title = "Series 1",
Values = yvals.AsChartValues(),
},
};
cartesianChart1.Series = new SeriesCollection
{
new LineSeries
{
Title = "Series 1",
Values = new ChartValues<double>(yvals)
},
};
另外回答
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]);
}
我想用 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,
},
};
}
}
}
LiveCharts LineSeries
期望其 Values
属性 类型为 ChartValues
的变量。所以在这种情况下你应该改变:
var yvals = new List<double>();
进入:
var yvals = new ChartValues<double>();
除了@Pikoh 的回答,您还可以使用 AsChartValues()
扩展将任何 IEnumerable
转换为 ChartValues
实例:
cartesianChart1.Series = new SeriesCollection
{
new LineSeries
{
Title = "Series 1",
Values = yvals.AsChartValues(),
},
};
cartesianChart1.Series = new SeriesCollection
{
new LineSeries
{
Title = "Series 1",
Values = new ChartValues<double>(yvals)
},
};
另外回答
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]);
}