简体   繁体   English

C#将Winforms图表绑定到对象列表

[英]c# bind winforms chart to list of objects

I have a list of objects defined like this: 我有这样定义的对象列表:

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

    public int Closed
    {
        get;
        set;
    }

    public int Canceled
    {
        get;
        set;
    }
}

How can I bind a winforms-chart (bar-chart type) to a List<ChartData> ? 如何将winforms-chart(条形图类型)绑定到List<ChartData> I need to have a series for each property in the object (ie, I will have 3 series) and for each point in the chart, I want to see the values for all 3 properties in the object. 我需要为对象中的每个属性创建一个序列(即,我将拥有3个序列),并且对于图表中的每个点,我都希望查看对象中所有3个属性的值。

I managed to programatically add the series (they're visible in the chart), but when I try to set the data source, it crashes: 我设法以编程方式添加了该系列(它们在图表中可见),但是当我尝试设置数据源时,它崩溃了:

        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();

with an System.ArgumentOutOfRangeException , saying that Data points insertion error. Only 1 Y values can be set for this data series. 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. ... ...

Any help/hint? 有帮助/提示吗?

Replace XValueMember with YValueMembers : 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM