Visual C# でグラフを描いてみました

ダイエットをしているのでそれの記録管理をします

>>> 2017/9/8 <<<   pakuさんからの要望より、他のソースコードも掲載しました。

グラフはChartツールを使って描きます。

今回は体重・体脂肪・BMI・骨格筋全てと体脂肪と骨格筋だけの場合との2つを切り替えて見られるようにしました。

また検索機関も1ヶ月単位あるいはすべての期間で選べるようにしました。

 

これでダイエットを続ける励みになればいいんだけど・・・

 

メニュー画面(Form1)の仕様

メニューを切り替えます。

 


Form1.cs



Form1.cs


Form1.cs


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;

namespace ダイエット記録_01
{
    public partial class Form1 : Form
    {
        #region "イニシャライズ"
        public Form1()
        {
            InitializeComponent();
        }
        #endregion

        #region "フォームロード時の処理"
        private void Form1_Load(object sender, EventArgs e)
        {
            //今日の日付を画面に表示する
            DateTime dtToday = DateTime.Today;
            label2.Text = dtToday.ToString("yyyy/MM/dd(ddd)");
        }
        #endregion

        #region "ボタン1クリック フォーム2 データ入力画面表示"
        private void button1_Click(object sender, EventArgs e)
        {
            //Form2クラスのインスタンスを作成する
            Form2 f2 = new Form2();
            //Form2を表示する
            //ここではモーダルダイアログボックスとして表示する
            //オーナーウィンドウにthisを指定する
            this.Hide();  
            f2.ShowDialog(this);
            //フォームが必要なくなったところで、Disposeを呼び出す
            f2.Dispose();
            Show();
        }
        #endregion

        #region "ボタン2 クリック フォーム3 グラフ表示画面表示"
        private void button2_Click(object sender, EventArgs e)
        {
            //Form3クラスのインスタンスを作成する
            Form3 f3 = new Form3();
            //Form3を表示する
            //ここではモーダルダイアログボックスとして表示する
            //オーナーウィンドウにthisを指定する
            this.Hide();
            f3.ShowDialog(this);
            //フォームが必要なくなったところで、Disposeを呼び出す
            f3.Dispose();
            this.Show();
        }
        #endregion

        #region "ボタン3クリック 終了"
        private void button3_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
        #endregion

    }
}







 

 

データ入力画面(Form2)の仕様

DataGridViewにデータを表示し、追加変更をします。


Form2.cs



Form2.cs


Form2.cs


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;

namespace ダイエット記録_01
{
    public partial class Form2 : Form
    {
        #region "イニシャライズ"
        public Form2()
        {
            InitializeComponent();
        }
        #endregion

        #region "フォームロード時の処理"
        private void Form2_Load(object sender, EventArgs e)
        {
           //今日の日付を画面に表示する
            DateTime dtToday = DateTime.Today;
            label2.Text = dtToday.ToString("yyyy/MM/dd(ddd)");

            //値の設定
            comboBox1.Items.Add("1ヶ月前から");
            comboBox1.Items.Add("2ヶ月前から");
            comboBox1.Items.Add("3ヶ月前から");
            comboBox1.Items.Add("全ての期間");
            comboBox1.SelectedIndex = 0;
            //データベース内容を編集
            EditDB();
        }
        #endregion

        #region "ボタン2クリック 終了"
        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
        #endregion

        #region "ボタン1クリック データチェック & データベースへの登録"
        private void button1_Click(object sender, EventArgs e)
        {
            EditDB();
        }
        #endregion

        #region "データベース内容を編集"
        private void EditDB()
        {
            DateTime dtf;
            string sel =  comboBox1.Text;
            if (sel == "全ての期間")
            {
                // TODO: このコード行はデータを 
                //'dietDataSet.DietTable' テーブルに読み込みます。
                //必要に応じて移動、または削除をしてください。
                this.ダイエットテーブルTableAdapter.Fill
                   (this.テストデータベースDataSet.ダイエットテーブル);
            }
            else
            {
                if (sel == "1ヶ月前から")
                {
                    //今日の日付から1ヶ月前を計算する
                    dtf = DateTime.Today;
                    dtf = dtf.AddMonths(-1);
                }
                else if (sel == "2ヶ月前から")
                {
                    //今日の日付から2ヶ月前を計算する
                    dtf = DateTime.Today;
                    dtf = dtf.AddMonths(-2);
                }
                else
                {
                    //今日の日付から3ヶ月前を計算する
                    dtf = DateTime.Today;
                    dtf = dtf.AddMonths(-3);
                }

                // TODO: このコード行はデータを
                //'dietDataSet.DietTable' テーブルに読み込みます。
                //必要に応じて移動、または削除をしてください。
                this.ダイエットテーブルTableAdapter.FillByDate
                   (this.テストデータベースDataSet.ダイエットテーブル, (dtf));
            }
        }
        #endregion

