热门文章 | 热门软件| 热门源码 | 热门电影 | 知识库 | 联系我们
软件 源码 教程 影视 健康 招聘
  HTML | JavaScript | ASP | PHP | JSP | NET | VB | VC | VF | Windows | Linux | Mysql | Mssql | Oracle | Struts 
当前位置: 创世纪计算机资源网 -> 文章频道 ->vc 
站内搜索:
用C#创建PDA应用程序的柱形图控件(4)
作者:佚名 来源:天极开发 整理日期:2007-10-19

#region Component Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the Code Editor.
///

private void InitializeComponent()
{
 this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
 this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
 this.hScrollBar1.Location = new System.Drawing.Point(1, 100);
 this.hScrollBar1.Maximum = 1;
 this.hScrollBar1.LargeChange = 1;
 this.hScrollBar1.Size = new System.Drawing.Size(100, 16);
 this.hScrollBar1.ValueChanged += new System.EventHandler(this.hScrollBar1_ValueChanged);

 this.Controls.Add(this.hScrollBar1);
}
#endregion
//
// private ArrayList mCudeData;

protected override void OnResize(EventArgs e)
{
 //this.Refresh();
}

Graphics mGraphics;
Pen mBlackPen=new Pen(Color.Black);
//偏差
int TopHeightWarp=16; //顶偏差(文本高)

int LeftWidthWarp=0; //左偏差(最大数据文本宽)
int UnderHeightWarp=10; //底偏差(底文本高)
int BetweenLineHeight=10;//水平线的高
int LineCount=10;//水平线数

// //This Paint function uses routines common to both platforms.

int ClientHeight;
int mWidth;
int YHeight;
Rectangle rcClient;
System.Drawing.Region Region1;
public void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
 //base.Paint(null,e);
 mGraphics=e.Graphics;
 //读取数据
 this.rcClient = this.ClientRectangle;
 Region1=new Region ( this.rcClient);
 Region1=Region1.Clone();

 ClientHeight=rcClient.Height;
 objGraph.mGraphics=e.Graphics; //mGraphics.MeasureString//

 //计算最大的x轴、y轴坐标
 CountMaxScaleXScaleY();
 UnderHeightWarp=(int)objGraph.mGraphics.MeasureString("文本",this.mTextFont).Height+5;
 UnderHeightWarp+=this.hScrollBar1.Height;
 //作图的范围区(开始点、长、宽、高)
 mBeginPoint.X =this.ClientRectangle.X+LeftWidthWarp ;
 mBeginPoint.Y =this.ClientRectangle.Y+rcClient.Height-UnderHeightWarp ;

 //写标题
 DrawTitle(rcClient);
 int Width=rcClient.Width-this.mLenght-LeftWidthWarp;
 mWidth=Width;
 int Height=rcClient.Height-this.mLenght-TopHeightWarp-UnderHeightWarp;
 this.YHeight= Height;
 int Lenght=this.mLenght;

 //设置流动条
 // this.hScrollBar1.Location = new System.Drawing.Point(0, rcClient.Y+rcClient.Height);
 InitPage();
 if (this.hScrollBarVisible)
 {
  this.hScrollBar1.Left=0;
  this.hScrollBar1.Top=rcClient.Height-this.hScrollBar1.Height ;
  this.hScrollBar1.Maximum=this.PageCount-1;

  if(rcClient.Width <246)
   this.hScrollBar1.Width= rcClient.Width;
  else
   this.hScrollBar1.Width=246;
 }
 else
 {
  this.hScrollBar1.Left=0;
  this.hScrollBar1.Top=0 ;
 }
 //从此分别画图
 if (this.mMaxYValue> 10)
 {
  //水平网络线
  this.BetweenLineHeight=(int)objGraph.mGraphics.MeasureString("文本",this.mTextFont).Height;
  this.LineCount=Height/BetweenLineHeight;
 }
 else
 {
  this.BetweenLineHeight=Height/10;
  //线数应该是能整
  this.LineCount=10;
 }
 //画与它父相同背景颜色的区域,以隐藏角边
 Color ParentBackColor=Color.Brown;

 objGraph.DrawRectangle( ParentBackColor,new Point(0,0),rcClient.Width,rcClient.Height);
 e.Graphics.DrawRectangle(new Pen(ParentBackColor), rcClient);

 objGraph.DrawPDAChart(this.mGridLineColor, this.mAxesXColor,this.mAxesYColor,_
 this.mBackColor,mBeginPoint,Lenght,Width,Height, this.mShowXScale,this.mShowYScale);
 //画线和左文本(内部左下点)
 Point p=mBeginPoint;
 p.X+=this.mLenght;
 p.Y-=this.mLenght;
 DrawGridLineAndTexts(p,Width);
 //mBeginPoint

 //画矩形与写文本
 //CreateCubes(mBeginPoint,Width,rcClient.Height);
 //mBeginPoint.X+=10;
 CreatePageCubes(mBeginPoint,Width,ClientHeight);

 DrawTitle(rcClient);
}
}

