バージョン

データ バインディング

このトピックでは、UltraDataPieChart コントロールにデータをバインドする方法を説明します。トピックの最後で、完全なコード例を提供します。

トピックは以下のとおりです。

はじめに

以下の手順は、UltraDataPieChart コントロールをデータ コレクションにバインドする方法を示します。

要件

以下の Infragistics アセンブリをメイン プロジェクトに追加します。

  • Infragistics.Win.DataVisualization.UltraDataChart.dll

  • Infragistics.Win.DataVisualization.Shared.dll

  • Infragistics.Win.Portable.Core.dll

また、次の Infragistics 名前空間を追加します。

C# の場合:

using Infragistics.Win.DataVisualization;
using Infragistics.Win.DataVisualization.Shared;
using Infragistics.Win.Portable.Core;

VB の場合:

Imports Infragistics.Win.DataVisualization
Imports Infragistics.Win.DataVisualization.Shared
Imports Infragistics.Win.Portable.Core

データへのバインディング

UltraDataPieChart には、少なくとも 2 つのプロパティを定義するデータ項目のコレクションが必要です。1 つはラベルとして使用するプロパティ、もう 1 つは各スライスの値として使用する数値プロパティです。データ項目がこれらの最小プロパティのみを定義していると仮定すると、コントロールの DataSource プロパティを設定するだけで、自動的にこれらのプロパティを認識します。

基礎となるデータ項目に 2 つ以上のプロパティがある場合、使用するプロパティを定義する別の方法があります。最初の方法は、LabelMemberPath および ValueMemberPath プロパティを使用し、それぞれラベルと値に使用するプロパティ名を指定することです。

また、チャートの IncludedProperties または ExcludedProperties コレクションを利用することもできます。これらのコレクションは、UltraDataPieChart のプロットに含めるまたは除外するプロパティ名の文字列配列を受け取ります。

コード例

以下のコードは、シンプルな値-ラベルペアを表す DataItem クラスと、ExcludedProperties コレクションを示すために使用する 3 番目の文字列プロパティを作成します。また、これらの DataItem のコレクションを表す Data クラスもあります:

C# の場合:

public class Data : ObservableCollection<DataItem>
{
    public Data()
    {
        this.Add(new FinancialDataPoint { Spending = 20, Budget = 60, Label = "Administration" });
        this.Add(new FinancialDataPoint { Spending = 80, Budget = 40, Label = "Sales" });
        this.Add(new FinancialDataPoint { Spending = 30, Budget = 60, Label = "IT" });
        this.Add(new FinancialDataPoint { Spending = 80, Budget = 40, Label = "Marketing" });
        this.Add(new FinancialDataPoint { Spending = 40, Budget = 60, Label = "Development" });
        this.Add(new FinancialDataPoint { Spending = 60, Budget = 20, Label = "CustomerSupport" });
    }
}

public class DataItem
{
    public string Label { get; set; }
    public double Spending { get; set; }
    public double Budget { get; set; }
}

以下のコードは、UltraDataPieChart を追加し、IncludedPropertiesExcludedProperties、および LabelMemberPathValueMemberPath のコード スニペットとともにデータにバインドする方法を示しています。

C# の場合:

UltraDataPieChart dataPieChart = new UltraDataPieChart()
{
    Dock = DockStyle.Fill,
    DataSource = new Data(),
    ExcludedProperties = new string[] { "Budget" },
    IncludedProperties = new string[] { "Label", "Spending" }
};

this.Controls.Add(dataPieChart);

その他の方法:

UltraDataPieChart dataPieChart = new UltraDataPieChart()
{
    Dock = DockStyle.Fill,
    DataSource = new Data(),
    ValueMemberPath = "Spending",
    LabelMemberPath = "Label"
};

this.Controls.Add(dataPieChart);

関連コンテンツ