以下のコードは、フォームに追加した C1Chart コントロールを含む新規プロジェクトに追加すると、データ連結を使用して完全に機能するプログラムを示します。Form_Load イベントを作成して処理する必要もあることに注意してください。このサンプルで使用するデータベースは、製品に付属のNorthWind データベースです。ファイルパスが各自のインストールに一致しない場合、実行する前にデータベースを変更する必要があります。
以下のサンプルは、データベースと C1Chart をコードだけで処理します。ただし、設計時に Microsoft .NET IDE とそのプロパティページを使用して設定全体を管理できます。TableMapping を含む2つの DataAdapters を使用すると、単一 DataSet を DataSource として使用し、同一テーブルの2つのデータセットをグラフ化できます。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Private Sub BindMultipleSeriesViewsAndChartSetup(ByVal chart As C1.Win.C1Chart.C1Chart)
' 次のオブジェクトは System.Data.OleDb 名前空間にあります。
Dim connect As OleDbConnection = New OleDbConnection()
Dim adapt1 As OleDbDataAdapter = New OleDbDataAdapter()
Dim adapt2 As OleDbDataAdapter = New OleDbDataAdapter()
Dim select1 As OleDbCommand = New OleDbCommand()
Dim select2 As OleDbCommand = New OleDbCommand()
' サンプルデータベースへの接続を設定します。
connect.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" + _
"User ID=Admin;" + _
"Data Source=C:\Users\Documents\ComponentOne Samples\Common\NWind.mdb;" + _
"Jet OLEDB:Engine Type=5;"
' [Sales Totals by Amount] 内の Save-A-Lot エントリを選択します。
select1.CommandText = _
"SELECT SaleAmount, ShippedDate, CompanyName " + _
"FROM [Sales Totals by Amount] " + _
"WHERE (CompanyName = 'Save-a-lot Markets') " + _
"ORDER BY ShippedDate"
select1.Connection = connect
' [Sales Totals by Amount] 内の Quick-Stop エントリを選択します。
select2.CommandText = _
"SELECT SaleAmount, ShippedDate, CompanyName " + _
"FROM [Sales Totals by Amount] " + _
"WHERE (CompanyName = 'QUICK-Stop') " + _
"ORDER BY ShippedDate"
select2.Connection = connect
' Mapping オブジェクトに System.Data.Common 名前空間を使用します。
' TableMapping を使用し、同一テーブルに対して
' 複数のビューを同じDataSet に表示します。
' アダプタ、adapt1 を設定します。
adapt1.SelectCommand = select1
Dim ColumnMaps_SaveALot As DataColumnMapping() = _
{ _
New DataColumnMapping("SaleAmount", "SaleAmount"), _
New DataColumnMapping("CompanyName", "CompanyName"), _
New DataColumnMapping("ShippedDate", "ShippedDate") _
}
adapt1.TableMappings.Add(New DataTableMapping("Table", "SaveALot", _
ColumnMaps_SaveALot))
' アダプタ、adapt2 を設定します。
adapt2.SelectCommand = select2
Dim ColumnMaps_QuickStop As DataColumnMapping() = _
{ _
New DataColumnMapping("SaleAmount", "SaleAmount"), _
New DataColumnMapping("CompanyName", "CompanyName"), _
New DataColumnMapping("ShippedDate", "ShippedDate") _
}
adapt2.TableMappings.Add(New DataTableMapping("Table", "QuickStop", _
ColumnMaps_QuickStop))
' データセットを作成して、すべてのアダプタからのデータを入力します。
Dim ds As DataSet = New DataSet()
adapt1.Fill(ds)
adapt2.Fill(ds)
' グラフを設定し、DataSource、DataFields、および各プロパティを割り当てます。
chart.Dock = DockStyle.Fill
chart.DataSource = ds
Dim sc As ChartDataSeriesCollection = chart.ChartGroups(0).ChartData.SeriesList
sc.RemoveAll()
' Save-A-Lot データセットを追加します。
Dim s As ChartDataSeries = sc.AddNewSeries()
s.Label = "Save-A-Lot"
s.X.DataField = "SaveALot.ShippedDate"
s.Y.DataField = "SaveALot.SaleAmount"
' Quick-Stop データセットを追加します。
s = sc.AddNewSeries()
s.Label = "Quick-Stop"
s.X.DataField = "QuickStop.ShippedDate"
s.Y.DataField = "QuickStop.SaleAmount"
' 軸と凡例を設定します。
chart.ChartArea.AxisX.AnnoFormat = FormatEnum.DateShort
chart.ChartArea.AxisY.AnnoFormat = FormatEnum.NumericCurrency
chart.ChartArea.AxisY.Min = 0
' 棒グラフに切り替えます。
chart.ChartGroups[0].ChartType = Chart2DTypeEnum.Bar
chart.ChartGroups[0].ShowOutline = false
' 凡例の配置と向きを設定し、表示させます。
chart.Legend.Compass = CompassEnum.North
chart.Legend.Orientation = LegendOrientationEnum.Horizontal
chart.Legend.Visible = true
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
C1Chart1.Location = New Point(0)
C1Chart1.Size = Me.ClientSize
BindMultipleSeriesViewsAndChartSetup(C1Chart1)
End Sub
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
private void BindMultipleSeriesViewsAndChartSetup(C1.Win.C1Chart.C1Chart chart)
{
// 次のオブジェクトは System.Data.OleDb 名前空間にあります。
OleDbConnection connect = new OleDbConnection();
OleDbDataAdapter adapt1 = new OleDbDataAdapter();
OleDbDataAdapter adapt2 = new OleDbDataAdapter();
OleDbCommand select1 = new OleDbCommand();
OleDbCommand select2 = new OleDbCommand();
// サンプルデータベースへの接続を設定します。
connect.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"User ID=Admin;" +
"Data Source=C:\Users\Documents\ComponentOne Samples\Common\NWind.mdb;" + "Jet OLEDB:Engine Type=5;";
// [Sales Totals by Amount] 内の Save-A-Lot エントリを選択します。
select1.CommandText =
"SELECT SaleAmount, ShippedDate, CompanyName " +
"FROM [Sales Totals by Amount] " +
"WHERE (CompanyName = \'Save-a-lot Markets\') " +
"ORDER BY ShippedDate";
select1.Connection = connect;
// [Sales Totals by Amount] 内の Quick-Stop エントリを選択します。
select2.CommandText =
"SELECT SaleAmount, ShippedDate, CompanyName " +
"FROM [Sales Totals by Amount] " +
"WHERE (CompanyName = \'QUICK-Stop\') " +
"ORDER BY ShippedDate";
select2.Connection = connect;
// Mapping オブジェクトに System.Data.Common 名前空間を使用します。
// TableMapping を使用し、同一テーブルに対して
// 複数のビューを同じDataSet に表示します。
// アダプタ、adapt1 を設定します。
adapt1.SelectCommand = select1;
DataColumnMapping [] ColumnMaps_SaveALot =
{
new DataColumnMapping("SaleAmount", "SaleAmount"),
new DataColumnMapping("CompanyName", "CompanyName"),
new DataColumnMapping("ShippedDate", "ShippedDate")
};
adapt1.TableMappings.Add(new DataTableMapping("Table", "SaveALot", ColumnMaps_SaveALot));
// アダプタ、adapt2 を設定します。
adapt2.SelectCommand = select2;
DataColumnMapping [] ColumnMaps_QuickStop =
{
new DataColumnMapping("SaleAmount", "SaleAmount"),
new DataColumnMapping("CompanyName", "CompanyName"),
new DataColumnMapping("ShippedDate", "ShippedDate")
};
adapt2.TableMappings.Add(new DataTableMapping("Table", "QuickStop", ColumnMaps_QuickStop));
// データセットを作成して、すべてのアダプタからのデータを入力します。
DataSet ds = new DataSet();
adapt1.Fill(ds);
adapt2.Fill(ds);
// グラフを設定し、DataSource、DataFields、および各プロパティを割り当てます。
chart.Dock = DockStyle.Fill;
chart.DataSource = ds;
ChartDataSeriesCollection sc = chart.ChartGroups[0].ChartData.SeriesList;
sc.RemoveAll();
// Save-A-Lot データセットを追加します。
ChartDataSeries s = sc.AddNewSeries();
s.Label = "Save-A-Lot";
s.X.DataField = "SaveALot.ShippedDate";
s.Y.DataField = "SaveALot.SaleAmount";
// Quick-Stop データセットを追加します。
s = sc.AddNewSeries();
s.Label = "Quick-Stop";
s.X.DataField = "QuickStop.ShippedDate";
s.Y.DataField = "QuickStop.SaleAmount";
// 軸と凡例を設定します。
chart.ChartArea.AxisX.AnnoFormat = FormatEnum.DateShort;
chart.ChartArea.AxisY.AnnoFormat = FormatEnum.NumericCurrency;
chart.ChartArea.AxisY.Min = 0;
// 棒グラフに切り替えます。
chart.ChartGroups[0].ChartType = Chart2DTypeEnum.Bar;
chart.ChartGroups[0].ShowOutline = false;
// 凡例の配置と向きを設定し、表示させます。
chart.Legend.Compass = CompassEnum.North;
chart.Legend.Orientation = LegendOrientationEnum.Horizontal;
chart.Legend.Visible = true; chart.Legend.Visible = true;
}
private void Form1_Load(object sender, System.EventArgs e)
{
c1Chart1.Location = new Point(0);
c1Chart1.Size = this.ClientSize;
BindMultipleSeriesViewsAndChartSetup(c1Chart1);
}
|
|