!!!C# グラフ [C#][Visual Studio] {{category グラフ}} *[Microsoft Chart Controls for Microsoft .NET Framework 3.5|http://www.microsoft.com/downloads/details.aspx?familyid=130F7986-BF49-4FE5-9CA8-910AE6EA442C&displaylang=ja] *[Microsoft Chart Controls Add-on for Microsoft Visual Studio 2008|http://www.microsoft.com/downloads/details.aspx?familyid=1D69CE13-E1E5-4315-825C-F14D33A303E9&displaylang=en] *[Microsoft Chart Controls for Microsoft .NET Framework 3.5 Language Pack|http://www.microsoft.com/downloads/details.aspx?FamilyId=581FF4E3-749F-4454-A5E3-DE4C463143BD&displaylang=en] *[Microsoft Chart Controls for .NET Framework Documentation|http://www.microsoft.com/downloads/details.aspx?FamilyID=ee8f6f35-b087-4324-9dba-6dd5e844fd9f&DisplayLang=en] *[Samples Environment for Microsoft Chart Controls|http://code.msdn.microsoft.com/mschart/Release/ProjectReleases.aspx?ReleaseId=1591] !!!Microsoft Chart Controls *http://code.msdn.microsoft.com/mschart !!インストール !チャートコントロールのアセンブリ *[Microsoft Chart Controls for Microsoft .NET Framework 3.5|http://www.microsoft.com/downloads/details.aspx?familyid=130F7986-BF49-4FE5-9CA8-910AE6EA442C&displaylang=ja] *ASP.NET および Windows Forms チャートコントロール を含む新しいアセンブリをインストールします !Visual Studio アドオン *[Microsoft Chart Controls Add-on for Microsoft Visual Studio 2008|http://www.microsoft.com/downloads/details.aspx?familyid=1D69CE13-E1E5-4315-825C-F14D33A303E9&displaylang=en] *Microsoft Chart Controls for .NET Framework 3.5 が事前に導入されていること *Visual Studio のASP.NET、Windows Forms チャートコントロールをツールボックスおよびインテリセンスに統合する {{ref_image chartctrl01.jpg}} !ドキュメント *[Microsoft Chart Controls for .NET Framework Documentation|http://www.microsoft.com/downloads/details.aspx?FamilyID=ee8f6f35-b087-4324-9dba-6dd5e844fd9f&DisplayLang=en] {{ref_image chartctrl02.jpg}} !サンプル *[MSDN Code Gallery|http://code.msdn.microsoft.com/mschart/Release/ProjectReleases.aspx?ReleaseId=1591] *ここから、サンプルソリューションをダウンロードし、ビルドすると、WinFormsChartSamples.exe ができる。 {{ref_image chartctrl05.jpg}} ::WinFormsChartSamples.exe *サンプルの例およびソースコードが確認できるプログラム {{ref_image chartctrl03.jpg}} *ソースの確認 {{ref_image chartctrl04.jpg}} !!グラフの作成 !単純なグラフ {{ref_image chartctrl06.jpg}} using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace MsChartCtrlTest { public partial class Form1 : Form { private Chart chart1 = null; public Form1() { InitializeComponent(); // 初期表示ダミーデータをデータグリッドに設定 Random rnd = new Random(); for (int i = 0; i < 10; i++) { string[] row = new string[] { "axis_label" + i, (rnd.NextDouble() * 100).ToString() }; dataGridView1.Rows.Add(row); } // グラフコントロールを動的に配置 chart1 = new Chart(); ChartArea chartArea = new ChartArea(); this.chart1.ChartAreas.Add(chartArea); this.chart1.Location = new System.Drawing.Point(12, 12); this.chart1.Size = new System.Drawing.Size(312, 228); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.chart1 }); } private void button1_Click(object sender, EventArgs e) { // グラフの表示 this.chart1.Series.Clear(); Series series = new Series(); for (int i = 0; i < dataGridView1.Rows.Count-1; i++) { DataPoint point = series.Points.Add(Convert.ToDouble(dataGridView1.Rows[i].Cells[1].Value)); point.AxisLabel = dataGridView1.Rows[i].Cells[0].Value.ToString(); } this.chart1.Series.Add(series); this.chart1.ResetAutoValues(); } } } !機能追加してみる *グラフタイプ変更(棒、折れ線、円) *3D表示、回転 {{ref_image chartctrl07.jpg}} using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace MsChartCtrlTest { public partial class Form1 : Form { private Chart chart1 = null; public Form1() { InitializeComponent(); // 初期表示ダミーデータをデータグリッドに設定 Random rnd = new Random(); for (int i = 0; i < 10; i++) { string[] row = new string[] { "label" + i, (rnd.NextDouble() * 100).ToString() }; dataGridView1.Rows.Add(row); } // グラフのタイプを選択 comboBox1.Items.Add(SeriesChartType.Bar); comboBox1.Items.Add(SeriesChartType.Line); comboBox1.Items.Add(SeriesChartType.Pie); comboBox1.SelectedItem = SeriesChartType.Bar; // 3Dの場合のY軸回転 trackBar1.Maximum = 180; trackBar1.Minimum = -180; trackBar1.TickFrequency = 10; // グラフコントロールを動的に配置 chart1 = new Chart(); ChartArea chartArea = new ChartArea(); this.chart1.ChartAreas.Add(chartArea); this.chart1.Location = new System.Drawing.Point(12, 12); this.chart1.Size = new System.Drawing.Size(312, 228); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.chart1 }); } private void button1_Click(object sender, EventArgs e) { this.chart1.Series.Clear(); // 3Dスタイルの有効無効 this.chart1.ChartAreas[0].Area3DStyle.Enable3D = checkBox1.Checked; Series series = new Series(); // グラフのタイプを設定 series.ChartType = (SeriesChartType)comboBox1.SelectedItem; for (int i = 0; i < dataGridView1.Rows.Count-1; i++) { DataPoint point = series.Points.Add(Convert.ToDouble(dataGridView1.Rows[i].Cells[1].Value)); point.AxisLabel = dataGridView1.Rows[i].Cells[0].Value.ToString(); } this.chart1.Series.Add(series); this.chart1.ResetAutoValues(); } private void trackBar1_Scroll(object sender, EventArgs e) { if (checkBox1.Checked) { this.chart1.ChartAreas[0].Area3DStyle.Rotation = trackBar1.Value; } } private void Form1_Load(object sender, EventArgs e) { } } }