안녕하세요, 허블입니다.
오늘은 C# winform에서 제공해주는 Chart 클래스에 대해서 알아보려고 합니다. Chart를 이용하면 별도의 라이브러리 설치 없이 그래프를 렌더링 할 수 있습니다. Chart 클래스를 사용하기 위해서는 .NET 버전이 4.0 이상 되어야 합니다.
.NET버전이 4.0 이상인지는 cmd창에서 dir %windir%\Microsoft.NET\framework /AD를 치면 됩니다. 그 이상의 버전은 Visual Studio Installer에서 쉽게 볼 수 있습니다.
4.0 미만이라면 Visual Studio Installer에서 수정을 하여 버전을 올려야 합니다. Chart 클래스가 보이지 않는다면 먼저 .Net의 버전을 확인을 해줍니다. 이제 Chart 클래스를 사용해보겠습니다.
1. 도구 상자에서 Chart 추가
먼저 도구 상자 탭에 데이터에서 Chart를 불러옵니다.
기본적인 UI는 이렇습니다. 그래프의 값을 입력해보도록 하겠습니다.
간단한 예제
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
chart1.Series[0].Points.Add(10);
chart1.Series[0].Points.Add(20);
chart1.Series[0].Points.Add(30);
chart1.Series[0].Points.Add(40);
chart1.Series[0].Points.Add(50);
}
private void chart1_Click(object sender, EventArgs e)
{
}
}
}
|
|
|
2. Chart 종류, 범례 설정
차트 속성에는 Series가 있습니다. Series 편집기에서 보이는 LegendText를 통해 범례를 변경할 수 있습니다. 레전드는 전설이라는 뜻도 있지만 범례라는 뜻도 있습니다.
ChartTYpe은 차트 종류입니다. 막대그래프, 꺾은선 그래프 등등...
여기서 추가를 통해서 범례를 추가할 수 있습니다.
Series 편집기를 통해서 위에와 같이 범례, 그래프 종류를 선택할 수 있습니다.
간단한 예제
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
chart1.Titles.Add("타이틀 제목");
// 범례1
chart1.Series["데이터1"].Points.Add(55);
chart1.Series["데이터1"].Points.Add(35);
chart1.Series["데이터1"].Points.Add(35);
chart1.Series["데이터1"].Points.Add(25);
chart1.Series["데이터1"].Points.Add(15);
// 범례2
chart1.Series["데이터2"].Points.Add(10);
chart1.Series["데이터2"].Points.Add(5);
chart1.Series["데이터2"].Points.Add(3);
chart1.Series["데이터2"].Points.Add(4);
chart1.Series["데이터2"].Points.Add(5);
}
private void chart1_Click(object sender, EventArgs e)
{
}
}
}
|
3. Title설정
타이틀 제목은 타이틀 편집기에서 Text를 편집하면 됩니다.
Font를 편집해주고 컴파일을 하면...
그래프에 타이틀이 추가가 됩니다.
4. 예제 소스 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Series 삭제
chart1.Series.Clear();
// 객체 생성
Series Chart1 = chart1.Series.Add("Series1");
Series Chart2 = chart1.Series.Add("Series2");
// 타이틀 객체 생성
Title title = new Title();
title.Text = "타이틀은 제목";
title.ForeColor = Color.Blue;
title.Font = new Font("맑은고딕", 25, FontStyle.Bold);
chart1.Titles.Add(title);
// 범례 설정
Chart1.LegendText = "데이터 1";
Chart2.LegendText = "데이터 2";
// 차트 종류 설정
Chart1.ChartType = SeriesChartType.Line; // 선
Chart2.ChartType = SeriesChartType.Point; // 점
// 차트 색상 설정
Chart1.Color = Color.LightGreen;
Chart2.Color = Color.Red;
// 차트 굵기 설정
Chart1.BorderWidth = 5;
// 범례1 데이터
for (double i = 0; i < 2 * Math.PI; i+= 0.1)
{
Chart1.Points.AddXY(i, Math.Sin(i));
}
// 범례2 데이터
for (double i = 0; i < 2; i += 0.1)
{
Chart2.Points.AddXY(i, i);
}
}
private void chart1_Click(object sender, EventArgs e)
{
}
}
}
|
예제를 통해 Chart의 간단한 사용 방법에 대해서 알아보았습니다.
예제에 대한 설명은 주석을 참고하시면 됩니다.
2021.05.30 - [프로그래밍/MFC] - [MFC]CStdioFile 클래스를 이용해 유니코드 텍스트 저장, 불러오기
'프로그래밍 > C#' 카테고리의 다른 글
C/C++ DLL 생성 및 C# 윈폼에서 사용하기 (0) | 2024.04.19 |
---|---|
C# USB 통신, LibUsbDotNet (0) | 2020.08.27 |
C# 시리얼 통신 아스키 외 값 읽기 (8) | 2020.08.17 |
C# 유튜브 동영상 다운로더 만들기(MP3, MP4) (1) | 2020.06.22 |
C# 마우스 제어(클릭, 좌표 이동) (4) | 2019.08.25 |