关闭

C#.net编写的图表生成类

914人阅读 评论(1) 收藏 举报

using System;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace WebUI
{
 /// <summary>
 /// ChartBrush 的摘要说明。
 /// </summary>
 public class ChartBrush
 {
  private int map_width;//图表高度
  private int map_height;//图表宽度
  private string map_name;//图表名称
  private string group_field;//分组字段
  private string[] sum_field;//求和字段
  private string group_name;//分组名
  private string[] sum_name;//值名
  
  //
  #region ///构造函数
  public ChartBrush(int Width,int Hight)
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
   map_width=Width;
   map_height=Hight;
   Initialization();
  }
  public ChartBrush()
  {
   map_width=600;
   map_height=400;
   Initialization();
  }
  #endregion
  //
  //
  private void Initialization()
  {
   map_name="报表标题";
  }
  //
  #region *******属性***************
  /// <summary>
  /// 图表的宽度
  /// </summary>
  public int Map_Width
  {
   get
   {
    return map_width;
   }
   set
   {
    map_width=value;
   }
  }
  /// <summary>
  /// 图表高度
  /// </summary>
  public int Map_Height
  {
   get
   {
    return map_height;
   }
   set
   {
    map_height=value;
   }
  }
  /// <summary>
  /// 图表名称
  /// </summary>
  public string Map_Name
  {
   get
   {
    return map_name;
   }
   set
   {
    map_name=value;
   }
  }
  /// <summary>
  /// 分组字段名,根据Group_Field字段进行分组
  /// </summary>
  public string Group_Field
  {
   set
   {
    group_field=value;
   }
  }
  /// <summary>
  /// 求和字段名,根据Sum_Field字段进行求和
  /// </summary>
  public string[] Sum_Field
  {
   set
   {
    sum_field=value;
   }
  }
  /// <summary>
  /// 组标题,设置图表组标题
  /// </summary>
  public string Group_Name
  {
   set
   {
    group_name=value;
   }
  }
  /// <summary>
  /// 数据标题,设置图表数据标题
  /// </summary>
  public string[] Sum_Name
  {
   set
   {
    sum_name=value;
   }
  }
 
  #endregion
  //
  /// <summary>
  /// 生成平面柱状视图
  /// </summary>
  /// <param name="dt"></param>
  private void DrawCharthistogram(DataTable dt)
  {
   #region 基本参数
   int ystat=30;
   int linewidth=8;
   int hemline=30;
   int upside=50;
   int starboard=30;
   Brush[] brush=new Brush[5];
   brush[0]=Brushes.Red;
   brush[1]=Brushes.Blue;
   brush[2]=Brushes.Yellow;
   brush[3]=Brushes.Green;
   brush[4]=Brushes.DarkCyan;
   #endregion
   //
   Bitmap map=new Bitmap(this.map_width,this.map_height);
   //
   #region 绘制标题
   Graphics g=Graphics.FromImage(map);
   g.Clear(Color.White);
   g.DrawRectangle(new Pen(Brushes.BurlyWood),0,0,map_width-5,map_height-5);
   //绘制标题
   g.DrawString(this.map_name,new Font("宋体",14),Brushes.Black,new Point(50,3));
   //g.DrawString(sum_name,new Font("宋体",10),Brushes.Red,new Point(0,this.map_height/2));
   g.DrawString(group_name,new Font("宋体",10),Brushes.Red,new Point(map_width/2-10,map_height-15));
   //绘制X轴
   g.DrawLine(new Pen(Brushes.Blue),new Point(ystat,map_height-hemline),new Point(map_width-ystat-starboard,map_height-hemline));
   //绘制y轴
   g.DrawLine(new Pen(Brushes.Blue),new Point(ystat,upside),new Point(ystat,map_height-hemline));
   string tablename=dt.TableName;
   //string temp="select "+this.group_field+",sum("+this.sum_field+") "+" from "+tablename+ " group by "+this.group_field+";";
   #endregion
   //
   DataRow[] drs=dt.Select();
   int count=drs.Length;
   int Wscale=(map_width-ystat-starboard)/count;
   //
   #region   计算绘制图形坐标值
   double maxvalue=0;
   for(int i=0;i<count;i++)
   {
    for(int j=0;j<sum_field.Length;j++)
    {
     if(i==0&&j==0)
     {
      maxvalue=Double.Parse(drs[0][sum_field[0]].ToString());
     }
     else
     {
      if(Double.Parse(drs[i][sum_field[j]].ToString())>maxvalue)
      {
       maxvalue=Double.Parse(drs[i][sum_field[j]].ToString());
      }
     }
    }
   }
   double Hscale=maxvalue/(map_height-hemline-upside);
   int graduation=(map_height-hemline-upside)/5;
   int factgraduation=(int)(maxvalue/5);
   #endregion
   //绘制 图形
   #region *************
   g.DrawString("0",new Font("宋体",6),Brushes.Red,new Point(ystat-15,map_height-hemline));
   for(int i=0;i<5;i++)
   {
    g.DrawString((factgraduation*(i+1)).ToString(),new Font("宋体",8),Brushes.Red,new Point(ystat-15,map_height-hemline-graduation*(i+1)));
    g.DrawLine(new Pen(Brushes.Blue),new Point(ystat,map_height-hemline-graduation*(i+1)),new Point(ystat+5,map_height-hemline-graduation*(i+1)));
   }
   //
   int[] yvalue=new int[sum_field.Length];
   int[] xvalue=new int[sum_field.Length];
   for(int i=0;i<count;i++)
   {
    //得到左上角y坐标
    for(int j=0;j<sum_field.Length;j++)
    {
     yvalue[j]=(int)(Double.Parse(drs[i][sum_field[j]].ToString())/Hscale);
    
     //yvalue=map_height-18-yvalue;
     //
     if(i==0)
     {
      //xvalue=(Wscale-hemline)/2+ystat;
      if(j==0)
      {
       xvalue[j]=(int)(Wscale/2-(sum_field.Length*linewidth+(sum_field.Length-1)*2.5)+ystat);
      }
      else
      {
       xvalue[j]=xvalue[j-1]+linewidth*2+5;
      }
     }
     else
     {
      if(j==0)
      {
       xvalue[j]=(int)(Wscale/2-(sum_field.Length*linewidth+(sum_field.Length-1)*2.5)+ystat+Wscale*i);
      }
      else
      {
       xvalue[j]=xvalue[j-1]+linewidth*2+5;
      }
      //xvalue+=Wscale;
     }
   
    }
    int m=0;
    //绘制方块
    for(int k=0;k<sum_field.Length;k++)
    {
     if(k<5)
     {
      m=k;
     }
     else
     {
      m=0;
     }
     g.FillRectangle(brush[m],new Rectangle(xvalue[k]-linewidth,map_height-hemline-yvalue[k],linewidth*2,yvalue[k]));
     //g.DrawString(sum_name[k],new Font("宋体",8),Brushes.Black,map_width-30,
     g.DrawString(sum_name[k]+" "+drs[i][sum_field[k]].ToString(),new Font("宋体",8),Brushes.Green,new Point(xvalue[k]-linewidth*2,map_height-hemline-yvalue[k]-10));
     //g.DrawRectangle(new Pen(Brushes.Green),xvalue-linewidth,map_height-hemline-yvalue,linewidth*2,yvalue);
     
    }
    g.DrawString(drs[i][group_field].ToString(),new Font("宋体",9),Brushes.Red,new Point(xvalue[0]-5,map_height-hemline+2));
   }
   #endregion
   //
   map.Save(System.Web.HttpContext.Current.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);

   //
  }
  //
  /// <summary>
  /// 生成平面饼图
  /// </summary>
  /// <param name="dt"></param>
  private void DrawChartPie(DataTable dt)
  {
   //上边界
   int upsize=50;
   int hemline=50;
   int xcenter=map_width/2;
   int ycenter=(map_height-upsize-hemline)/2+upsize;
   int xpiesize=300;
   int ypiesize=280;
   int X=xcenter-xpiesize/2-50;
   int Y=ycenter-ypiesize/2;
   Brush[] brush=new Brush[5];
   brush[0]=Brushes.Red;
   brush[1]=Brushes.Blue;
   brush[2]=Brushes.Yellow;
   brush[3]=Brushes.Green;
   brush[4]=Brushes.DarkCyan;
   //
   Bitmap map=new Bitmap(this.map_width,this.map_height);
   Graphics g=Graphics.FromImage(map);
   g.Clear(Color.White);
   //计算饼图各个部分所站的角度
   try
   {
    DataRow drs=dt.Rows[0];
    int count=sum_field.Length;
   
   double sumvalue=0;
   for(int i=0;i<count;i++)
   {
    sumvalue+=Double.Parse(drs[this.sum_field[i]].ToString());
   }
   int[] angle=new int[count+1];
   angle[0]=0;
   for(int i=1;i<count;i++)
   {
    
    angle[i]=(int)(Double.Parse(drs[sum_field[i-1]].ToString())/sumvalue*360);
    
   }
    int sumangle=0;
    for(int i=0;i<count;i++)
    {
     sumangle+=angle[i];
    }
   angle[count]=360-sumangle;

   //开始绘制
   g.DrawString(map_name,new Font("宋体",14),Brushes.Black,new Point(20,15));
   g.DrawRectangle(new Pen(Brushes.BurlyWood),0,0,map_width-5,map_height-5);
   for(int i=0;i<count;i++)
   {
    if(i==0)
    {
     g.DrawPie(new Pen(Brushes.Red),X,Y,xpiesize,ypiesize,angle[i],angle[i+1]);
     g.FillPie(brush[i],X,Y,xpiesize,ypiesize,angle[i],angle[i+1]);
    }
    else
    {
     g.DrawPie(new Pen(Brushes.Red),X,Y,xpiesize,ypiesize,angle[i]+angle[i-1],angle[i+1]);
     g.FillPie(brush[i],X,Y,xpiesize,ypiesize,angle[i]+angle[i-1],angle[i+1]);
    }
    //
    g.DrawString(sum_name[i]+":"+drs[sum_field[i]].ToString(),new Font("宋体",9),Brushes.Black,new Point(map_width-120,100+i*20));
    g.FillRectangle(brush[i],new Rectangle(map_width-30,100+i*20,15,15));
    //g.DrawString(sum_name[i]+drs[sum_field[i]].ToString(),new Font("宋体",9),Brushes.Black,new Point(180,(map_height-hemline)+i*20));
   }
   map.Save(System.Web.HttpContext.Current.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
   }
   catch
   {
   }
  }
  //
  //
  //
  /// <summary>
  /// 生成PS曲线图
  /// </summary>
  /// <param name="dt"></param>
  private void DrawChartPS(DataTable dt)
  {
   #region 基本参数
   int ystat=30;
   //int linewidth=8;
   int hemline=30;
   int upside=50;
   int starboard=30;
   Brush[] brush=new Brush[5];
   brush[0]=Brushes.Red;
   brush[1]=Brushes.Blue;
   brush[2]=Brushes.Yellow;
   brush[3]=Brushes.Green;
   brush[4]=Brushes.DarkCyan;
   #endregion
   //
   Bitmap map=new Bitmap(this.map_width,this.map_height);
   //
   #region 绘制标题
   Graphics g=Graphics.FromImage(map);
   g.Clear(Color.White);
   g.DrawRectangle(new Pen(Brushes.BurlyWood),0,0,map_width-1,map_height-1);
   //绘制标题
   g.DrawString(this.map_name,new Font("宋体",14),Brushes.Black,new Point(50,3));
   //g.DrawString(sum_name,new Font("宋体",10),Brushes.Red,new Point(0,this.map_height/2));
   g.DrawString(group_name,new Font("宋体",10),Brushes.Red,new Point(map_width/2-10,map_height-15));
   //绘制X轴
   g.DrawLine(new Pen(Brushes.Blue),new Point(ystat,map_height-hemline),new Point(map_width-ystat-starboard,map_height-hemline));
   //绘制y轴
   g.DrawLine(new Pen(Brushes.Blue),new Point(ystat,upside),new Point(ystat,map_height-hemline));
   //string tablename=dt.TableName;
   //string temp="select "+this.group_field+",sum("+this.sum_field+") "+" from "+tablename+ " group by "+this.group_field+";";
   #endregion
   //
   DataRow[] drs=dt.Select();
   int count=drs.Length;
   int Wscale=(map_width-ystat-starboard)/count;
   //
   #region   ///计算绘制图形坐标值
   double maxvalue=0;
   for(int i=0;i<count;i++)
   {
    for(int j=0;j<sum_field.Length;j++)
    {
     if(i==0&&j==0)
     {
      maxvalue=Double.Parse(drs[0][sum_field[0]].ToString());
     }
     else
     {
      if(Double.Parse(drs[i][sum_field[j]].ToString())>maxvalue)
      {
       maxvalue=Double.Parse(drs[i][sum_field[j]].ToString());
      }
     }
    }
   }
   double Hscale=(map_height-hemline-upside)/maxvalue;
   //刻度
   int graduation=(map_height-hemline-upside)/5;
   int factgraduation=(int)(maxvalue/5);
   #endregion
   //绘制 图形
   #region *************
   g.DrawString("0",new Font("宋体",6),Brushes.Red,new Point(ystat-15,map_height-hemline));
   for(int i=0;i<5;i++)
   {
    g.DrawString((factgraduation*(i+1)).ToString(),new Font("宋体",8),Brushes.Red,new Point(ystat-15,map_height-hemline-graduation*(i+1)));
    g.DrawLine(new Pen(Brushes.Blue),new Point(ystat,map_height-hemline-graduation*(i+1)),new Point(ystat+5,map_height-hemline-graduation*(i+1)));
   }
   //
   int[] yvalue=new int[sum_field.Length];
   int[] xvalue=new int[sum_field.Length];
   //
   int k=0;
   for(int i=0;i<this.sum_field.Length;i++)
   {
    int xbegin=0;
    int ybegin=0;
    int xend=0;
    int yend=0;
    
    for(int j=0;j<count-1;j++)
    {
     if(k>=5)
     {
      k=0;
     }
     if(j==0)
     {
      xbegin=ystat+Wscale/2;
      ybegin=map_height-(int)(Double.Parse(drs[j][sum_field[i]].ToString())*Hscale)-ystat;
      xend=xbegin+Wscale;
      yend=map_height-(int)(Double.Parse(drs[j][sum_field[i]].ToString())*Hscale)-ystat;
     }
     else
     {
      xbegin=xend;
      ybegin=yend;
      xend=xbegin+Wscale;
      yend=map_height-(int)(Double.Parse(drs[j+1][this.sum_field[i]].ToString())*Hscale)-ystat;
     }
     if(i==0)
     {
      g.DrawString(drs[j][group_field].ToString(),new Font("宋体",9),Brushes.Red,(float)xbegin,(float)(map_height-hemline));
      if(j==count-2)
      {
       g.DrawString(drs[j+1][group_field].ToString(),new Font("宋体",9),Brushes.Red,(float)xend,(float)(map_height-hemline));
      }
     }
     if(j==count-2)
     {
      g.DrawString(drs[j][sum_field[i]].ToString(),new Font("宋体",8),brush[k],xbegin,ybegin-9); 
      g.DrawString(drs[j+1][sum_field[i]].ToString(),new Font("宋体",8),brush[k],xend,yend-9);
      
     }
     else
     {
      g.DrawString(drs[j][sum_field[i]].ToString(),new Font("宋体",8),brush[k],xbegin,ybegin-9);
     }
     
     g.DrawLine(new Pen(brush[k]),new Point(xbegin,ybegin),new Point(xend,yend));
    }
    k++;
   }
   //
   #endregion
   //
   map.Save(System.Web.HttpContext.Current.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);

   //
  }
  public void DrawChart(DataTable dt,ChartTypes.TypeSelect cty)
  {
   DataTable datatable=dt;
   switch(cty)
   {
    case ChartTypes.TypeSelect.N_Histogram:
     //
     DrawCharthistogram(datatable);
     //
     break;
    case ChartTypes.TypeSelect.N_Pie:
     //
     DrawChartPie(datatable);
     //
     break;
    case ChartTypes.TypeSelect.N_PSCurve:
     //
     DrawChartPS(datatable);
     //
     break;

   }
  }
  //
 }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:57593次
    • 积分:1046
    • 等级:
    • 排名:千里之外
    • 原创:41篇
    • 转载:10篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论