첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 슬라이스 브러시 배열
        /// </summary>
        private Brush[] sliceBrushArray =
        {
            Brushes.Red,
            Brushes.LightGreen,
            Brushes.Blue,
            Brushes.LightBlue,
            Brushes.Green,
            Brushes.Lime,
            Brushes.Orange,
            Brushes.Fuchsia,
            Brushes.Yellow,
            Brushes.Cyan,
        };

        /// <summary>
        /// 슬라이스 펜 배열
        /// </summary>
        private Pen[] slicePenArray = { Pens.Black };

        /// <summary>
        /// 값 배열
        /// </summary>
        private float[] valueArray = new float[10];

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            #region 이벤트를 설정한다.

            Load  += Form_Load;
            Paint += Form_Paint;

            #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(10, 40);
            }
        }

        #endregion
        #region 폼 페인트시 처리하기 - Form_Paint(sender, e)

        /// <summary>
        /// 폼 페인트시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(BackColor);

            if((ClientSize.Width < 20) || (ClientSize.Height < 20))
            {
                return;
            }

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            int size = (ClientSize.Width > ClientSize.Height ? ClientSize.Height : ClientSize.Width) - 20;
            int left = (ClientSize.Width  - size) / 2;
            int top  = (ClientSize.Height - size) / 2;

            Rectangle rectangle = new Rectangle(left, top, size, size);

            DrawPieChart(e.Graphics, rectangle, this.sliceBrushArray, this.slicePenArray, this.valueArray);
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 파이 차트 그리기 - DrawPieChart(graphics, rectangle, brushArray, penArray, valueArray)

        /// <summary>
        /// 파이 차트 그리기
        /// </summary>
        /// <param name="graphics">그래픽스</param>
        /// <param name="rectangle">사각형</param>
        /// <param name="brushArray">브러시 배열</param>
        /// <param name="penArray">펜 배열</param>
        /// <param name="valueArray">값 배열</param>
        private void DrawPieChart(Graphics graphics, Rectangle rectangle, Brush[] brushArray, Pen[] penArray, float[] valueArray)
        {
            float totalValue = valueArray.Sum();
            float startAngle = 0;

            for(int i = 0; i < valueArray.Length; i++)
            {
                float sweepAngle = valueArray[i] * 360f / totalValue;

                graphics.FillPie(brushArray[i % brushArray.Length], rectangle, startAngle, sweepAngle);

                graphics.DrawPie(penArray[i % penArray.Length], rectangle, startAngle, sweepAngle);

                startAngle += sweepAngle;
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by 사용자 icodebroker

댓글을 달아 주세요