[英]c# bind winforms chart to list of objects
我有这样定义的对象列表:
public class ChartData
{
public int New
{
get;
set;
}
public int Closed
{
get;
set;
}
public int Canceled
{
get;
set;
}
}
如何将winforms-chart(条形图类型)绑定到List<ChartData>
? 我需要为对象中的每个属性创建一个序列(即,我将拥有3个序列),并且对于图表中的每个点,我都希望查看对象中所有3个属性的值。
我设法以编程方式添加了该系列(它们在图表中可见),但是当我尝试设置数据源时,它崩溃了:
List<ChartData> data = new List<ChartData>();
// fill with random int values
chart.DataSource = data;
chart.Series.Add("New").XValueMember = "New";
chart.Series["New"].ChartType = SeriesChartType.Bar;
chart.Series["New"].XValueType = ChartValueType.Int32;
chart.Series["New"].YValueType = ChartValueType.Int32;
chart.Series.Add("Canceled").XValueMember = "Canceled";
chart.Series["Canceled"].ChartType = SeriesChartType.Bar;
chart.Series["Canceled"].XValueType = ChartValueType.Int32;
chart.Series["Canceled"].YValueType = ChartValueType.Int32;
chart.Series.Add("Closed").XValueMember = "Closed";
chart.Series["Closed"].ChartType = SeriesChartType.Bar;
chart.Series["Closed"].XValueType = ChartValueType.Int32;
chart.Series["Closed"].YValueType = ChartValueType.Int32;
chart.DataBind();
System.ArgumentOutOfRangeException
,表示Data points insertion error. Only 1 Y values can be set for this data series.
Data points insertion error. Only 1 Y values can be set for this data series.
...
有帮助/提示吗?
将XValueMember
替换为YValueMembers
:
chart.Series.Add("New").YValueMembers = "New";
chart.Series["New"].ChartType = SeriesChartType.Bar;
chart.Series["New"].XValueType = ChartValueType.Int32;
chart.Series["New"].YValueType = ChartValueType.Int32;
chart.Series.Add("Canceled").YValueMembers = "Canceled";
chart.Series["Canceled"].ChartType = SeriesChartType.Bar;
chart.Series["Canceled"].XValueType = ChartValueType.Int32;
chart.Series["Canceled"].YValueType = ChartValueType.Int32;
chart.Series.Add("Closed").YValueMembers = "Closed";
chart.Series["Closed"].ChartType = SeriesChartType.Bar;
chart.Series["Closed"].XValueType = ChartValueType.Int32;
chart.Series["Closed"].YValueType = ChartValueType.Int32;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.