//以左下坐标p,颜色color,长Lenght,宽Width,高Height,x轴文本textX,画立体图
public void AddOneCube(string textX,Point p,Color color,int Lenght,int Width, int Height)
{
 try
 {
  objGraph.DrawCube (color,p,Lenght,Width,Height);
  //文本
  int txtWidth=(int)objGraph.mGraphics.MeasureString(textX,mTextFont).Width;
  int txtHeight=(int)objGraph.mGraphics.MeasureString(textX,mTextFont).Height;

  int x=(p.X+Width/2)-txtWidth/2;
  int y=p.Y+txtHeight/2;
  this.objGraph.DrawText(textX,Color.Black,this.mTextFont,x,y-5);
 }
 catch(Exception ex)
 {
  string str=ex.Message;
 }}

//一页立方体图形个数
int OnePageCubeCount=10;
int CurrentPage=0;//当前页
int PageCount=0;//多少页
//水平轴的相对值
int XScale;
//Y轴的相对值
double YScale=0.2;

Color[] color={Color.Red,Color.Blue,Color.Green,Color.Yellow,Color.YellowGreen,Color
.Magenta,_
Color.Cyan,Color.Coral,Color.SlateGray,Color.Pink,Color.Crimson,Color
.DodgerBlue,Color.Chartreuse };

