c#bind winforms图表到对象列表 [英] c# bind winforms chart to list of objects

查看:214
本文介绍了c#bind winforms图表到对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
    }
}



如何绑定一个winforms-图表类型)到列表< ChartData> ?我需要有一个系列对象中的每个属性(即,我将有3个系列),对于图表中的每个点,我想查看对象中所有3个属性的值。

How can I bind a winforms-chart (bar-chart type) to a 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.

我设法编程添加系列(它们在图表中可见),但是当我尝试设置数据源时,它崩溃:

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

使用 System.ArgumentOutOfRangeException 数据点插入错误。只能为此数据系列设置1个Y值。 ...

任何帮助/提示?

推荐答案

使用替换 XValueMember YValueMembers



Replace XValueMember with 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;

这篇关于c#bind winforms图表到对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