| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

C Sharp グラフ

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

C# グラフ

C Sharp | Visual Studio |

Microsoft Chart Controls

インストール

チャートコントロールのアセンブリ

Visual Studio アドオン

0261 chartctrl01.jpg

ドキュメント

0262 chartctrl02.jpg

サンプル

  • MSDN Code Gallery
  • ここから、サンプルソリューションをダウンロードし、ビルドすると、WinFormsChartSamples.exe ができる。

0265 chartctrl05.jpg

WinFormsChartSamples.exe
  • サンプルの例およびソースコードが確認できるプログラム

0263 chartctrl03.jpg

  • ソースの確認

0264 chartctrl04.jpg

グラフの作成

単純なグラフ

0266 chartctrl06.jpg

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Windows.Forms.DataVisualization.Charting;
  10.  
  11. namespace MsChartCtrlTest
  12. {
  13. public partial class Form1 : Form
  14. {
  15. private Chart chart1 = null;
  16. public Form1()
  17. {
  18. InitializeComponent();
  19.  
  20. // 初期表示ダミーデータをデータグリッドに設定
  21. Random rnd = new Random();
  22. for (int i = 0; i < 10; i++)
  23. {
  24. string[] row = new string[] {
  25. "axis_label" + i, (rnd.NextDouble() * 100).ToString()
  26. };
  27. dataGridView1.Rows.Add(row);
  28. }
  29.  
  30. // グラフコントロールを動的に配置
  31. chart1 = new Chart();
  32. ChartArea chartArea = new ChartArea();
  33. this.chart1.ChartAreas.Add(chartArea);
  34. this.chart1.Location = new System.Drawing.Point(12, 12);
  35. this.chart1.Size = new System.Drawing.Size(312, 228);
  36. this.Controls.AddRange(new System.Windows.Forms.Control[] { this.chart1 });
  37. }
  38.  
  39. private void button1_Click(object sender, EventArgs e)
  40. {
  41. // グラフの表示
  42. this.chart1.Series.Clear();
  43.  
  44. Series series = new Series();
  45. for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
  46. {
  47. DataPoint point = series.Points.Add(Convert.ToDouble(dataGridView1.Rows[i].Cells[1].Value));
  48. point.AxisLabel = dataGridView1.Rows[i].Cells[0].Value.ToString();
  49. }
  50. this.chart1.Series.Add(series);
  51. this.chart1.ResetAutoValues();
  52. }
  53. }
  54. }

機能追加してみる

  • グラフタイプ変更(棒、折れ線、円)
  • 3D表示、回転

0267 chartctrl07.jpg

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Windows.Forms.DataVisualization.Charting;
  10.  
  11. namespace MsChartCtrlTest
  12. {
  13. public partial class Form1 : Form
  14. {
  15. private Chart chart1 = null;
  16. public Form1()
  17. {
  18. InitializeComponent();
  19.  
  20. // 初期表示ダミーデータをデータグリッドに設定
  21. Random rnd = new Random();
  22. for (int i = 0; i < 10; i++)
  23. {
  24. string[] row = new string[] {
  25. "label" + i, (rnd.NextDouble() * 100).ToString()
  26. };
  27. dataGridView1.Rows.Add(row);
  28. }
  29.  
  30. // グラフのタイプを選択
  31. comboBox1.Items.Add(SeriesChartType.Bar);
  32. comboBox1.Items.Add(SeriesChartType.Line);
  33. comboBox1.Items.Add(SeriesChartType.Pie);
  34. comboBox1.SelectedItem = SeriesChartType.Bar;
  35.  
  36. // 3Dの場合のY軸回転
  37. trackBar1.Maximum = 180;
  38. trackBar1.Minimum = -180;
  39. trackBar1.TickFrequency = 10;
  40.  
  41. // グラフコントロールを動的に配置
  42. chart1 = new Chart();
  43. ChartArea chartArea = new ChartArea();
  44. this.chart1.ChartAreas.Add(chartArea);
  45. this.chart1.Location = new System.Drawing.Point(12, 12);
  46. this.chart1.Size = new System.Drawing.Size(312, 228);
  47. this.Controls.AddRange(new System.Windows.Forms.Control[] { this.chart1 });
  48. }
  49.  
  50. private void button1_Click(object sender, EventArgs e)
  51. {
  52. this.chart1.Series.Clear();
  53.  
  54. // 3Dスタイルの有効無効
  55. this.chart1.ChartAreas[0].Area3DStyle.Enable3D = checkBox1.Checked;
  56. Series series = new Series();
  57.  
  58. // グラフのタイプを設定
  59. series.ChartType = (SeriesChartType)comboBox1.SelectedItem;
  60. for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
  61. {
  62. DataPoint point = series.Points.Add(Convert.ToDouble(dataGridView1.Rows[i].Cells[1].Value));
  63. point.AxisLabel = dataGridView1.Rows[i].Cells[0].Value.ToString();
  64. }
  65. this.chart1.Series.Add(series);
  66. this.chart1.ResetAutoValues();
  67. }
  68.  
  69. private void trackBar1_Scroll(object sender, EventArgs e)
  70. {
  71. if (checkBox1.Checked)
  72. {
  73. this.chart1.ChartAreas[0].Area3DStyle.Rotation = trackBar1.Value;
  74. }
  75. }
  76. private void Form1_Load(object sender, EventArgs e)
  77. {
  78. }
  79. }
  80. }