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

时间:2015-11-09 11:35:28

标签: c# winforms list charts bind

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

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. ...

任何帮助/提示?

1 个答案:

答案 0 :(得分:3)

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;

{{1}}

#659