■ 레이블을 갖는 히스토그램 그리기
------------------------------------------------------------------------------------------------------------------------
▶ MainForm.cs
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary> /// 최소 값 /// </summary> private const int MIN_VALUE = 0;
/// <summary> /// 최대 값 /// </summary> private const int MAX_VALUE = 100;
/// <summary> /// 값 배열 /// </summary> private float[] valueArray = new float[10];
/// <summary> /// 레이블 배열 /// </summary> private Label[] labelArray = new Label[10];
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent();
#region 이벤트를 설정한다.
Load += Form_Load; this.canvasPictureBox.Paint += canvasPictureBox_Paint; this.canvasPictureBox.Resize += canvasPictureBox_Resize;
#endregion }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event
#region 폼 로드시 처리하기 - Form_Load(sender, e)
/// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { Random random = new Random();
for(int i = 0; i < this.valueArray.Length; i++) { this.valueArray[i] = random.Next(MIN_VALUE + 5, MAX_VALUE - 5);
this.labelArray[i] = new Label();
this.labelArray[i].Parent = this.canvasPictureBox; this.labelArray[i].Text = this.valueArray[i].ToString(); this.labelArray[i].ForeColor = Color.Black; this.labelArray[i].BackColor = Color.Transparent; this.labelArray[i].AutoSize = true; } }
#endregion #region 캔버스 픽처 박스 페인트시 처리하기 - canvasPictureBox_Paint(sender, e)
/// <summary> /// 캔버스 픽처 박스 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void canvasPictureBox_Paint(object sender, PaintEventArgs e) { DrawHistogram ( e.Graphics, this.canvasPictureBox.BackColor, this.valueArray, this.canvasPictureBox.ClientSize.Width, this.canvasPictureBox.ClientSize.Height ); }
#endregion #region 캔버스 픽처 박스 크기 조정시 처리하기 - canvasPictureBox_Resize(sender, e)
/// <summary> /// 캔버스 픽처 박스 크기 조정시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void canvasPictureBox_Resize(object sender, EventArgs e) { this.canvasPictureBox.Refresh(); }
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 히스토그램 그리기 - DrawHistogram(graphics, backgroundColor, valueArray, width, height)
/// <summary> /// 히스토그램 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="backgroundColor">배경색</param> /// <param name="valueArray">값 배열</param> /// <param name="width">너비</param> /// <param name="height">높이</param> private void DrawHistogram(Graphics graphics, Color backgroundColor, float[] valueArray, int width, int height) { Color[] colorArray = new Color[] { Color.Red, Color.LightGreen, Color.Blue, Color.Pink, Color.Green, Color.LightBlue, Color.Orange, Color.Yellow, Color.Purple };
graphics.Clear(backgroundColor);
RectangleF dataBoundRectangle = new RectangleF(0, 0, valueArray.Length, MAX_VALUE);
PointF[] pointArray = { new PointF(0, height), new PointF(width, height), new PointF(0, 0) };
Matrix transformMatrix = new Matrix(dataBoundRectangle, pointArray);
graphics.Transform = transformMatrix;
using(Pen pen = new Pen(Color.Black, 0)) { for(int i = 0; i < valueArray.Length; i++) { RectangleF valueRectangle = new RectangleF(i, 0, 1, valueArray[i]);
using(Brush brush = new SolidBrush(colorArray[i % colorArray.Length])) { graphics.FillRectangle(brush, valueRectangle);
graphics.DrawRectangle ( pen, valueRectangle.X, valueRectangle.Y, valueRectangle.Width, valueRectangle.Height ); }
PointF[] valuePointArray = { new PointF ( valueRectangle.Left + valueRectangle.Width / 2f, valueRectangle.Bottom ) };
transformMatrix.TransformPoints(valuePointArray);
this.labelArray[i].Location = new Point ( (int)valuePointArray[0].X - this.labelArray[i].Width / 2, (int)valuePointArray[0].Y - this.labelArray[i].Height ); } }
graphics.ResetTransform();
graphics.DrawRectangle(Pens.Black, 0, 0, width - 1, height - 1); }
#endregion } }
|
------------------------------------------------------------------------------------------------------------------------
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 시어핀스키 8각형(Sierpinski Octagon) 그리기 (0) | 2019.01.15 |
---|---|
[C#/WINFORM] 쌍곡나사선(Hyperbolic Spiral) 그리기 (0) | 2019.01.15 |
[C#/WINFORM] 아르키메데스 나선(Archimedes Spiral) 그리기 (0) | 2019.01.15 |
[C#/WINFORM] Component 클래스 : 커스텀 컴포넌트 사용하기 (0) | 2019.01.15 |
[C#/WINFORM] 컬럼 크기에 맞춰 텍스트 그리기 (0) | 2019.01.14 |
[C#/WINFORM] 레이블을 갖는 히스토그램 그리기 (0) | 2019.01.14 |
[C#/WINFORM] 장미 곡선(Rose Curve) 그리기 (0) | 2019.01.14 |
[C#/WINFORM] 테오 스파이럴(Spiral of Theodorus) 그리기 (0) | 2019.01.14 |
[C#/WINFORM] 피타고라스 나무 프랙탈(Pythagorean Tree Fractal) 그리기 (0) | 2019.01.14 |
[C#/WINFORM] 시어핀스키 펜타곤(Sierpinski Pentagon) 그리기 (0) | 2019.01.14 |
[C#/WINFORM] Cursor 클래스 : 애니메이션 커서 사용하기 (0) | 2019.01.14 |
댓글을 달아 주세요