トップ 差分 一覧 ping ソース 検索 ヘルプ PDF RSS ログイン

C# グラフ



目次



記事一覧

キーワード

C# グラフ

[C#][Visual Studio]
[グラフ]


Microsoft Chart Controls

 インストール

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

Visual Studio アドオン


ドキュメント

サンプル

  • MSDN Code Gallery
  • ここから、サンプルソリューションをダウンロードし、ビルドすると、WinFormsChartSamples.exe ができる。
WinFormsChartSamples.exe
  • サンプルの例およびソースコードが確認できるプログラム
  • ソースの確認

 グラフの作成

単純なグラフ


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表示、回転
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)
        {
        }
    }
}



YAGI Hiroto (piroto@a-net.email.ne.jp)
twitter http://twitter.com/pppiroto

Copyright© 矢木 浩人 All Rights Reserved.