        #region "終了ボタンクリック"
        private void button1_Click_1(object sender, EventArgs e)
        {
            Close();
        }
        #endregion
        
        #region "変更ボタンクリック"
        private void button2_Click_1(object sender, EventArgs e)
        {
            this.ダイエットテーブルBindingSource.EndEdit();

            this.ダイエットテーブルTableAdapter.Update
                      (this.テストデータベースDataSet.ダイエットテーブル);

            EditDB();
        }
        #endregion

        #region "コンボボックス変更時の処理"
        private void comboBox1_SelectedIndexChanged
               (object sender, EventArgs e)
        {
            EditDB();
        }
        #endregion
    }
}








 

 

グラフ表示画面(Form3)の仕様

日付表示はラベルにしておいてプログラムで今日の日付を編集する。

グラフ領域はチャートを使用し線グラフで表示する。

検索期間はコンボボックスにして「1か月前から」「2か月前から」「3か月前から」「すべての期間」から選択できるようにする。初期値は「すべての期間」としこの値が変更された場合にはその期間に合わせたグラフを再表示する。

グラフの系列はコンボボックスで全てまたは体脂肪と骨格筋の2種類から選択できるようにする。初期値は「すべてのグラフ」としこの値が変更された場合にはその種類に合わせたグラフを再表示する。

体脂肪と骨格筋はそれぞれ体脂肪率×体重と骨格筋率×体重で計算する。

 

グラフのY軸の範囲はすべての系列の場合最大値が100,最小値が0とし、体脂肪と骨格筋だけの場合は最大値が30,最小値が10とする。

 


Form3.cs



Form3.cs


