function DBChartPie(parent){
	var pdiv,obj=document.createElement('div');//创建一个控制节点
	if(parent){pdiv=parent;}else pdiv=document.body;//获取父块
	pdiv.appendChild(obj);
	obj.max=0;			   //取得的最大值
	obj.total=0;			//所取值的总和
	obj.LabelValue=new Array();   //
    obj.Value=new Array();	
	obj.count;//记录总数	
	obj.Canvas;   
	obj.DelChildren=DBCP_delchildren; //删除
	obj.Refresh=DBCP_refresh;
	obj.DrawCaky=DBCP_drawcaky;//画饼状图
	obj.GetMaxValue=DBCP_maxvalue;  //取最大值
	obj.Style=obj.style;	
	obj.style.width=400;
	obj.style.height=400;
	obj.LabelField=null;
	obj.ValueField=null;
	obj.DataSet;	
	obj.HandleMessage=DBCP_handlemessage;
	obj.Show=DBCP_show;
	obj.onpropertychange=DBCP_onpropertychange;//属性变化的事件
	//obj.style.position='absolute';	
    obj.oncontextmenu=div_oncontextmenu;
	return obj;
}

function DBCP_onpropertychange()//属性变化处理
{
  var n=event.propertyName;
  switch(n){
	case 'DataSet':
	  if(this.OldDataSet)this.OldDataSet.RemoveLink(this);//从原来的DATASET中移除
      this.DataSet.LinkObj(this);
	  this.OldDataSet=this.DataSet;
	  break;
  }
}
function DBCP_show(){
	
	this.GetMaxValue();
	this.DrawCaky();
}
function DBCP_drawcaky(){
	var circlediv=document.createElement('div');
	this.appendChild(circlediv);	
	var width=this.offsetWidth,height=this.offsetHeight;	//图形大小	
	var val=Math.min(parseInt(this.style.width),parseInt(this.style.height));//取最小值
	var radius=Math.round(parseInt(val)*2.0);  //园心和半径	
	var xo=yo=radius;
	var tdiv=document.createElement('div');  //放颜色说明的块
	tdiv.style.position='absolute';
	tdiv.style.top=0;	
//	tdiv.style.left=Math.round(radius/2.5);//块的位置和园的大小的关系	
//	tdiv.style.width=100;	//快的宽度
	circlediv.appendChild(tdiv);
	this.Canvas=new Canvas(this);		
	var ori=5;     //缩放比例	 多少倍
	var arr=0;     //当前数据对应的宽度
	var bangle=0;  //角度
	var coordsize=width*ori+','+height*ori;  //比例
	this.Canvas.Group(width,height,coordsize);	
	var postop=20, posleft=this.offsetWidth*0.7;//颜色说明的位置
	var rwidth=20,rheight=10;
	var rcolor=new Array();   //颜色
	var color1=255,color2=150,color3=150;//初始颜色
	for(var i=1;i<=this.count;i++){		
		arr=Math.round(this.Value[i]/this.total*360*65536);     //当前数据对应的值所占的宽度				
		if((color1=color1-10)<0)    //颜色设置
			color1=color1+26;
		if((color2=color2+40)>255)
			color2=color2-20;
		if((color3=color3+23)>255)
			color3=color3-40;		
		rcolor[i]='rgb('+color1+','+color2+','+color3+')';				
		this.Canvas.Brush.Color=rcolor[i];
		var path="M "+xo+" "+yo+" AE "+xo+" "+yo+" "+radius+" "+radius+" "+bangle+" "+arr+"X E";
		this.Canvas.Shape(width*ori,height*ori,path).title=this.LabelField+'='+this.LabelValue[i]+','+this.ValueField+'='+this.Value[i]+'； Per is '+this.Value[i]/this.total*100+'%';		  
		bangle=bangle+arr;		//下次画图的起始位置			
	}
	this.Canvas.FrameDiv=tdiv;
	this.Canvas.Pen.Color='black';
	this.Canvas.Top=postop-20;
	this.Canvas.Left=posleft;
	this.Canvas.Textbox(this.DataSet.Fields.Field[this.LabelField].DisplayLabel);
	this.Canvas.Top=postop-30;
	this.Canvas.Left=this.offsetLeft;
	this.Canvas.Textbox(this.DataSet.Fields.Field[this.ValueField].DisplayLabel);
	this.Canvas.Pen.Color='black';
	for(var i=1;i<=this.count;i++){		      //颜色表示图
		this.Canvas.Brush.Color=rcolor[i];
		this.Canvas.Top=postop;
		this.Canvas.Left=posleft;		
		this.Canvas.Rect(rwidth,rheight);
		this.Canvas.Left=posleft+rwidth+10;
		this.Canvas.Top=postop-2;
		this.Canvas.Textbox(this.LabelValue[i]);
		postop=postop+rheight+3;
	}
}


function DBCP_maxvalue(){
	this.DataSet.AutoControl=false;
	this.count=this.DataSet.RecordCount;	 
	this.DataSet.First();	 
	for(var i=1;i<=this.count;i++){		
		this.Value[i]=this.DataSet.Fields.Field[this.ValueField].Value;
		this.total=this.total+parseFloat(this.Value[i]);  
		this.LabelValue[i]=this.DataSet.Fields.Field[this.LabelField].GetValue();
        this.max=Math.max(this.max,this.Value[i]);
		this.DataSet.Next();
  }   
	this.DataSet.AutoControl=true;
	this.DataSet.First();
}

function DBCP_delchildren(){   //删除块中的节点
	var fd=this;
	var childno;		
	while(fd.childNodes.length!=0){
		childno =fd.children(0);
		fd.removeChild(childno);
		childno.removeNode(false);		
		}
}

function DBCP_refresh(){	
		this.DelChildren();
		this.total=0; 
		this.max=0;
		this.GetMaxValue();
		this.DrawCaky();
}

function DBCP_handlemessage(send,msg,recno){
	switch(msg){
		case 0:this.Refresh();break;
		case 1:break;
		case 2:break;
		case 3:break;
		case 4:break;
		case 5:this.Refresh();break;
		case 6:break;
		case 7:this.DelChildren();break;
		case 8:break;
		case 9:break;
	}
}