第十二周 C# 试验 《 4 》

来源:互联网 发布:java超市账单管理系统 编辑:程序博客网 时间:2024/05/18 13:47

4.编写一个Windows应用程序,由给定数据25,15,10,30,20饼图(如图4)

 

private void Form1_Paint(object sender, PaintEventArgs e)

{

            int [] array={25,15,10,30,20};

            float sum=0;

            for (int i = 0; i < array.Length; i++)

            {

                sum+=array[i];

            }

            Graphics g = e.Graphics;

            SolidBrush brush =newSolidBrush(Color.Red);

            g.FillPie(brush, new Rectangle(10, 10, 200, 200), 0, (float)array[0] *360/ sum);

            brush = new SolidBrush(Color.Blue);

            g.FillPie(brush, new Rectangle(10, 10, 200, 200), (float)array[0] * 360 / sum, (float)array[1] * 360 / sum);

            brush = new SolidBrush(Color.Green);

            g.FillPie(brush, new Rectangle(10, 10, 200, 200), (float)(array[0]+array[1]) * 360 / sum,

                (float)array[2] * 360 / sum);

            brush = new SolidBrush(Color.Yellow);

            g.FillPie(brush, new Rectangle(10, 10, 200, 200),

(float)(array[0] + array[1]+array[2]) * 360 / sum,

                (float)array[3] * 360 / sum);

            brush = new SolidBrush(Color.White);

            g.FillPie(brush, new Rectangle(10, 10, 200, 200),

(float)(array[0] + array[1] + array[2]+array[3]) * 360 / sum,

                (float)array[4] * 360 / sum);

}

 
 
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Drawing.Drawing2D;namespace WindowsFormsApplication2{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Paint(object sender, PaintEventArgs e)        {            int[] array = { 25, 15, 10, 30, 20 };            float sum = 0;            for (int i = 0; i < array.Length; i++)            {                sum += array[i];            }            Graphics g = e.Graphics;            SolidBrush brush = new SolidBrush(Color.Red);            g.FillPie(brush, new Rectangle(10, 10, 200, 200), 0, (float)array[0] * 360 / sum);            brush = new SolidBrush(Color.Blue);            g.FillPie(brush, new Rectangle(10, 10, 200, 200), (float)array[0] * 360 / sum, (float)array[1] * 360 / sum);            brush = new SolidBrush(Color.Green);            g.FillPie(brush, new Rectangle(10, 10, 200, 200), (float)(array[0] + array[1]) * 360 / sum,                (float)array[2] * 360 / sum);            brush = new SolidBrush(Color.Yellow);            g.FillPie(brush, new Rectangle(10, 10, 200, 200),(float)(array[0] + array[1] + array[2]) * 360 / sum,                (float)array[3] * 360 / sum);            brush = new SolidBrush(Color.White);            g.FillPie(brush, new Rectangle(10, 10, 200, 200),(float)(array[0] + array[1] + array[2] + array[3]) * 360 / sum,                (float)array[4] * 360 / sum);}        }    }