■ UserControl 클래스를 사용해 등고선 차트 컨트롤을 만드는 방법을 보여준다.
▶ ContourChartControl.cs
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace TestProject; /// <summary> /// 등고선 차트 컨트롤 /// </summary> public partial class ContourChartControl : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 레벨 배열 /// </summary> private float[] levelArray; /// <summary> /// 색상 배열 /// </summary> private Color[] colorArray; /// <summary> /// 데이터 배열 /// </summary> private float[,] valueArray; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ContourChartControl() /// <summary> /// 생성자 /// </summary> public ContourChartControl() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 데이터 설정하기 - SetData(levelArray, colorArray, valueArray) /// <summary> /// 데이터 설정하기 /// </summary> /// <param name="levelArray">레벨 배열</param> /// <param name="colorArray">색상 배열</param> /// <param name="valueArray">값 배열</param> public void SetData(float[] levelArray, Color[] colorArray, float[,] valueArray) { this.levelArray = levelArray; this.colorArray = colorArray; this.valueArray = valueArray; Invalidate(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 페인트 처리하기 - OnPaint(e) /// <summary> /// 페인트 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if(this.valueArray == null || this.levelArray == null || this.colorArray == null) { return; } DrawContour(e.Graphics); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 포인트 스케일 설정하기 - ScalePoint(sourcePoint) /// <summary> /// 포인트 스케일 설정하기 /// </summary> /// <param name="sourcePoint">소스 포인트</param> /// <returns>스케일 포인트</returns> private PointF ScalePoint(PointF sourcePoint) { return new PointF(sourcePoint.X * Width / (valueArray.GetLength(0) - 1), Height - sourcePoint.Y * Height / (valueArray.GetLength(1) - 1)); } #endregion #region 등고선 셀 그리기 - DrawContourCell(graphics, x, y, valueArray, lowLevel, highLevel, color) /// <summary> /// 등고선 셀 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="valueArray">값 배열</param> /// <param name="lowLevel">하한 레벨</param> /// <param name="highLevel">상한 레벨</param> /// <param name="color">색상</param> private void DrawContourCell(Graphics graphics, int x, int y, float[] valueArray, float lowLevel, float highLevel, Color color) { List<PointF> pointList = new List<PointF>(); for(int i = 0; i < 4; i++) { int j = (i + 1) % 4; if((valueArray[i] < lowLevel && valueArray[j] >= lowLevel) || (valueArray[i] >= lowLevel && valueArray[j] < lowLevel)) { float t = (lowLevel - valueArray[i]) / (valueArray[j] - valueArray[i]); pointList.Add ( new PointF ( x + (i % 2) + t * ((j % 2) - (i % 2)), y + (i / 2) + t * ((j / 2) - (i / 2)) ) ); } } if(pointList.Count == 2) { using(Pen pen = new Pen(color, 1)) { graphics.DrawLine(pen, ScalePoint(pointList[0]), ScalePoint(pointList[1])); } } } #endregion #region 등고선 그리기 - DrawContour(graphics) /// <summary> /// 등고선 그리기 /// </summary> /// <param name="graphics">그래픽스</param> private void DrawContour(Graphics graphics) { int width = this.valueArray.GetLength(0); int height = this.valueArray.GetLength(1); for(int x = 0; x < width - 1; x++) { for(int y = 0; y < height - 1; y++) { float[] cellValueArray = new float[] { valueArray[x , y ], valueArray[x + 1, y ], valueArray[x + 1, y + 1], valueArray[x , y + 1] }; for(int i = 0; i < this.levelArray.Length - 1; i++) { DrawContourCell(graphics, x, y, cellValueArray, this.levelArray[i], this.levelArray[i + 1], this.colorArray[i]); } } } } #endregion } |
▶ MainForm.cs
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 |
using System; using System.Drawing; using System.Windows.Forms; namespace TestProject; /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); float[] levelArray = { 0f, 0.2f, 0.4f, 0.6f, 0.8f, 1f }; Color[] colorArray = { Color.Blue, Color.Green, Color.Yellow, Color.Orange, Color.Red }; float[,] valueArray = GetValueArray(); contourChartControl.SetData(levelArray, colorArray, valueArray); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 값 배열 구하기 - GetValueArray() /// <summary> /// 값 배열 구하기 /// </summary> /// <returns>값 배열</returns> public float[,] GetValueArray() { int width = this.contourChartControl.Width; int height = this.contourChartControl.Height; float[,] valueArray = new float[width, height]; for(int x = 0; x < width; x++) { for(int y = 0; y < height; y++) { float xNormalized = (float)x / width * 10; float yNormalized = (float)y / height * 10; valueArray[x, y] = (float)(Math.Sin(xNormalized) * Math.Cos(yNormalized) + Math.Cos(xNormalized * 0.5f) * Math.Sin(yNormalized * 0.5f)); valueArray[x, y] = (valueArray[x, y] + 1) / 2; } } return valueArray; } #endregion } |