Form3.cs


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 ダイエット記録_01
{
    public partial class Form3 : Form
    {
        #region "イニシャライズ"
        public Form3()
        {
            InitializeComponent();
        }
        #endregion

        #region "フォームロード時の処理"
        private void Form3_Load(object sender, EventArgs e)
        {
            //今日の日付を画面に表示する
            DateTime dtToday = DateTime.Today;
            label2.Text = dtToday.ToString("yyyy/MM/dd(ddd)");

            //画面タイトルの表示
            this.label1.Text = "ダイエット記録のグラフ";

            //コンボボックス1値の設定
            comboBox1.Items.Add("1ヶ月前から");
            comboBox1.Items.Add("2ヶ月前から");
            comboBox1.Items.Add("3ヶ月前から");
            comboBox1.Items.Add("全ての期間");
            comboBox1.SelectedIndex = 3;
            //コンボボックス2値の設定
            comboBox2.Items.Add("全てのグラフ");
            comboBox2.Items.Add("体脂肪と骨格筋");
            comboBox2.SelectedIndex = 0;

            // グラフの表示
            DispChart();
        }
        #endregion

        #region "グラフ表示関数"
        public void DispChart()
        {
            //変数定義
            double f;
            DateTime dtf;

            //ChartArea(グラフ領域)
            ChartArea area = new ChartArea("area1");

            //Title(グラフのタイトル)
            Title title = new Title("title1");

            //ChartAreaにTitleを紐付ける
            title.DockedToChartArea = "area1";

            //Series(グラフに表示されるひとつの系列)
            Series series1 = new Series();
            Series series2 = new Series();
            Series series3 = new Series();
            Series series4 = new Series();

            //デフォルトで追加されているSeriesとLegendの初期化
            chart1.Series.Clear();
            chart1.Legends.Clear();

            //Seriesの種類を折れ線グラフに設定
            if (comboBox2.Text == "全てのグラフ")
            {
                series1.ChartType = SeriesChartType.Line;
                series2.ChartType = SeriesChartType.Line;
            }
            series3.ChartType = SeriesChartType.Line;
            series4.ChartType = SeriesChartType.Line;

            //折れ線の太さを設定
            if (comboBox2.Text == "全てのグラフ")
            {
                series1.BorderWidth = 3;
                series2.BorderWidth = 3;
            }
            series3.BorderWidth = 3;
            series4.BorderWidth = 3;

            //コンボボックス1の検索期間を判定
            string sel =  comboBox1.Text;
            if (sel == "全ての期間")
            {
                dtf = new DateTime(2001, 1, 1);
            }
            else
            {
                if (sel == "1ヶ月前から")
                {
                    //今日の日付から1ヶ月前を計算する
                    dtf = DateTime.Today;
                    dtf = dtf.AddMonths(-1);
                }
                else if (sel == "2ヶ月前から")
                {
                    //今日の日付から2ヶ月前を計算する
                    dtf = DateTime.Today;
                    dtf = dtf.AddMonths(-2);
                }
                else
                {
                    //今日の日付から3ヶ月前を計算する
                    dtf = DateTime.Today;
                    dtf = dtf.AddMonths(-3);
                }
            }

            //データベースを検索しデータをリストに読み込む
            var adapter = new テストデータベースDataSetTableAdapters
                .ダイエットテーブルTableAdapter();
            var data = adapter.GetDataByDate(dtf);

            //グラフのデータを追加(リストからデータのある箇所だけ)
            foreach (var row in data)
            {
                if (comboBox2.Text == "全てのグラフ")
                {
                    series1.Points.AddXY(row.日付, row.体重);

                    series2.Points.AddXY(row.日付, row.BMI);
                }
                f = (row.体重 * row.骨格筋率) / 100;
                series3.Points.AddXY(row.日付, f);

                f = (row.体重 * row.体脂肪率) / 100;
                series4.Points.AddXY(row.日付, f);
            }

            //Y軸目盛り設定
            if (comboBox2.Text == "全てのグラフ")
            {
                chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
                chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 100;
            }
            else
            {
                chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 10;
                chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 30;
            }

            //グラフタイトルを指定
            area.Name = "ダイエット記録のグラフ";

            //凡例の作成と位置情報の指定
            Legend leg = new Legend();
            leg.DockedToChartArea = "ChartArea1";
            leg.Alignment = StringAlignment.Near;

            //凡例に表示される文字列を指定
            if (comboBox2.Text == "全てのグラフ")
            {
                series1.Name = "体重";
                series2.Name = "BMI";
            }
            series3.Name = "骨格筋";
            series4.Name = "体脂肪";

            //軸ラベルの設定
            chart1.ChartAreas["ChartArea1"].AxisX.Title = "日付";
            chart1.ChartAreas["ChartArea1"].AxisY.Title = "";

            //目盛間隔の設定
            if (comboBox2.Text == "全てのグラフ")
            {
                chart1.ChartAreas["ChartArea1"].AxisX.Interval = 7;
                chart1.ChartAreas["ChartArea1"].AxisY.Interval = 5;
            }
            else
            {
                chart1.ChartAreas["ChartArea1"].AxisX.Interval = 7;
                chart1.ChartAreas["ChartArea1"].AxisY.Interval = 2;
            }

            //目盛線
            chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
            chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = true;

            //作ったSeriesをchartコントロールに追加する
            chart1.Series.Add(series1);
            chart1.Series.Add(series2);
            chart1.Series.Add(series3);
            chart1.Series.Add(series4);

            chart1.Legends.Add(leg);
        }
        #endregion

        #region "ボタン1クリック 終了"
        private void button1_Click_1(object sender, EventArgs e)
        {
            Close();
        }
        #endregion

        #region "コンボボックス1変更時"
        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            DispChart();
        }
        #endregion

        #region "コンボボックス2変更時"
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            DispChart();
        }
        #endregion
    }
}
    







コメントをお書きください

コメント: 1
  • #1

    paku (水曜日, 06 9月 2017 13:44)

    こんにちわ.
    こんにちわ。
    私、現在C#の勉強をしているpakuと申します。
    今回、グラフと表を使ったC#を書きたいと思い、検索していたところ
    こちらのサイトに着きました。
    もし、よろしければで良いのですがこのプログラムのその他のフォームのコードも
    見せていただけないでしょうか?
    サンプルコードを探しても、良いものが見つからなくて・・・。
    よろしくお願いいたします。