博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

学习使用GDI+绘制饼状图

Posted on 2005-10-18 16:56  Leey  阅读(5264)  评论(2编辑  收藏  举报
早就申请了博客园的空间,但是一直也没有发过文章。
这次就算个开始吧

对于GDI+,我不是很熟悉,只是工作中用到了,才现找资料学习
这次画饼状图也是一样,Web页面中需要加入统计的饼状图,没办法。
虽然最终把图画出来了,但是图上却有一条虚线去不掉,到现在也搞不懂为什么。
如下图,是最终的效果。



在黄色区域中间有一条细细的虚线。
代码如下:
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace JRJC.Web.QuanZheng
{
    
/// <summary>
    
/// XChart 的摘要说明。
    
/// </summary>
    public class XChart
    {
        
/// <summary>
        
/// 根据传递的数组绘制饼图
        
/// </summary>
        
/// <param name="Width">宽度</param>
        
/// <param name="Height">高度</param>
        
/// <param name="dataArr">数据数组</param>
        
/// <param name="FileName">文件名称</param>
        public static void DrawPieByArrayList(int Width, int Height, int Magin, int Blank, DataAndColorPair[] arr, string FileName)
        {
            Bitmap bitmap 
= new Bitmap(Width, Height); //图片
            Graphics g = Graphics.FromImage(bitmap); //绘图
            g.DrawRectangle(new Pen(Color.White),0,0,Width,Height); //边框
            g.FillRectangle(new SolidBrush(Color.White), 11,Width - Magin, Height - Magin); 
            SolidBrush brush 
= new SolidBrush(Color.Blue); //画笔
            g.SmoothingMode = SmoothingMode.AntiAlias; //柔化
            
//g.DrawLine(new Pen(brush,10),1,1,10,10);
            Rectangle outline = new Rectangle(Blank,Blank,Width - Blank * 2, Height - Blank * 2); //饼图范围
            
            
//绘制饼图
            float total = 0;
            
float start = 0;
            
float angle = 0;
            
for(int i=0;i<arr.Length;i++)
            {
                total 
+= arr[i].Data;
            }
            
//Console.WriteLine(total.ToString());
            for(int i=0;i<arr.Length;i++)
            {
                angle 
= (float)(arr[i].Data/total) * 360;
                
//Console.WriteLine(angle.ToString());
                if(angle<=180)
                {
                    Draw3DPie(
ref g,arr[i].ShowColor,outline,start,angle);
                }
                
else//如果大于180度,则将大Pie拆开两块画
                {
                    
float acuteAngle=angle-180;
                    Draw3DPie(
ref g,arr[i].ShowColor,outline,start,acuteAngle);
                    Draw3DPie(
ref g,arr[i].ShowColor,outline,start+acuteAngle,angle-acuteAngle);
                }
                
//Draw3DPie(ref g,arr[i].ShowColor,outline,start,angle);
                start += angle;
            }
            bitmap.Save(FileName, ImageFormat.Gif);
        }
        
private static void Draw3DPie(ref Graphics g,Color brushColor,Rectangle outline,float start,float angle)
        {
            
//以下使用HatchBrush画阴影
            
//if((start <135 ) || (start == 180))
            if((start <135 ))
            {
                
//深度
                for(int iLoop2 = 0; iLoop2 < 10; iLoop2++)
                {
                    g.FillPie(
new HatchBrush(HatchStyle.Percent50,brushColor), 
                        outline.X, 
                        outline.Y 
+ iLoop2, 
                        outline.Width, 
                        outline.Height, 
                        start, 
                        angle);                    
                }
            }
            
if(start == 135)
            {
                
for(int iLoop2 = 0; iLoop2 < 10; iLoop2++)
                {
                    g.FillPie(
new HatchBrush(HatchStyle.Percent50,brushColor), 
                        outline.X 
- 30,
                        outline.Y 
+ iLoop2 + 15
                        outline.Width, 
                        outline.Height, 
                        start,
                        angle);
                }
                g.FillPie(
new SolidBrush(brushColor), 
                    outline.X 
- 30
                    outline.Y 
+ 15,
                    outline.Width, 
                    outline.Height, 
                    start, 
                    angle);
            } 
            
else //画pie
            {
                g.FillPie(
new SolidBrush(brushColor), outline.X, outline.Y, outline.Width,outline.Height, start, angle);
            }
            
//g.FillPie(new SolidBrush(arr[i].ShowColor),outline,start,angle);
        }
    }
}