//计算页
private void InitPage()
{
 if(this.OnePageCubeCount==0) return;
 if (mDataTable.Rows.Count   this.OnePageCubeCount=mDataTable.Rows.Count ;
 if(this.OnePageCubeCount==0) return;
  PageCount=mDataTable.Rows.Count/this.OnePageCubeCount;
 //水平轴的相对值
 XScale=Width/this.OnePageCubeCount ;
 //Y轴的相对值
 if(this.mMaxYValue <=0) return;
 {
  if(this.mMaxYValue==0) return;
  this.YScale=double.Parse(this.YHeight.ToString())/double.Parse( this.mMaxYValue.ToString() ) ;//System.Math.
  //this.YScale=double.Parse(this.YScale.ToString())/double.Parse(this.LineCount.ToString() );
 }
 // this.YScale=double.Parse(System.Convert.ToString(1))/double.Parse( this.mMaxYValue.ToString() ) ;//System.Math.
}

private void hScrollBar1_ValueChanged(object sender, System.EventArgs e)
{
 //OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
 //清画出的图
 this.CurrentPage=hScrollBar1.Value;
 // if (mGraphics.Clip==null)
 // {
  mGraphics=this.CreateGraphics ();
  this.objGraph.mGraphics=mGraphics;
 // }
 //mGraphics.Clip=this.Region1;
 //画矩形与写文本,最多一屏
 //mGraphics.Clear(this.mBackColor) ;
 //mGraphics.ResetClip();
 //CreatePageCubes(mBeginPoint,Width,ClientHeight);
 System.Windows.Forms.PaintEventArgs e1=new PaintEventArgs( mGraphics,this.rcClient);
 OnPaint(null,e1);
}
public void NextPage()
{
 this.objGraph.mGraphics=this.CreateGraphics ();
 this.CurrentPage++;
 Bitmap bm = new Bitmap(10,10);
 Graphics g = Graphics.FromImage(bm);

 if (this.CurrentPage> this.PageCount)
  this.CurrentPage--;
 //画矩形与写文本,最多一屏
 //mGraphics.Clear(Color.Red) ;
 //mGraphics.ResetClip();
 CreatePageCubes(mBeginPoint,Width,ClientHeight);
}

//在左下顶点,宽Width,高Height建立立方体
private void CreatePageCubes(Point BeginP ,int Width,int Height)
{
 if(mDataTable.Rows.Count==0) return;
 int Between=10;
 switch(this.OnePageCubeCount)
 {
  case 1:
   Between= mWidth/2;
   break;
  case 2:
   Between= mWidth/3;
   break;
  case 3:
   Between= mWidth/4;
   break;
  case 4:
   Between= mWidth/5;
   break;
  case 5:
   Between= mWidth/6;
   break;
  case 6:
   Between= mWidth/7;
   break;
  case 7:
   Between= mWidth/8-1;
   break;
  case 8:
   Between= mWidth/9-2;
   break;
  case 9:
   Between=mWidth/9-5;
   break;
  case 10:
   Between=mWidth/10-5;
   break;
 }
int RowIndex=this.OnePageCubeCount*this.CurrentPage;
Point p=BeginP;//dr.
p.X-=8;
for ( int i=0;i {
//p.X= this.XScale*(i+1)+10;
p.X+=Between+this.mLenght;
double CubeHeight=this.YScale*System.Convert.ToInt32(mDataTable.Rows[
RowIndex][this.mDataColumnName]);
//if ((p.X > = 0) && (p.X <= Width) && (p.Y > = 0) && (p.Y <= Height))
//{
string text=mDataTable.Rows[RowIndex][this.mShowColumnName].ToString() ;
string Data=mDataTable.Rows[RowIndex][this.mDataColumnName].ToString() ;
int ColorIndex=RowIndex;
if (ColorIndex> =color.Length)
ColorIndex=color.Length-1;
if (this.mShowXText==false) text=" ";
AddOneCube(text,p,color[i],this.mLenght,this.mLenght+4,System.Convert.
ToInt32(CubeHeight));
//}
RowIndex++;
}
}

//在左下顶点,宽Width,高Height建立立方体
private void CreateCubes(Point BeginP ,int Width,int Height)
{
if(mDataTable.Rows.Count==0) return;
//水平轴的相对值
int XScale=Width/mDataTable.Rows.Count ;
//Y轴的相对值
if(this.mMaxYValue <=0) return;
int YScale=Height/(this.mMaxYValue*this.LineCount) ;

int RowIndex=0;
Color[] color={Color.Red,Color.Blue,Color.Green,Color.Yellow,Color.YellowGreen,Color
.Magenta,_
Color.Cyan,Color.Coral,Color.SlateGray,Color.Pink,Color.Crimson,Color
.DodgerBlue,Color.Chartreuse };

foreach(DataRow dr in mDataTable.Rows)
{
RowIndex+=1;
Point p=BeginP;//dr.
p.X= XScale*RowIndex+10;
int CubeHeight=YScale*System.Convert.ToInt32(dr[this.mDataColumnName]);
if ((p.X > = 0) && (p.X <= Width) && (p.Y > = 0) && (p.Y <= Height))
{
string text=dr[this.mShowColumnName].ToString() ;
string Data=dr[this.mDataColumnName].ToString() ;
int ColorIndex=RowIndex;
if (ColorIndex> =color.Length)
ColorIndex=color.Length-1;
if (this.mShowXText==false) text=" ";
AddOneCube(text,p,color[ColorIndex-1],this.mLenght,this.mLenght+5,
CubeHeight);
//objGraph.DrawCube (color,p,this.mLenght,Width,Height);
}
}
}
//计算最大的x、y轴坐标,左边文本宽,最小的线高
private void CountMaxScaleXScaleY()
{
if(mDataTable.Rows.Count==0) return;

this.mMaxXValue=mDataTable.Rows.Count ;
this.mMaxYValue=0;
//this.l.
foreach(DataRow dr in mDataTable.Rows)
{
int CubeHeight=System.Convert.ToInt32(dr[this.mDataColumnName]);
if (mMaxYValue {
mMaxYValue=CubeHeight;
}
int TextWidth=(int)objGraph.mGraphics.MeasureString(System.Convert.
ToDouble( dr[this.mDataColumnName])._
ToString() ,this.mTextFont).Width+10;
if (LeftWidthWarp {
LeftWidthWarp=TextWidth;
}
}
//LeftWidthWarp+=10;
}
//在顶部中心位置写标题
private void DrawTitle(Rectangle rcClient)
{
int Width=(int)objGraph.mGraphics.MeasureString(this.mTitle,mTit
leFont).Width;
int Height=(int)objGraph.mGraphics.MeasureString(this.mTitle,mTitleFont).Height;
this.TopHeightWarp=Height;
int x=rcClient.Width/2- Width/2;
int y=rcClient.Y+Height/2-5;
this.objGraph.DrawText(this.mTitle,Color.Blue,mTitleFont,x,y);
}

//画所有水平网络线
//p:起始点;Width:线宽;BetweenHeight:二线之间高,Count:线数
// private void DrawGridLineAndTexts(Point p,int Width)
// {
// for(int i=0;i // {
// DrawGridLineAndText(p,p,Width,Lenght);
// p.Y-=BetweenHeight;
// }
// }


[1]  [2]  [3]  [4]  [5]  [6]  
相关文章