C# 动态绘制扇形图;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace pieDemp
{
    public partial class Form1 : Form
    {
        private int count = 0;
        // Brushes used to fill pie slices.
        private Brush[] SliceBrushes =
        {
            Brushes.Red,
            Brushes.LightGreen,
            Brushes.Blue,
            Brushes.LightBlue,
            Brushes.Green,
            Brushes.Lime,
            Brushes.Orange,
            Brushes.Fuchsia,
            Brushes.Yellow,
            Brushes.Cyan,
        };

        // Pens used to outline pie slices.
        private Pen[] SlicePens = { Pens.Black };

        // The data values to chart.
        private float[] Values = new float[10];

        // Make some random data.
        private void Form1_Load(object sender, EventArgs e)
        {
            Random rand = new Random();
            for (int i = 0; i < Values.Length; i++)
            {
                Values[i] = rand.Next(10, 40);
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            Console.WriteLine(e.Location.X.ToString() + "," + e.Location.Y.ToString());
        }

        public Form1()
        {
            InitializeComponent();
            timer1.Start();
        }

        // Draw the pie chart.
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(BackColor);
            if ((ClientSize.Width < 20) || (ClientSize.Height < 20)) return;

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            //Rectangle rect = new Rectangle(
                //10, 10, ClientSize.Width - 20, ClientSize.Height - 20);
            //DrawPieChart(e.Graphics, rect, SliceBrushes, SlicePens, Values);

            //以下绘图操作
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            const int margin = 10;
            const int width = 100;
            Graphics gr = e.Graphics;
            Pen outline_pen = Pens.Red;
            Brush fill_brush = Brushes.LightGreen;
            Brush fill_brush1 = Brushes.Red;




            using (Pen ellipse_pen = new Pen(Color.Blue))
            {
                //圆形外虚线的长度,以及间隔
                ellipse_pen.DashPattern = new float[] { 2, 2 };
                ////以矩形左上角为参考点,绘制内切圆
                Rectangle rect1 = new Rectangle(margin + 130, 100, width + 100, width + 100);
                gr.DrawRectangle(ellipse_pen, rect1);
                gr.DrawEllipse(ellipse_pen, rect1);
                //gr.FillEllipse(fill_brush,rect1);
                gr.FillPie(fill_brush, rect1, -90, 50);
                gr.DrawPie(outline_pen, rect1, -90, 50);

                //Rectangle rect2 = new Rectangle(230, 190, 20, 20);
                int left = 230, top = 190, w = 20;
                left = left - count*10;
                top = top - count * 10;
                w = w + count * 20;

                Rectangle rect = new Rectangle(left, top, w, w);
                gr.DrawEllipse(ellipse_pen, rect);
                gr.FillPie(fill_brush1, rect, -90, 50);
                gr.DrawPie(outline_pen, rect, -90, 50);

                count++;
                if (count == 10)
                    count = 0;

                //Console.WriteLine("hello world");
                //Rectangle rect2 = new Rectangle(230, 190, 20, 20);
                //Rectangle rect3 = new Rectangle(220, 180, 40, 40);
                //Rectangle rect4 = new Rectangle(210, 170, 60, 60);
                //Rectangle rect5 = new Rectangle(200, 160, 80, 80);
                //Rectangle rect6 = new Rectangle(190, 150, 100, 100);
                //Rectangle rect7 = new Rectangle(180, 140, 120, 120);
                //Rectangle rect8 = new Rectangle(170, 130, 140, 140);
                //Rectangle rect9 = new Rectangle(160, 120, 160, 160);
                //Rectangle rect10 = new Rectangle(150, 110, 180, 180);
                //Rectangle rect11 = new Rectangle(140, 100, 200, 200);































                // Everything else.6
                //140,100

                //gr.FillPie(fill_brush, rect1, -30, 60);
                //gr.DrawPie(outline_pen, rect1, -30, 60);

                //gr.FillPie(fill_brush, rect1, -10, 10);
                //gr.DrawPie(outline_pen, rect1, -10, 10);

                // Northeast wedge.
                //Rectangle rect0 = new Rectangle(180, 140, 120, 120);
                //gr.DrawEllipse(ellipse_pen, rect0);
                //gr.FillPie(fill_brush1, rect0, -90, 50);
                //gr.DrawPie(outline_pen, rect0, -90, 50);



                //开始

                //Rectangle rect2 = new Rectangle(230, 190, 20, 20);
                //gr.DrawEllipse(ellipse_pen, rect2);
                //gr.FillPie(fill_brush1, rect2, -90, 50);
                //gr.DrawPie(outline_pen, rect2, -90, 50);

                //Rectangle rect3 = new Rectangle(220, 180, 40, 40);
                //gr.DrawEllipse(ellipse_pen, rect3);
                //gr.FillPie(fill_brush1, rect3, -90, 50);
                //gr.DrawPie(outline_pen, rect3, -90, 50);

                //Rectangle rect4 = new Rectangle(210, 170, 60, 60);
                //gr.DrawEllipse(ellipse_pen, rect4);
                //gr.FillPie(fill_brush1, rect4, -90, 50);
                //gr.DrawPie(outline_pen, rect4, -90, 50);

                //Rectangle rect5 = new Rectangle(200, 160, 80, 80);
                //gr.DrawEllipse(ellipse_pen, rect5);
                //gr.FillPie(fill_brush1, rect5, -90, 50);
                //gr.DrawPie(outline_pen, rect5, -90, 50);

                //Rectangle rect6 = new Rectangle(190, 150, 100, 100);
                //gr.DrawEllipse(ellipse_pen, rect6);
                //gr.FillPie(fill_brush1, rect6, -90, 50);
                //gr.DrawPie(outline_pen, rect6, -90, 50);

                //Rectangle rect7 = new Rectangle(180, 140, 120, 120);
                //gr.DrawEllipse(ellipse_pen, rect7);
                //gr.FillPie(fill_brush1, rect7, -90, 50);
                //gr.DrawPie(outline_pen, rect7, -90, 50);


                //Rectangle rect8 = new Rectangle(170, 130, 140, 140);
                //gr.DrawEllipse(ellipse_pen, rect8);
                //gr.FillPie(fill_brush1, rect8, -90, 50);
                //gr.DrawPie(outline_pen, rect8, -90, 50);

                //Rectangle rect9 = new Rectangle(160, 120, 160, 160);
                //gr.DrawEllipse(ellipse_pen, rect9);
                //gr.FillPie(fill_brush1, rect9, -90, 50);
                //gr.DrawPie(outline_pen, rect9, -90, 50);

                //Rectangle rect10 = new Rectangle(150, 110, 180, 180);
                //gr.DrawEllipse(ellipse_pen, rect10);
                //gr.FillPie(fill_brush1, rect10, -90, 50);
                //gr.DrawPie(outline_pen, rect10, -90, 50);

                ////以矩形左上角为参考点,绘制内切圆
                //Rectangle rect1 = new Rectangle(margin + 130, 100, width + 100, width + 100);
                //gr.DrawRectangle(ellipse_pen, rect1);
                //gr.DrawEllipse(ellipse_pen, rect1);
                ////gr.FillEllipse(fill_brush,rect1);
                //gr.FillPie(fill_brush1, rect1, -90, 50);
                //gr.DrawPie(outline_pen, rect1, -90, 50);
                //以上









                //gr.FillPie(fill_brush, rect, 30, 60);
                //gr.DrawPie(outline_pen, rect, 30, 60);

                //gr.FillPie(fill_brush, rect, 0, 10);
                //gr.DrawPie(outline_pen, rect, 0, 10);

                //// East wedge.
                //rect.Y += width + margin;
                //rect.X = margin + 30;
                //gr.DrawEllipse(ellipse_pen, rect);
                //gr.FillPie(fill_brush, rect, 315, 90);
                //gr.DrawPie(outline_pen, rect, 315, 90);

                //// Everything else.
                //rect.X += width + margin;
                //gr.DrawEllipse(ellipse_pen, rect);
                //gr.FillPie(fill_brush, rect, 315, -270);
                //gr.DrawPie(outline_pen, rect, 315, -270);

                //// Northwest quadrant.
                //rect.Y += width + margin;
                //rect.X = margin + 30;
                //gr.DrawEllipse(ellipse_pen, rect);
                //gr.FillPie(fill_brush, rect, 180, 90);
                //gr.DrawPie(outline_pen, rect, 180, 90);

                //// Everything else.
                //rect.X += width + margin;
                //gr.DrawEllipse(ellipse_pen, rect);
                //gr.FillPie(fill_brush, rect, 180, -270);
                //gr.DrawPie(outline_pen, rect, 180, -270);
            }
        }


        // Draw a pie chart.
        private static void DrawPieChart(Graphics gr,
            Rectangle rect, Brush[] brushes, Pen[] pens,
            float[] values)
        {
            // Get the total of all angles.
            float total = values.Sum();

            // Draw the slices.
            float start_angle = 0;
            for (int i = 0; i < values.Length; i++)
            {
                float sweep_angle = values[i] * 360f / total;
                gr.FillPie(brushes[i % brushes.Length],
                    rect, start_angle, sweep_angle);
                gr.DrawPie(pens[i % pens.Length],
                    rect, start_angle, sweep_angle);
                start_angle += sweep_angle;
            }
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            Refresh();
        }
    }
}

  C# 动态绘制扇形图;

C# 动态绘制扇形图;

上一篇:Windows下Redis开机自启动


下一篇:c#设计模式(4)——抽象工厂模式