Chart Controlsにデータバインディングしてみる例
.NET FrameworkのChart Controlsにデータバインディングしてみることにした.
なかなか難しかった.
と思ったら,データの列だけであれば,次のようにすればよいらしい.
DataTableを使用すればいちいちクラスを作らなくてもデータの列を保管できる.
そして,これをそのままバインドすればよい.
using System;
using System.Data;
using System.Windows.Forms;
namespace chartTEST
{
    public partial class Form1 : Form
    {
        private DataTable table = new DataTable("Table");
        private Timer timer = new Timer();
        private Int64 count = 0;
        public Form1()
        {
            InitializeComponent();
            
            // カラム名の追加
            table.Columns.Add("DateTime", Type.GetType("System.DateTime"));
            table.Columns.Add("Value", Type.GetType("System.Double"));
            for (int i = 0; i < 1; i++)
            {
                DataRow row = table.NewRow();
                row[0] = DateTime.Now;
                row[1] = 0.001;
                table.Rows.Add(row);
            }
            chart1.Series[0].XValueMember = "DateTime";
            chart1.Series[0].YValueMembers = "Value";
            this.chart1.DataSource = table;
            this.chart1.DataBind();
            timer.Interval = 1000;
            timer.Tick += timer1_Tick;
            timer.Start();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            count++;
            DataRow row = table.NewRow();
            row[0] = DateTime.Now;
            row[1] = 1.0 * Math.Sin(count * 0.1) + 0.001;
            table.Rows.Add(row);
            this.chart1.DataBind();
        }
    }
}

 
        
