簡體   English   中英

C#將Winforms圖表綁定到對象列表

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM