C# 画圆饼分析图

Code:

using System;
using System.Drawing;


    namespace aa    {
        public class Pie
        {
            private Graphics objGraphics; //Graphics 类提供将对象绘制到显示设备的方法
            private Bitmap objBitmap; //位图对象

            private int m_Width = 700; //图像宽度
            private int m_Height = 400; //图像高度
            private string m_Title = "";
            private string m_Unit = "百分比";
            //键
            private string[] m_Keys = new string[]{"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
            //值
            private float[] m_Values = new float[]{21.5f,36.2f,48.7f,55.4f,21.6f,12.8f,99.5f,36.4f,78.2f,56.4f,45.8f,66.4f};
            private Color m_BgColor = Color.Snow; //背景
            private Color m_TextColor = Color.Black; //文字颜色
            private Color m_BorderColor = Color.Black; //整体边框颜色
            //Pie颜色数组
            private Color[] m_PieColor = new Color[]{Color.Green,Color.Red,Color.Yellow,Color.Blue,Color.Orange,Color.Aqua,Color.SkyBlue,Color.DeepPink,Color.Azure,Color.Brown};

            public int Width
            {
                set
                {
                    if(value < 300)
                    {
                        m_Width = 300;
                    }
                    else
                    {
                        m_Width = value;
                    }
                }
                get
                {
                    return m_Width;
                }
            }

            public int Height
            {
                set
                {
                    if(value < 300)
                    {
                        m_Height = 300;
                    }
                    else
                    {
                        m_Height = value;
                    }
                }
                get
                {
                    return m_Height;
                }
            }

            public string Title
            {
                set
                {
                    m_Title  = value;
                }
                get
                {
                    return m_Title;
                }
            }

            public string Unit
            {
                set
                {
                    m_Unit = value;
                }
                get
                {
                    return m_Unit;
                }
            }

            public string[] Keys
            {
                set
                {
                    m_Keys = value;
                }
                get
                {
                    return m_Keys;
                }
            }

            public float[] Values
            {
                set
                {
                    m_Values = value;
                }
                get
                {
                    return m_Values;
                }
            }

            public Color BgColor
            {
                set
                {
                    m_BgColor = value;
                }
                get
                {
                    return m_BgColor;
                }
            }

            public Color TextColor
            {
                set
                {
                    m_TextColor = value;
                }
                get
                {
                    return m_TextColor;
                }
            }

            public Color BorderColor
            {
                set
                {
                    m_BorderColor = value;
                }
                get
                {
                    return m_BorderColor;
                }
            }

            public Color[] PieColor
            {
                set
                {
                    m_PieColor = value;
                }
                get
                {
                    return m_PieColor;
                }
            }

            private Color GetColor(int i) //分配各Pie的颜色
            {
                try
                {
                    if( 0 < i && i < PieColor.Length )
                    {
                        return PieColor[i];
                    }
                    else if( i >= PieColor.Length )
                    {
                        int j = i % PieColor.Length;
                        return PieColor[j];
                    }
                    else
                    {
                        return Color.Green;
                    }
                }
                catch
                {
                    return Color.Green;
                }
            }

            //生成图像并返回bmp图像对象
            public Bitmap CreateImage()
            {
                InitializeGraph();

                DrawRight(ref objGraphics);

                DrawContent(ref objGraphics);

                return objBitmap;
            }

            //初始化和填充图像区域,画出边框,初始标题
            private void InitializeGraph()
            {
            
                //根据给定的高度和宽度创建一个位图图像
                objBitmap = new Bitmap(Width,Height);

                //从指定的 objBitmap 对象创建 objGraphics 对象 (即在objBitmap对象中画图)
                objGraphics = Graphics.FromImage(objBitmap);

                //根据给定颜色(LightGray)填充图像的矩形区域 (背景)
                objGraphics.DrawRectangle(new Pen(BorderColor,1),0,0,Width,Height);
                objGraphics.FillRectangle(new SolidBrush(BgColor),1,1,Width-2,Height-2);

                //初始化标题
                CreateTitle(ref objGraphics);
            }

            //初始化右边说明部分
            private void DrawRight(ref Graphics objGraphics)
            {
                objGraphics.DrawString(String.Format("单位:{0}",Unit),new Font("宋体",10),new SolidBrush(TextColor),Width-95,30);

                Point KeysPoint = new Point(Width-120,50);
                Point KeysTextPoint = new Point(Width-90,50);
                for(int i=0;i < Keys.Length;i++)
                {
                    objGraphics.DrawRectangle(new Pen(BorderColor,1),KeysPoint.X,KeysPoint.Y,21,11);
                    objGraphics.FillRectangle(new SolidBrush(GetColor(i)),KeysPoint.X,KeysPoint.Y,20,10);
                    objGraphics.DrawString(String.Format("{0} {1}",Keys[i],Values[i]),new Font("宋体",10),new SolidBrush(TextColor),KeysTextPoint);
                    KeysPoint.Y +=15;
                    KeysTextPoint.Y += 15;
                }
            }

            private void DrawContent(ref Graphics objGraphics)
            {
                float Sum = 0;
                float StartAngle = 0;
                float SweepAngle = 0;
                float CircleHeight = 0;
                float CircleX = 0;
                float CircleY = 0;

                for(int i=0;i<Values.Length;i++)
                {
                    Sum += Values[i];
                }
                if(Width > Height)
                {
                    CircleHeight = Height - 100;
                }
                else
                {
                    CircleHeight = Width - 150;
                }
                CircleX = (Width - 150)/2 - CircleHeight/2;
                CircleY = Height - 50 - CircleHeight;
                for(int i=0;i<Values.Length;i++)
                {
                    SweepAngle = (Values[i]/Sum)*360;
                    objGraphics.DrawPie (new Pen(BorderColor,1),CircleX,CircleY,CircleHeight,CircleHeight,StartAngle,SweepAngle);
                    objGraphics.FillPie (new SolidBrush(GetColor(i)),CircleX,CircleY,CircleHeight,CircleHeight,StartAngle,SweepAngle);
                    StartAngle += SweepAngle;
                }
            }

            //初始化标题
            private void CreateTitle(ref Graphics objGraphics)
            {
                objGraphics.DrawString(Title,new Font("宋体",16),new SolidBrush(TextColor),new Point(5,5));
            }
        }
    }

 

 

 调用:

Pie pie = new Pie(); //定义一个画图类实例
            DataTable dt = ssih.GetList("1=1 order by dataDate desc");
            string[] kzKeys = {"看多","看平","看空"};
            pie.Keys = kzKeys;
            float[] kzValues = {float.Parse(Convert.ToString(Convert.ToDouble(dt.Rows[0]["zxyc1"].ToString())*100)),float.Parse(Convert.ToString(Convert.ToDouble(dt.Rows[0]["zxyc2"].ToString())*100)),float.Parse(Convert.ToString(Convert.ToDouble(dt.Rows[0]["zxyc3"].ToString())*100))};
            pie.Values = kzValues;
            Bitmap bmp = pie.CreateImage();
            bmp.Save(@"D:\work\cnqsq\Pie.jpg",ImageFormat.Jpeg);
//D:\work\cnqsq\Pie.jpg此目录要有asp_net写入的权限
 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值