본문 바로가기

C# Form

[C# Form] Chart Control을 이용한 그래프 그리기

 

ecg-ppg.zip

그림과 같이 ECG, PPG 신호를 파일에서 읽어서 화면에 뿌려주는 프로그램을 만들어 보자.

Chart Control을 사용한다.

 

실행화면은 다음과 같다.

 

 

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 ecg_ppg
{
    public partial class Form1 : Form
    {
        int Min = Int32.MaxValue;
        int Max = Int32.MinValue;
        int Min2 = Int32.MaxValue;
        int Max2 = Int32.MinValue;
        double[] ecg = new double[100000];
        double[] ppg = new double[100000];
        private int ecgLength;
        private int ppgLength;
       
        private int Tick = 0;
        private bool isTimerOn = true;

        public Form1()
        {
            InitializeComponent();
        }

        private void chart1_Click(object sender, EventArgs e)
        {
            if (isTimerOn)
            {
                timer1.Stop();
                isTimerOn = false;
            }
            else
            {
                timer1.Start();
                isTimerOn = true;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ecgppgRead();
            chart1.Series["ecg"].BorderWidth = 1;
            chart1.Series["ppg"].Color = Color.Red;

            for (int x = 0; x < ecgLength; x += 1)
            {
                chart1.Series["ecg"].Points.AddXY((double)x, ecg[x]);
            }
            for (int x = 0; x < ppgLength; x += 1)
            {
                chart1.Series["ppg"].Points.AddXY((double)x, ppg[x]);
            }
            Controls.Add(chart1);
        }

        private void ecgppgRead()
        {
            string filename = @"C:\Users\bikang\Documents\Visual Studio 2010\Projects\ecg-ppg\ecg-ppg\a101.txt";
            string[] lines = System.IO.File.ReadAllLines(filename);

            string filename2 = @"C:\Users\bikang\Documents\Visual Studio 2010\Projects\ecg-ppg\ecg-ppg\ppg.txt";
            string[] lines2 = System.IO.File.ReadAllLines(filename2);

            int i = 0;
            foreach (string line in lines)
            {
                ecg[i] = Convert.ToDouble(line);
                if (ecg[i] < Min) Min = Convert.ToInt32(ecg[i]);
                if (ecg[i] > Max) Max = Convert.ToInt32(ecg[i]);
                i++;
            }
            ecgLength = i;
            Console.WriteLine("Min = {0}, Max ={1}", Min, Max);

            i = 0;
            foreach (string line in lines2)
            {
                ppg[i] = Convert.ToDouble(line);
                if (ppg[i] < Min2) Min2 = Convert.ToInt32(ppg[i]);
                if (ppg[i] > Max2) Max2 = Convert.ToInt32(ppg[i]);
                i++;
            }
  
            ppgLength = i;
            Console.WriteLine("Min2 = {0}, Max2 = {1}", Min2, Max2);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ecgChartSetting(Tick * 10);
            Tick += 1;
        }

        private void ecgChartSetting(int minX)
        {
            chart1.ChartAreas["ChartArea1"].AxisX.Minimum = minX;
            chart1.ChartAreas["ChartArea1"].AxisX.Maximum = minX + 1500;
            chart1.ChartAreas["ChartArea1"].AxisY.Minimum = Min / 100 * 100;
            chart1.ChartAreas["ChartArea1"].AxisY.Maximum = Max / 100 * 100;

            chart1.ChartAreas["ChartArea2"].AxisX.Minimum = minX;
            chart1.ChartAreas["ChartArea2"].AxisX.Maximum = minX + 1500;
            chart1.ChartAreas["ChartArea2"].AxisY.Minimum = Min2;
            chart1.ChartAreas["ChartArea2"].AxisY.Maximum = Max2; ;
        }
    }
}