using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Collections; using System.Collections.Specialized; namespace Controls.Custom { /// <summary> /// ChartReport 的摘要說(shuō)明。 /// </summary> [DefaultProperty("Text"), ToolboxData("<{0}:ChartPieReport runat=server></{0}:ChartReport>")] public class ChartPieReport:Control { private int max; private string data; private string dataInfor; private string color; private string strPie; string[] tmpdata;//傳的數(shù)值 string[] tmpdatainfor;//傳的對(duì)應(yīng)內(nèi)容 string[] tmpColor; string str_temp=""; string strdivdatainfo=""; ArrayList resultData=new ArrayList(); int r=2000; int colorInfoTop = -2000; int dataInfoTop = 10; double total=0; #region 屬性 [Bindable(true),Category("Appearance"),DefaultValue("")] public int Max { get { return max; } set { max=value; } } [Bindable(true),Category("Data"),DefaultValue("")] public string Data { get { return data; } set { data=value; } } [Bindable(true),Category("Data"),DefaultValue("")] public string DataInfor { get { return DataInfor ; } set { dataInfor=value; } } [Bindable(true),Category("Data"),DefaultValue("")] public string Color { get { return color; } set { color=value; } } #endregion public ChartPieReport() { strPie+="<v:group id=\"p\" CoordOrig=\"3000,2700\" CoordSize=\"7200,4050\" style=\"WIDTH:480px;POSITION:relative;HEIGHT:270px\">"; strPie+="<v:rect style=\"LEFT:-3000px;WIDTH:9600px;POSITION:relative;TOP:-2700px;HEIGHT:5400px\" fillcolor=\"white\""; strPie+="strokecolor=\"black\" coordsize=\"21600,21600\">"; strPie+="<v:shadow on=\"t\" type=\"single\" color=\"silver\" offset=\"4pt,3pt\"></v:shadow>"; strPie+="</v:rect>"; strPie+="<v:group id=\"group1\" CoordSize=\"6600,5400\" style=\"WIDTH:8000px;POSITION:absolute;HEIGHT:5400px\">"; strdivdatainfo="<div id=\"dataInfo\">"; } protected override void Render(HtmlTextWriter output) { tmpdata=data.Split(new char[]{','});//傳的數(shù)值 tmpdatainfor=dataInfor.Split(new char[]{','});//傳的對(duì)應(yīng)內(nèi)容 tmpColor=color.Split(new char[]{','});//顏色 showMorePie(); this.Page.Response.Write (strPie); } #region 方法 private void showMorePie() { //首先取數(shù)字的絕對(duì)值 for(int i=0;i<tmpdata.Length;i++) { tmpdata[i]=Convert.ToString(Math.Abs(Convert.ToDouble(tmpdata[i]))); total+=Convert.ToDouble(tmpdata[i]); } //冒泡排序 object temp; for(int i=0;i<tmpdata.Length-1;i++) { for(int j=i+1;j<tmpdata.Length;j++) { if(Convert.ToDouble(tmpdata[i])<Convert.ToDouble(tmpdata[j])) { temp=Convert.ToDouble(tmpdata[i]); tmpdata[i]=tmpdata[j]; tmpdata[j]=temp.ToString(); temp=tmpdatainfor[i]; tmpdatainfor[i]=tmpdatainfor[j]; tmpdatainfor[j]=temp.ToString(); } } } int loop=tmpdata.Length; //判斷是不是給出的最大的值與實(shí)際給的內(nèi)容不符合 if(loop>max) { for(int i=max;i<tmpdata.Length;i++) tmpdata[max-1]+=Convert.ToDouble(tmpdata[i]); tmpdatainfor[max-1]="其他"; loop=max; } for(int i=0;i<loop;i++) { resultData.Add(Convert.ToDouble(tmpdata[i])/total); showColorInfo(i); showDataInfo(i); } strdivdatainfo+="</div>"; createPie(0,Convert.ToDouble(resultData[0]),tmpColor[0].ToString(),tmpdatainfor[0]+":"+tmpdata[0].ToString()); double currentValue=Convert.ToDouble(resultData[0]); for(int i=1;i<loop-1;i++) { createPie(currentValue,Convert.ToDouble(currentValue+Convert.ToDouble(resultData[i])),tmpColor[i].ToString(),tmpdatainfor[i]+":"+tmpdata[i].ToString()); currentValue+=Convert.ToDouble(resultData[i]); } createPie(currentValue,360,tmpColor[loop-1].ToString(),tmpdatainfor[loop-1]+":"+tmpdata[loop-1].ToString()); strPie+="<div id=\"colorInfo\">"; strPie+= str_temp; strPie+="</div>"; strPie+=strdivdatainfo; strPie+="</v:group>"; strPie+="</v:group>"; } public void showColorInfo(int i) { int top=colorInfoTop-300+i*373; string temp="<v:rect id='a"+i.ToString()+"' style='position:relative;left:2600;top:"+top.ToString()+";WIDTH:180;HEIGHT:180;' fillcolor='"+tmpColor[i]+"' strokecolor='black'><v:shadow on='t' type='single' color='silver' offset='2pt,2pt'></v:shadow></v:rect>"; str_temp=temp+str_temp; } public void showDataInfo(int i) { int top = dataInfoTop-10 + i*11; string nInfo="<Div align='left' style='font-size:9pt;position:relative;top:"+top+";left:420'>"+tmpdatainfor[i]+"</Div>"; strdivdatainfo+= nInfo; } public void createPie(double sa,double ea,string color,string n) { double fs=Convert.ToDouble(Math.PI*2*sa); double fe=Convert.ToDouble(Math.PI*2*ea); int sx=Convert.ToInt32(r*Math.Cos(fs)); int sy=Convert.ToInt32(-r*Math.Sin(fs)); int ex=Convert.ToInt32(r*Math.Cos(fe)); int ey=Convert.ToInt32(-r*Math.Sin(fe)); int index; if (sa<=0.3) { index = 100 - Convert.ToInt32((sa * 100)); strPie+="<v:shape onclick='JavaScript:alert(\""+n+"\")' title='"+n+"' style='position:relative;left:-800;top:-1000;z-index:"+index.ToString()+";width:7000;height:6000' fillcolor='"+color+"' path='m 0,0 l "+sx+","+sy+" ar -2000,-2000,2000,2000,"+sx+","+sy+","+ex+","+ey+","+" l 0,0 x e'> <v:Extrusion on='t' foredepth='0' backdepth='20pt' rotationangle='66,0' color='"+color+"'></v:Extrusion></v:shape>"; } else if (sa>=0.75) { index = (100 - Convert.ToInt32((sa * 100)))+100; strPie+="<v:shape onclick='JavaScript:alert(\""+n+"\")' title='"+n+"' style='position:relative;left:-800;top:-1000;z-index:"+index.ToString()+";width:7000;height:6000' fillcolor='"+color+"' path='m 0,0 l "+sx+","+sy+" ar -2000,-2000,2000,2000,"+sx+","+sy+","+ex+","+ey+","+" l 0,0 x e'> <v:Extrusion on='t' foredepth='0' backdepth='20pt' rotationangle='66,0' color='"+color+"'></v:Extrusion></v:shape>"; } else { strPie+="<v:shape onclick='JavaScript:alert(\""+n+"\")' title='"+n+"' style='position:relative;left:-800;top:-1000;z-index:100;width:7000;height:6000' fillcolor='"+color+"' path='m 0,0 l "+sx+","+sy+" ar -2000,-2000,2000,2000,"+sx+","+sy+","+ex+","+ey+","+" l 0,0 x e'> <v:Extrusion on='t' foredepth='0' backdepth='20pt' rotationangle='66,0' color='"+color+"'></v:Extrusion></v:shape>"; } } #endregion } } //使用例子 private void Page_Load(object sender, System.EventArgs e) { Chat1.Max=13; Chat1.Data="12.5,25,19.5,17.5,12.5,16,9,21,3,7,8,6.2,9"; Chat1.DataInfor="楊浦區(qū),閘北區(qū),普陀區(qū),虹口區(qū),寶山區(qū),嘉定區(qū),黃浦區(qū),閔行區(qū),浦東區(qū),靜安,徐匯區(qū),其他區(qū),其他區(qū)"; Chat1.Color="red,#FFEBCD,#FFFF55,#00BFFF,#E6E6FA,#7B68EE,#FFCC33,#FFC0CB,#008B8B,#c27f34,#FFDEAD,#00FA9A,#D3D3D3"; } //保證程序運(yùn)行正常請(qǐng)按下面做
下面是使用的時(shí)候,html代碼中需要加入的,注意<html>標(biāo)記處,和<Style>標(biāo)記處。