バージョン

データ バインディング

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

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

概要

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

プレビュー

PieChartWithLegend Win.png

図 1: コード例で実装された UltraPieChart

要件

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

  • Infragistics.Win.DataVisualization.UltraDataChart.dll

  • Infragistics.Win.DataVisualization.Shared.dll

  • Infragistics.Win.Portable.Core.dll

以下の名前空間を追加します。

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

概要

  1. データ モデルの定義

  2. Pie Chart コントロールのインスタンスの追加

  3. (オプション) 結果の検証

手順

  1. データ モデルを定義します

データをモデル化するためのクラスを作成します。以下のコードは、シンプルな値とラベルのペアを表す DataItem クラス、およびそれらのペアのコレクションを表す Data クラスを作成します。

C# の場合:

    public class DataItem
    {
        public string Label { get; set; }
        public double Value { get; set; }
    }
    public class Data : ObservableCollection<DataItem>
    {
        public Data()
        {
            Add(new DataItem { Label = "Item 1", Value = 5 });
            Add(new DataItem { Label = "Item 2", Value = 6 });
            Add(new DataItem { Label = "Item 3", Value = 3 });
            Add(new DataItem { Label = "Item 4", Value = 7 });
            Add(new DataItem { Label = "Item 5", Value = 5 });
        }
    }

Visual Basic の場合:

Public Class DataItem
   Public Property Label() As String
      Get
         Return _Label
       End Get
       Set
          _Label = Value
       End Set
    End Property
    Private _Label As String
    Public Property Value() As Double
       Get
          Return _Value
       End Get
       Set
          _Value = Value
       End Set
    End Property
    Private _Value As Double
End Class
Public Class Data
   Inherits ObservableCollection(Of DataItem)
   Public Sub New()
      Add(New DataItem() With { .Label = "Item 1", .Value = 5 })
      Add(New DataItem() With { .Label = "Item 2", .Value = 6 })
      Add(New DataItem() With { .Label = "Item 3", .Value = 3 })
      Add(New DataItem() With { .Label = "Item 4", .Value = 7 })
      Add(New DataItem() With { .Label = "Item 5", .Value = 5 })
   End Sub
 End Class
  1. UltraPieChart コントロールのインスタンスを追加します。

フォームにデータ コレクションのインスタンス、凡例インスタンス、および Pie Chart のインスタンスを追加します:

C# の場合:

UltraPieChart pieChart = new UltraPieChart();
this.Controls.Add(pieChart);
pieChart.Dock = DockStyle.Fill;
pieChart.LabelMemberPath = "Label";
pieChart.ValueMemberPath = "Value";
pieChart.DataSource = new Data();
UltraItemLegend legend = new UltraItemLegend();
this.Controls.Add(legend);
legend.Dock = DockStyle.Right;
legend.Height = 500;
pieChart.Legend = legend;
legend.BringToFront();

VB の場合:

Dim pieChart As New UltraPieChart ()
Me.Controls.Add(pieChart)
pieChart.Dock = DockStyle.Fill
pieChart.LabelMemberPath = "Label"
pieChart.ValueMemberPath = "Value"
pieChart.DataSource = New Data ()
Dim legend As New UltraItemLegend()
legend.Dock = DockStyle.Right
legend.Height = 500
pieChart.Legend = legend
legend.BringToFront()
  1. (オプション) 結果を確認します

結果を検証するために、アプリケーションを実行します。Pie Chart コントロールのデータ コレクションへのバインドが成功すれば、結果のチャートは上記の図 1 に示すようなものになります。