是否有可能从一个普通的列表中设置图表数据源? [英] Is it possible to set a chart datasource from a generic list?
问题描述
我有一个包含数据的2列的LINQ查询创建的列表。
VAR的结果= root.Descendants()元素(传感)
。凡(EL => (字符串)el.Attribute(名)==传感器1)
.Elements(EVT)
。选择(EL = gt;新建{T1 = el.Attribute(时间 ).value的,
V1 = el.Attribute(VAL)。值})
.ToList()
我试图用图表控件数据源使用该列表,但是当我调用bind方法我收到此错误:
<块引用>
System.ArgumentException了未处理的HResult = -2147024809结果
消息=系列的数据点不支持类型为
<值;> f__AnonymousType0`2 [System.Double, System.Decimal]仅
这些类型的值可用于:双人间,小数,单,的int,long,UINT,
ULONG,字符串,日期时间,短,USHORT
块引用>
//结果= root.Descendants()定义为VAR结果泛型列表
chart1.DataSource =结果;
chart1.DataBind(); //这是导致异常线。
问候。
是的,它是可能的,而不是由列表绑定到图
本身。
有几种完全不同的方法来做图表数据绑定,所有套不同的利弊。
我使用的 Series.Points.DataBindXY
或的 Series.Points.DataBind
方法。
下面是一个例子:
//创建一个列表测试数据:
名单,LT;的PointF>点=新的List<&的PointF GT;();
的for(int i = 0; I< 100;我++)points.Add(新的PointF(I,1F * I / 2F * R.Next(8)));
现在从它创建一个通用的列表:
变种人= points.Select(X =>新建{T1 = XX,V1 = XY})了ToList()。
现在这个作品:
someSeries.Points.DataBindXY(人,T1,人的V1);
或也是这样的:
someSeries.DataBind(人,T1,V1,);
在你的情况,你会写,也许这样的:
chart1.Series [0] .Points.DataBind(因此,T1,V1);
请注意,一个图
通常只可以显示值对,而在 DGV
可以创建为多列作为数据源
了。因此,图
需要找到x和y的值。
一点帮助
I have a list created from a linq query that contains 2 columns of data.
var result = root.Descendants().Elements("sensor")
.Where(el => (string)el.Attribute("name") == "Sensor1")
.Elements("evt")
.Select(el => new { t1 = el.Attribute("time").Value,
v1 = el.Attribute("val").Value })
.ToList()
I'm trying to use the chart control datasource to use that list, but when I call the bind method I receive this error:
System.ArgumentException was unhandled HResult=-2147024809
Message=Series data points do not support values of type <>f__AnonymousType0`2[System.Double,System.Decimal] only values of these types can be used: Double, Decimal, Single, int, long, uint, ulong, String, DateTime, short, ushort.
//result is a generic list defined as var result = root.Descendants()
chart1.DataSource = result;
chart1.DataBind(); // This is line that causes the exception.
Regards.
Yes it is possible, but not by binding the list to the Chart
itself.
There are several quite different methods to do Chart Databinding, all with different sets of pros and cons..
I suggest using the Series.Points.DataBindXY
or Series.Points.DataBind
methods.
Here is an example:
// create a list with test data:
List<PointF> points = new List<PointF>();
for (int i = 0; i < 100; i++) points.Add(new PointF(i, 1f * i / 2f * R.Next(8)));
Now create a generic list from it:
var al = points.Select(x => new { t1 = x.X, v1 = x.Y }).ToList();
Now this works:
someSeries.Points.DataBindXY(al, "t1", al, "v1");
or also this:
someSeries.DataBind(al, "t1", "v1", "" );
In your case you would write maybe this:
chart1.Series[0].Points.DataBind(result, "t1", "v1");
Note that a Chart
typically can only display pairs of values whereas a DGV
can create as many columns as the DataSource
has. So Chart
needs a little help in finding the x- and y-values..
这篇关于是否有可能从一个普通的列表中设置图表数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!