CSDN博客

img colayungnew

用c#做的控件—用vml生成饼图

发表于2004/12/30 11:28:00  955人阅读

分类: ASP.NET

 

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 的摘要说明。
    /// </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;//传的数值
        string[] tmpdatainfor;//传的对应内容
        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[]{','});//传的数值
            tmpdatainfor=dataInfor.Split(new char[]{','});//传的对应内容
            tmpColor=color.Split(new char[]{','});//颜色
            showMorePie();   
            this.Page.Response.Write (strPie);
        }
        #region 方法
        private void showMorePie()
        {
            //首先取数字的绝对值
            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;
            //判断是不是给出的最大的值与实际给的内容不符合
            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="杨浦区,闸北区,普陀区,虹口区,宝山区,嘉定区,黄浦区,闵行区,浦东区,静安,徐汇区,其他区,其他区";
           
            Chat1.Color="red,#FFEBCD,#FFFF55,#00BFFF,#E6E6FA,#7B68EE,#FFCC33,#FFC0CB,#008B8B,#c27f34,#FFDEAD,#00FA9A,#D3D3D3";
           
        }
//保证程序运行正常请按下面做


<HTML xmlns:v="urn:schemas-microsoft-com:vml">
    <HEAD>
        <title>WebForm2</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
        <STYLE> v/:* { BEHAVIOR: url(#default#VML) } </STYLE>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <cc1:ChartVolumeReport id="Chat1" runat="server"></cc1:ChartVolumeReport>
        </form>
    </body>
</HTML>

0 0

相关博文

我的热门文章

img
取 消
img