由于项目的原因,需要在WinForm上做出一个饼形统计图的展示效果。相关数据是动态从数据库中获取,相应所占圆的角度计算都不是难点,可以很快地做出一个饼形统计图。

但难点在于如何将一些介绍文字插入到相应的弧度上。

实现的效果如下图所示:

相应的代码如下:

        /*3个参数
     type:是指需要写柱形图,还是饼形图,后面有个相应的判断
     StartTime:是指统计开始时间
     EndTime:是指统计结束时间 */
private void Create_Chart(string type, DateTime StartTime, DateTime EndTime) { /*定义3个动态数组,分别储存选项,选项对应数量,选项对应百分比,选项对应的饼图角度,选项累积的总数*/ ArrayList arraylist_type = new ArrayList(); ArrayList arraylist_count = new ArrayList(); ArrayList arraylist_tp = new ArrayList(); ArrayList arraylist_angle = new ArrayList(); /*定义2个int变量,分别储存统计总量,统计的总行数*/ int sum = 0; int resultCount = 0; /*取数据库中的选项,对应数量
     在这里,我采用的Linq 取数据库中的数据,大家如果采用SQL语句的话,直接从数据库操作层返回一个DataSet就可以了,方式类似*/ using (BLLUser a = new BLLUser()) { IList<Unit.HandleCountClass> result = a.Statistics_Handle(type, StartTime, EndTime); resultCount = result.Count; foreach (var item in result) { arraylist_type.Add(item.type.ToString()); arraylist_count.Add(Convert.ToInt32(item.Handlecount)); sum = sum + item.Handlecount; } } //声明宽和高 int width = 470, height = 470; //创建1个Bitmap对象 Bitmap bitmap = new Bitmap(width, height); Graphics g = Graphics.FromImage(bitmap); //使用Clear方法使画布为白色 g.Clear(Color.White); //抗锯齿 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //高质量的文字 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; //像素均偏移0.5个单位,以消除锯齿 g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; //创建2个画笔对象 Brush brush1 = new SolidBrush(Color.Black); Brush brush2 = new SolidBrush(Color.Orange); Brush brush3 = new SolidBrush(Color.White); //创建Font对象 Font font1 = new Font("Arial", 8); /*计算每个选项所占的百分比*/ for (int i = 0; i < resultCount; i++) { arraylist_tp.Add(Convert.ToSingle(Convert.ToSingle(arraylist_count[i]) * 100 / Convert.ToSingle(sum))); } /*Bar Chart为画柱形图*/ if (com_chart.Text == "Bar Chart") { //绘制显示文字、柱形图 for (int i = 0; i < resultCount; i++) { g.DrawString(arraylist_type[i].ToString() + " :", font1, brush1, new Point(20, 10 + i * 30)); g.FillRectangle(brush2, 130, 10 + i * 30, Convert.ToSingle(arraylist_tp[i]), 17); g.DrawString(arraylist_count[i].ToString() + "条 , " +
            arraylist_tp[i].ToString() + "%", font1, brush1, new Point(135 + Convert.ToInt32(arraylist_tp[i]), 10 + i * 30)); } } /*Pie Chart为画饼形图*/ else if (com_chart.Text == "Pie Chart") { const double T = 3.14; /*Π的常量值*/ double halfangle = 0.0; int piex = 100, piey = 100, piew = 250, pieh = 250; /*计算每个选项所占饼形图的角度*/ for (int i = 0; i < resultCount; i++) { arraylist_angle.Add(Convert.ToSingle(360/Convert.ToSingle(sum)) * Convert.ToSingle(arraylist_count[i])); } //绘制饼形图   for (int i = 0; i < resultCount; i++) { float tem_angle = 0; for (int j = 0; j < i; j++) { tem_angle = tem_angle + Convert.ToSingle(arraylist_angle[j]); }
            
            /*Get_Color(),是一个循环取色的方法,代码在后面*/ g.FillPie(
new SolidBrush(Get_Color(i)),piex,piey,piew,pieh,tem_angle,Convert.ToSingle(arraylist_angle[i])); /*以下是对四个象限、以及对90度、180度、270度和360度的判断*/ if (tem_angle + Convert.ToSingle(arraylist_angle[i]) / 2 < 90) { halfangle = tem_angle + (Convert.ToSingle(arraylist_angle[i])) / 2; double tem_sin = Math.Sin(T / 180 * halfangle); double tem_cos = Math.Cos(T / 180 * halfangle); int y = Convert.ToInt32(125 * tem_sin); int x = Convert.ToInt32(125 * tem_cos); g.DrawString(arraylist_type[i].ToString(), font1, brush1, new Point(225 + x, 225 + y)); g.DrawString((Convert.ToInt32(arraylist_tp[i])).ToString() + "%", font1, brush1, new Point(225 + x, 225 + y + 12)); } else if (tem_angle + Convert.ToSingle(arraylist_angle[i]) / 2 == 90) { g.DrawString(arraylist_type[i].ToString(), font1, brush1, new Point(225, 225 + 125)); g.DrawString((Convert.ToInt32(arraylist_tp[i])).ToString() + "%", font1, brush1, new Point(225, 225 + 125 + 12)); } else if (tem_angle + Convert.ToSingle(arraylist_angle[i]) / 2 > 90 && tem_angle + Convert.ToSingle(arraylist_angle[i]) / 2 < 180) { halfangle = (180 - tem_angle - Convert.ToSingle(arraylist_angle[i]) / 2); double tem_sin = Math.Sin(T / 180 * halfangle); double tem_cos = Math.Cos(T / 180 * halfangle); int y = Convert.ToInt32(125 * tem_sin); int x = Convert.ToInt32(125 * tem_cos); g.DrawString(arraylist_type[i].ToString(), font1, brush1, new Point(225 - x, 225 + y)); g.DrawString((Convert.ToInt32(arraylist_tp[i])).ToString() + "%", font1, brush1, new Point(225 - x, 225 + y + 12)); } else if (tem_angle + Convert.ToSingle(arraylist_angle[i]) / 2 == 180) { g.DrawString(arraylist_type[i].ToString(), font1, brush1, new Point(225 - 125, 225)); g.DrawString((Convert.ToInt32(arraylist_tp[i])).ToString() + "%", font1, brush1, new Point(225 - 125, 225 + 12)); } else if (tem_angle + Convert.ToSingle(arraylist_angle[i]) / 2 > 180 && tem_angle + Convert.ToSingle(arraylist_angle[i]) / 2 < 270) { halfangle = (tem_angle - 180 + Convert.ToSingle(arraylist_angle[i]) / 2); double tem_sin = Math.Sin(T / 180 * halfangle); double tem_cos = Math.Cos(T / 180 * halfangle); int y = Convert.ToInt32(125 * tem_sin); int x = Convert.ToInt32(125 * tem_cos); g.DrawString(arraylist_type[i].ToString(), font1, brush1, new Point(225 - x, 225 - y)); g.DrawString((Convert.ToInt32(arraylist_tp[i])).ToString() + "%", font1, brush1, new Point(225 - x, 225 - y + 12)); } else if (tem_angle + Convert.ToSingle(arraylist_angle[i]) / 2 == 270) { g.DrawString(arraylist_type[i].ToString(), font1, brush1, new Point(225, 225 - 125)); g.DrawString((Convert.ToInt32(arraylist_tp[i])).ToString() + "%", font1, brush1, new Point(225, 225 - 125 + 12)); } else if (tem_angle + Convert.ToSingle(arraylist_angle[i]) / 2 > 270 && tem_angle + Convert.ToSingle(arraylist_angle[i]) / 2 < 360) { halfangle = (360 - tem_angle - Convert.ToSingle(arraylist_angle[i]) / 2); double tem_sin = Math.Sin(T / 180 * halfangle); double tem_cos = Math.Cos(T / 180 * halfangle); int y = Convert.ToInt32(125 * tem_sin); int x = Convert.ToInt32(125 * tem_cos); g.DrawString(arraylist_type[i].ToString(), font1, brush1, new Point(225 + x, 225 - y)); g.DrawString((Convert.ToInt32(arraylist_tp[i])).ToString() + "%", font1, brush1, new Point(225 + x, 225 - y + 12)); } else if (tem_angle + Convert.ToSingle(arraylist_angle[i]) / 2 == 360) { g.DrawString(arraylist_type[i].ToString(), font1, brush1, new Point(225 + 125, 225)); g.DrawString((Convert.ToInt32(arraylist_tp[i])).ToString() + "%", font1, brush1, new Point(225 + 125, 225 + 12)); } } }         
       /*需要在界面上拉一个PictureBox控件*/ pictureBox1.Image
= bitmap; }

 

 

        /*动态取色的函数*/
        private Color Get_Color(int itemindex)
        {
            Color objColor = new Color();
            int remainder = itemindex % 5;
            switch (remainder)
            {
                case 0:
                    objColor = Color.FromArgb(57, 134, 155);
                break;

                case 1:
                    objColor = Color.FromArgb(70,161,185);
                break;

                case 2:
                    objColor = Color.FromArgb(124,187,207);
                break;

                case 3:
                    objColor = Color.FromArgb(181,212,224);
                break;

                case 4:
                    objColor = Color.FromArgb(200,230,245);
                break;

                default:
                    objColor = Color.Yellow;
                break;                 
            }
            return objColor;
        }

 

 

posted on 2013-01-16 16:05  guolebin7  阅读(5808)  评论(2编辑  收藏  举报