gpt4 book ai didi

c# 将 winforms 图表绑定(bind)到对象列表

转载 作者:行者123 更新时间:2023-11-30 21:53:53 25 4
gpt4 key购买 nike

我有一个这样定义的对象列表:

public class ChartData
{
public int New
{
get;
set;
}

public int Closed
{
get;
set;
}

public int Canceled
{
get;
set;
}
}

如何将 winforms-chart(条形图类型)绑定(bind)到 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. ...

任何帮助/提示?

最佳答案

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;

enter image description here

关于c# 将 winforms 图表绑定(bind)到对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33608235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com