c# 실시간 라인 차트(그래프) 만들기 

 

 

1. 먼저 폼에 차타, 버튼, 타이머를 추가한다. 

 

2. 다음과 같이 코딩한다. 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Linegraph_Example
{
    public partial class Form1 : Form
    {
        double x;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "Start";
            timer1.Tick += timer1_Tick;
            timer1.Interval = 50;

            chart1.Series[0].ChartType = SeriesChartType.Line;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            chart1.Series[0].Points.AddXY(x, 3 * Math.Sin(5 * x) + 5 * Math.Cos(3 * x));

            if (chart1.Series[0].Points.Count > 100)
                chart1.Series[0].Points.RemoveAt(0);

            chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[0].Points[0].XValue;
            chart1.ChartAreas[0].AxisX.Maximum = x;

            x += 0.1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled)
            {
                timer1.Stop();
                button1.Text = "Start";
            }
            else
            {
                timer1.Start();
                button1.Text = "Stop";
            }
                
        }
    }
}

 

 

3. c#으로 실시간 라인차트 만들기 동영상 보고 따라한다. 

 

+ Recent posts