using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace WindowsApplication1
{
   class DarwGrapClass
    {
       /// <summary>
       /// 绘制柱状统计图型的方法
       /// 作者：钱哨
       /// 时间：2009.6.1       
       /// </summary>
        /// <param name="width">图像的宽度：int类型</param>
        /// <param name="height">图像的高度：int类型</param>
        /// <param name="topic">图像标题的内容：string类型</param>
        /// <param name="beginy">y轴距图像左边界的宽度：int类型</param>
        /// <param name="partx">x轴将被等分多少份：int类型</param>
        /// <param name="beginx">x轴距图像顶部的高度：int类型</param>
        /// <param name="party">y轴将被等分多少份：int类型</param>
        /// <param name="xmess">X轴信息内容：string[]</param>
        /// <param name="ymess">y轴信息内容：string[]</param>
       /// <param name="county">某次Y轴的具体数组值：int[]</param>
       /// <param name="ymax">y轴的最大值</param>
        /// <param name="xmax">x轴的最大值</param>
        /// <returns>Bitmap类型的线性图像</returns>
 
       public Bitmap CreateZhuImage(int width, int height, string topic, int beginy, int partx, int beginx, int party,string[] xmess,string[] ymess,int[] county,int ymax,int xmax)
       {
           Bitmap image = new Bitmap(width, height);
           //创建Graphics类对象
           Graphics g = Graphics.FromImage(image);
           try
           {
               //清空图片背景色
               g.Clear(Color.White);
               Font font = new Font("Arial", 10, FontStyle.Regular);
               Font font1 = new Font("宋体", 15, FontStyle.Bold);
               LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),Color.Blue, Color.BlueViolet, 1.2f, true);
               //图片的底色
               g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
               //在图片上面写标题文字
               g.DrawString(topic, font1, brush, new PointF(width/4, height-20));
               //画图片的边框线
               g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);

               //================绘制线条================
               //----------------------------绘制坐标轴----------------------------
               Pen mypen = new Pen(brush, 1);
               Pen mypen1 = new Pen(Color.Blue, 2);
               //计算剩余的X轴长度(xlength)，并根据X轴等分原则计算每分的长度(xpart)
               int xlength = image.Width - beginy;
               int xpart = Convert.ToInt32(xlength / partx);
               //计算剩余的Y轴长度(ylength)，并根据Y轴等分原则计算每分的长度(ypart)
               int ylength = image.Height - beginx-40;
               int ypart = Convert.ToInt32(ylength / party);

               //----------------------------绘制纵向线条开始----------------------------
               int x = beginy;
               for (int i = 0; i <= partx; i++)
               {
                   g.DrawLine(mypen, x, beginx, x, beginx + ylength - 10);
                   x = x + xpart;
               }

               //绘制带箭头的直线
               //mypen1.StartCap = LineCap.ArrowAnchor;
               g.DrawLine(mypen1, beginy, beginx, beginy, beginx + ylength-10);//所以真实的Y轴长度是(ylength)
               //----------------------------绘制纵向线条结束----------------------------

               //----------------------------绘制横向线条开始----------------------------
               int y = beginx-5;
               for (int i = 0; i <= party; i++)
               {
                   g.DrawLine(mypen, beginy, y, beginy + xlength, y);
                   y = y + ypart;
               }
               g.DrawLine(mypen1, beginy, beginx + ylength - 10, beginy + xlength, beginx + ylength-10);
               //----------------------------绘制横向线条结束----------------------------
              
               //================绘制X,Y轴的坐标内容================
               //x轴信息
               int x1 = beginy;
               for (int i = 0; i < partx; i++)
               {
                   g.DrawString(xmess[i].ToString(), font, Brushes.Blue, x1, beginx + ylength); //设置文字内容及输出位置
                   x1 = x1 + xpart;
               }
               //y轴
               int y1 = beginx + ypart;
               for (int i = 0; i < party; i++)
               {
                   g.DrawString(ymess[i].ToString(), font, Brushes.Blue, 1, y1); //设置文字内容及输出位置
                   y1 = y1 + ypart;
               }
               //================根据传过来的数组中的值（数目与X轴个数相等，但是Y值被放置在数组中）================
               //绘制柱状图.
               x = 80;
               Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
               SolidBrush mybrush = new SolidBrush(Color.Red);
               //SolidBrush mybrush2 = new SolidBrush(Color.Green);

               for (int i = 0; i < county.Length; i++)
               {
                   //第i期
                   int part1 = Convert.ToInt32(((decimal)Convert.ToInt32(county[i]) / ymax) * ylength);
                   g.FillRectangle(mybrush, beginy, ylength - part1, xpart / 2, part1);
                   //此处请注意：求小数时候，分母必须为decimal类型，否则结果一律为0；
                   g.DrawString(county[i].ToString(), font2, Brushes.GreenYellow, beginy, ylength - part1);
                   beginy += xpart;
                   //此处注意：最后使得补偿增加为X轴的宽度
               }

               return image;
           }
           catch
           {
               return image;
           }
       }
       /// <summary>
       /// 绘制折线统计图型的方法
       /// 作者：钱哨
       /// 时间：2009.6.1       
       /// </summary>
       /// <param name="width">图像的宽度：int类型</param>
       /// <param name="height">图像的高度：int类型</param>
       /// <param name="topic">图像标题的内容：string类型</param>
       /// <param name="beginy">y轴距图像左边界的宽度：int类型</param>
       /// <param name="partx">x轴将被等分多少份：int类型</param>
       /// <param name="beginx">x轴距图像顶部的高度：int类型</param>
       /// <param name="party">y轴将被等分多少份：int类型</param>
       /// <param name="xmess">X轴信息内容：string[]</param>
       /// <param name="ymess">y轴信息内容：string[]</param>
       /// <param name="county">某次Y轴的具体数组值：int[]</param>
       /// <param name="ymax">y轴的最大值</param>
       /// <param name="xmax">x轴的最大值</param>
       /// <returns>Bitmap类型的线性图像</returns>

       public Bitmap CreateLinesImage(int width, int height, string topic, int beginy, int partx, int beginx, int party, string[] xmess, string[] ymess, int[] county, int ymax, int xmax)
       {
           Bitmap image = new Bitmap(width, height);
           //创建Graphics类对象
           Graphics g = Graphics.FromImage(image);
           try
           {
               //清空图片背景色
               g.Clear(Color.White);
               Font font = new Font("Arial", 10, FontStyle.Regular);
               Font font1 = new Font("宋体", 15, FontStyle.Bold);
               LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.BlueViolet, 1.2f, true);
               //图片的底色
               g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
               //在图片上面写标题文字
               g.DrawString(topic, font1, brush, new PointF(width / 4, height - 20));
               //画图片的边框线
               g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);

               //================绘制线条================
               //----------------------------绘制坐标轴----------------------------
               Pen mypen = new Pen(brush, 1);
               Pen mypen1 = new Pen(Color.Blue, 2);
               Pen mypen2 = new Pen(Color.Red, 2);
               //计算剩余的X轴长度(xlength)，并根据X轴等分原则计算每分的长度(xpart)
               int xlength = image.Width - beginy;
               int xpart = Convert.ToInt32(xlength / partx);
               //计算剩余的Y轴长度(ylength)，并根据Y轴等分原则计算每分的长度(ypart)
               int ylength = image.Height - beginx - 40;
               int ypart = Convert.ToInt32(ylength / party);

               //----------------------------绘制纵向线条开始----------------------------
               int x = beginy;
               for (int i = 0; i <= partx; i++)
               {
                   g.DrawLine(mypen, x, beginx, x, beginx + ylength - 10);
                   x = x + xpart;
               }

               //绘制带箭头的直线
               //mypen1.StartCap = LineCap.ArrowAnchor;
               g.DrawLine(mypen1, beginy, beginx, beginy, beginx + ylength - 10);//所以真实的Y轴长度是(ylength)
               //----------------------------绘制纵向线条结束----------------------------

               //----------------------------绘制横向线条开始----------------------------
               int y = beginx - 5;
               for (int i = 0; i <= party; i++)
               {
                   g.DrawLine(mypen, beginy, y, beginy + xlength, y);
                   y = y + ypart;
               }
               g.DrawLine(mypen1, beginy, beginx + ylength - 10, beginy + xlength, beginx + ylength - 10);
               //----------------------------绘制横向线条结束----------------------------

               //================绘制X,Y轴的坐标内容================
               //x轴信息
               int x1 = beginy;
               for (int i = 0; i < partx; i++)
               {
                   g.DrawString(xmess[i].ToString(), font, Brushes.Blue, x1, beginx + ylength); //设置文字内容及输出位置
                   x1 = x1 + xpart;
               }
               //y轴
               int y1 = beginx + ypart;
               for (int i = 0; i < party; i++)
               {
                   g.DrawString(ymess[i].ToString(), font, Brushes.Blue, 1, y1); //设置文字内容及输出位置
                   y1 = y1 + ypart;
               }
               //================根据传过来的数组中的值（数目与X轴个数相等，但是Y值被放置在数组中）================
               //绘制柱状图.
               x = 80;
               Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
               SolidBrush mybrush = new SolidBrush(Color.Red);
               //SolidBrush mybrush2 = new SolidBrush(Color.Green);

               Point[] point1=new Point[county.Length+1];
               int x2 = beginy;
               for (int i = 0; i < county.Length+1; i++)
               {
                   if (i == 0)
                   {
                       point1[i].X = beginy; point1[i].Y = beginx + ylength - 10; //从106纵坐标开始, 到(0, 0)坐标时
                   }
                   else
                   {
                       //第i期
                       int part1 = Convert.ToInt32(((decimal)Convert.ToInt32(county[i-1]) / ymax) * ylength);
                       //获取若干的像素点
                       point1[i].X = x2; point1[i].Y = ylength - part1; //从(0, 0)纵坐标开始绘制
                       //开始书写文字
                       g.DrawString(Convert.ToInt32(county[i - 1]).ToString()+"人", font2, brush, point1[i]);
                       //此处注意：最后使得补偿增加为X轴的宽度
                       x2 = x2 + xpart;
                   }
               }
               g.DrawLines(mypen2, point1);
                   
               return image;
           }
           catch
           {
               return image;
           }
       }

       /// <summary>
       /// 绘制饼形统计图型的方法
       /// 作者：钱哨
       /// 时间：2009.6.1       
       /// </summary>
       /// <param name="width">图像的宽度：int类型</param>
       /// <param name="height">图像的高度：int类型</param>
       /// <param name="topic">图像标题的内容：string类型</param>
       /// <param name="xmess">饼形图基本信息内容：string[]</param>
       /// <returns>Bitmap类型的线性图像</returns>

       public Bitmap CreatePieImage(int width, int height, string topic,int[] county,string[] xmess)
       {
           Bitmap image = new Bitmap(width, height);
           //创建Graphics类对象
           Graphics g = Graphics.FromImage(image);
           try
           {
               //清空图片背景色
               g.Clear(Color.White);
               //设置字体，fonttitle为主标题的字体
               Font fontlegend = new Font("verdana", 9, FontStyle.Regular);
               Font fonttitle = new Font("宋体", 13, FontStyle.Bold);
               LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.BlueViolet, 1.2f, true);
               //图片的底色
               g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
               //画图片的边框线
               g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
               //计算饼形图的宽和高，设计时候考虑为宽度大于高度，横行显示。目的是在饼形图右侧写文字。
               int pieheight = height-10;//饼图直径
               int fontwidth = width - height;//文字区域宽度
               int fonty = 10;//文字区域左上角Y坐标
               //计算数组的和，得到饼形的总和
               int count = 0;
               for(int i=0;i<county.Length;i++)
               {
                   count += Convert.ToInt32(county[i]);
               }
               //绘制饼形图背景色
               SolidBrush brush1=new SolidBrush(Color.Beige);
               //饼形图边框颜色
               Pen pen1=new Pen(Color.Blue,1);
               Rectangle picrect=new Rectangle(5, 5, pieheight, pieheight);
               g.FillRectangle(brush1, picrect);
               g.DrawRectangle(pen1, picrect);
               //加上各种随机色
               ArrayList colors = new ArrayList();
               Random rnd = new Random();
               for (int i = 0; i < county.Length; i++)
               { 
                  colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));
               } 
               //开始绘制饼形图
               SolidBrush blackbrush = new SolidBrush(Color.Black);
               SolidBrush bluebrush = new SolidBrush(Color.Blue);

               Single currentdegree = 0.0f;
               int pieheight1 = pieheight+10;
               int fonty1 = fonty + 30;
               for (int j = 0; j < county.Length; j++)
               {
                   g.FillPie((SolidBrush)colors[j], picrect, currentdegree, Convert.ToSingle(county[j]) / count * 360);
                   currentdegree += Convert.ToSingle(county[j]) / count * 360;
                   //写注释
                   g.DrawString(Convert.ToString(xmess[j]) + "：" + Convert.ToString(Convert.ToSingle(county[j])), fontlegend, blackbrush, pieheight1, fonty1);
                   g.FillRectangle((SolidBrush)colors[j], pieheight1+130, fonty1, 50, 10);
                   fonty1 = fonty1 + fontlegend.Height * 3;
               }
               //以下为生成主标题
               topic = topic + "\n\n\n";
               StringFormat stringFormat = new StringFormat();
               g.DrawString(topic, fonttitle, blackbrush, new Rectangle(pieheight+10, 10, width, height), stringFormat);

               return image;
           }
           catch
           {
               return image;
           }
       }
    }
}
