Foursquare のデータをDevExpress Data Grid にデータバインドする。
CData API Driver for ADO.NET は、サードパーティ製コントロールと組み合わせて使える標準的なADO.NET データアクセスコンポーネントを備えています。通常のADO.NET データバインドの手順に沿って進めるだけで、UI コントロールからリアルタイムデータへの双方向アクセスを実現できます。この記事では、CData のコンポーネントを使って DevExpress UI コントロール(Windows Forms およびWeb コントロール) にデータバインドする方法を紹介します。今回は、リアルタイムのデータを可視化するチャートにデータバインドしていきます。
API キー認証の設定
Foursquare Places API は Service Key(Bearer トークン)認証を使用します。Service Key を取得するには、以下のステップで進めます:
- https://foursquare.com/developers/ の Foursquare Developer Console にアクセスします
- 新しいプロジェクトを作成するか、既存のプロジェクトを選択します
- API Keys セクションに移動します
- Places API 用に新しい Service Key を生成します
以下の接続プロパティを設定して接続を確立してください:
- AuthScheme:APIKey に設定します。
- ServiceKey:Developer Console から取得した Foursquare の Service Key に設定します。
- XPlacesApiVersion:(オプション)API バージョンの日付に設定します。デフォルトは 2025-06-17 です。
API キー接続文字列の例
Profile=C:\profiles\Foursquare.apip;AuthScheme=APIKey;ProfileSettings='APIKey=your_personal_access_token';
Windows Forms コントロール
下のコードでは、Foursquare でDevExpress のチャートに追加する方法を説明します。APIDataAdapter はチャートコントロールのSeries プロパティにバインドします。コントロールのDiagram プロパティはx 軸とy 軸をカラム名として定義します。
using (APIConnection connection = new APIConnection(
"Profile=C:\profiles\Foursquare.apip;AuthScheme=APIKey;ProfileSettings='APIKey=your_personal_access_token';")) {
APIDataAdapter dataAdapter = new APIDataAdapter(
"SELECT , FROM Autocomplete WHERE Query = 'abc'", connection);
DataTable table = new DataTable();
dataAdapter.Fill(table);
DevExpress.XtraCharts.Series series = new DevExpress.XtraCharts.Series();
chartControl1.Series.Add(series);
DataTable table = new DataTable();
series.ValueDataMembers.AddRange(new string[] { "" });
series.ArgumentScaleType = DevExpress.XtraCharts.ScaleType.Qualitative;
series.ArgumentDataMember = "";
series.ValueScaleType = DevExpress.XtraCharts.ScaleType.Numerical;
chartControl1.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;
((DevExpress.XtraCharts.SideBySideBarSeriesView)series.View).ColorEach = true;
}
Web コントロール
下のコードではFoursquare でDevExpress Web を操作するための設定方法を説明します。APIDataAdapter はチャートのSeries プロパティにバインドします。Diagram プロパティはx 軸とy 軸をカラム名として定義します。
using DevExpress.XtraCharts;
using (APIConnection connection = new APIConnection(
"Profile=C:\profiles\Foursquare.apip;AuthScheme=APIKey;ProfileSettings='APIKey=your_personal_access_token';"))
{
APIDataAdapter APIDataAdapter1 = new APIDataAdapter("SELECT , FROM Autocomplete WHERE Query = 'abc'", connection);
DataTable table = new DataTable();
APIDataAdapter1.Fill(table);
DevExpress.XtraCharts.Series series = new Series("Series1", ViewType.Bar);
WebChartControl1.Series.Add(series);
DataTable table = new DataTable();
series.ValueDataMembers.AddRange(new string[] { "" });
series.ArgumentScaleType = ScaleType.Qualitative;
series.ArgumentDataMember = "";
series.ValueScaleType = ScaleType.Numerical;
((DevExpress.XtraCharts.SideBySideBarSeriesView)series.View).ColorEach = true;
}