1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7
8 using System.Drawing;
9
10 public partial class _Default : System.Web.UI.Page
11 {
12 /// <summary>
13 /// 画饼图
14 /// </summary>
15 /// <param name="sender"></param>
16 /// <param name="e"></param>
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 //在一张图片上写“饼图显示”,并把图片显示出来
20 Bitmap bm = new Bitmap(400, 400);
21 Graphics g = Graphics.FromImage(bm);
22 g.Clear(Color.White); //先清理一下图形的背景色
23 g.DrawString("饼图显示",new Font("宋体",23),new SolidBrush(Color.Red),new PointF(5,5));
24
25
26 //画饼图
27 int[] Num = { 100, 200, 300, 460 }; //饼图上各部分所占的比例
28 Color[] colors = { Color.Red, Color.Green, Color.Blue, Color.Yellow }; //饼图各部分的颜色
29
30 //计算各部分数字的和
31 float TotalValue = 0;
32 for (int i = 0; i < Num.Length; i++)
33 {
34 TotalValue = TotalValue + Num[i];
35 }
36
38 float sweepAngle = 0; //扇形经过的角度
39 float startAngle = 0; //扇形起始的角度,不断的累加
40 float x = 50f;
41 float y = 50f; //全部都用float型,
42 float width = 200;
43 int index = 0; //指示器,指示用哪一个颜色
44 Color Singlecolor = new Color(); //有更简便的方法
45 foreach (int i in Num)
46 {
47 sweepAngle = i / TotalValue * 360; //旋转的角度
48 Singlecolor = colors[index]; //颜色
49 g.FillPie(new SolidBrush(Singlecolor), x, y, width, width, startAngle, sweepAngle); //填充扇形区域,从正右边开始,顺时针往下画,x,y的意思是??
50
51 g.DrawPie(Pens.Black , x, y, width, width, startAngle, sweepAngle); //画边
52 startAngle = startAngle + sweepAngle; //起始角度不断的累加
53 index++;
54 }
55 bm.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); //保存图片
56 g.Dispose();
57 }
58 }
请发表评论