CSDN博客

img colayungnew

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

发表于2004/12/30 11:09:00  1181人阅读

分类: 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>
    /// ChartVolumeReport 的摘要说明。
    /// </summary>
    [DefaultProperty("Text"),
    ToolboxData("<{0}:ChartVolumeReport runat=server></{0}:ChartVolumeReport>")]
    public class ChartVolumeReport:Control
    {
        int bx = 1000;
        int by = 4700;//坐标的最低处
        int yh = 80;//垂直阴影高度
        int xw = 80;//水平阴影宽度
        int count=0;
        double step;
        double max;
        double min;
        int deci = 2;
        double ystep;
        string str_Volume=string.Empty;
        string str_Group3=string.Empty;
        string str_TempA=string.Empty;
        string str_info=string.Empty;
        string str_dinfo=string.Empty;
        string temp=string.Empty;
        private string  data;
        private string dataInfor;
        private string colorArr;
        private string[] Color;

        string[] tmpdata;//传的数值
        string[] tmpdatainfor;//传的对应内容
        #region 属性
        [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 ColorArr
        {
            get
            {
                return colorArr;
            }
            set
            {
                colorArr=value;
            }   
        }
        #endregion
        public ChartVolumeReport()
        {
            str_Volume+="<v:group ID=/"group2/" style=/"WIDTH:480px;POSITION:relative;HEIGHT:270px/" coordsize=/"7200,4050/">";
            str_Volume+="<v:rect style=/"WIDTH:9600px;HEIGHT:5400px/" fillcolor=/"white/" strokecolor=/"black/" coordsize=/"21600,21600/">";
            str_Volume+="<v:shadow on=/"t/" type=/"single/" color=/"silver/" offset=/"4pt,3pt/"></v:shadow>";
            str_Volume+="</v:rect>";

            str_Group3+="<v:group ID=/"group3/" style=/"DISPLAY:block;WIDTH:9600px;POSITION:relative;HEIGHT:5400px/" coordsize=/"9600,5400/">";
            str_Group3+="<v:line from=/"1000,500/" to=/"1000,4900/" style=/"Z-INDEX:1;POSITION:absolute/" strokeweight=/"1pt/">";
            str_Group3+="<v:stroke StartArrow=/"classic/" />";
            str_Group3+="</v:line>";
            str_Group3+="<v:polyLine filled=/"t/" fillcolor=/"#adaead/" strokecolor=/"#adaead/" strokeweight=/"1pt/" points=/"1000,700,1100,620,1100,4620,1000,4700,1000,700/"></v:polyLine>";
            str_Group3+="<v:polyLine filled=/"t/" fillcolor=/"#d6d7d6/" strokecolor=/"#d6d7d6/" strokeweight=/"1pt/" points=/"1000,4700,1100,4620,9100,4620,9000,4700,1000,4700/"></v:polyLine>";
            str_Group3+="<v:polyLine filled=/"t/" fillcolor=/"#efefef/" strokecolor=/"#efefef/" strokeweight=/"1pt/" points=/"1000,4700,9000,4700,9000,4900,1000,4900,1000,4700/"></v:polyLine>";
            str_Group3+="<v:polyLine filled=/"t/" fillcolor=/"#fc0/" strokecolor=/"#fc0/" strokeweight=/"1pt/" points=/"9000,4700,9100,4620,9100,4820,9000,4900,9000,4700/"></v:polyLine>";

            str_info="<div id=/"info/">";
            str_dinfo="<div id=/"dinfo/">";

 

        }
        protected override void Render(HtmlTextWriter output)
        {
            Color=colorArr.Split(new char[]{','});
            tmpdata=data.Split(new char[]{','});//传的数值
            tmpdatainfor=dataInfor.Split(new char[]{','});//传的对应内容
            count=tmpdata.Length;
            step=  8000/count;
            max=Convert.ToDouble(tmpdata[0]);
            min=Convert.ToDouble(tmpdata[0]);
            for(int i=0;i<tmpdata.Length;i++)
            {
                if(Convert.ToDouble(tmpdata[i])/1>max)
                    max=Convert.ToDouble(tmpdata[i]);
                if(Convert.ToDouble(tmpdata[i])/1<min)
                    min=Convert.ToDouble(tmpdata[i]);
            }
            ystep=4000/max;
            drawColumn();
            this.Page.Response.Write(str_Volume);
        }
        private void drawColumn()
        {
            //画竖直背景
            for(int i=0;i<tmpdata.Length;i++)
            {
   
                if(i%2 != 0)
                {
                    temp="<v:PolyLine filled=t fillcolor='#F7F7F7' strokecolor='#F7F7F7' strokeweight=1pt points='"+(bx+i*step+xw)+","+(by-yh-10)+" "+(bx+i*step+xw)+",500 "+(bx+(i/1+1)*step+xw)+",500 "+(bx+(i/1+1)*step+xw)+","+(by-yh-10)+" "+(bx+i*step+xw)+","+(by-yh-10)+"'/>";
                    str_TempA=temp+"<v:PolyLine filled=t fillcolor='#dedfde' strokecolor='#dedfde' strokeweight=1pt points='"+(bx+i*step)+","+by+" "+(bx+(i/1+1)*step)+","+by+" "+(bx+(i/1+1)*step)+","+(by+200)+" "+(bx+i*step)+","+(by+200)+" "+(bx+i*step)+","+by+"'/>"+str_TempA;
                }
               
            }
            //str_Volume+=str_TempA;

            //画水平横线和Y侧横线
            string lineColor="";
            for(int i=1;i<11;i++)
            {
                double ynum;
                if (deci >0 )
                {
                    ynum = Math.Round(Math.Pow(10,deci)*i*max*0.1)*Math.Pow(10,deci+1)/(Math.Pow(10,deci+deci+1));
                }
                else
                {
                    ynum = Math.Round(i*max*0.1);
                }
                if(i%3==0)
                    lineColor="#B5E3B5";
                else
                    lineColor="#E7E3E7";
                temp="<v:Line title="+ynum+" strokecolor='"+lineColor+"' strokeweight=1pt from='"+(bx+xw)+","+(by-i*400-yh)+"' to='"+9100+","+(by-i*400-yh)+"'/>";
                str_TempA=str_TempA+temp;;
                temp="<v:Line title="+ynum+" strokecolor='#E7E3E7' strokeweight=1pt from='"+bx+","+(by-i*400)+"' to='"+(bx+xw)+","+(by-i*400-yh)+"'/>";
                str_TempA=str_TempA+temp;
                temp="<v:Line title="+ynum+" strokecolor='#000000' strokeweight=1pt from='"+bx+","+(by-i*400)+"' to='"+(bx-100)+","+(by-i*400)+"'/>";
                str_TempA=str_TempA+temp;
                double top = 0 + i*12.5;
                if (deci>0)
                {
                    ynum = Math.Round(Math.Pow(10,deci)*(11-i)*max*0.1)*Math.Pow(10,deci+1)/(Math.Pow(10,deci+deci+1));
                }
                else
                {
                    ynum = Math.Round((11-i)*max*0.1);
                }
                string nInfo="<Div align='right' style='font-size:9pt;position:relative;top:"+top+";left:-555'>"+ynum+"</Div>";
                str_dinfo+= nInfo;

            }
            //画X侧横线画柱状图
            string infostr="<TABLE cellspacing='0' cellpadding='0' border=0 style='POSITION:absolute;LEFT:6px;TOP:299px;width:535px;'><TR>";
            for(int i=0;i<tmpdata.Length;i++)
            {
                infostr+="<TD width='"+100/tmpdata.Length+"%' align='center'><FONT SIZE=2>"+tmpdatainfor[i].ToString()+"</FONT></TD>";
               
                if(Convert.ToDouble(tmpdata[i])/1 != 0)
                    createColumn(i);
            }
            infostr+="</TR></TABLE>";
            str_info=infostr;
            //完成,综合语句
            str_Volume+=str_TempA;
            str_Volume+=str_Group3;
            str_Volume+=str_info;
            str_Volume+="</div>";
            str_Volume+=str_dinfo;
            str_Volume+="</div>";
            str_Volume+="</v:group></v:group>";

        }
        private void createColumn(int index)
        {
            double sx=bx+index*step/1+step/4;
            double h=Convert.ToDouble(tmpdata[index])*ystep;
            double sy=4700-h;
            string showMsg=tmpdatainfor[index]+":"+tmpdata[index];
            str_Group3+="<v:polyLine onclick='JavaScript:alert(/""+showMsg+"/")' title='"+showMsg+"' style='z-index:9' filled=t fillcolor='"+Color[index]+"' strokecolor='"+Color[index]+"' strokeweight=1pt points='"+sx+","+by+" "+sx+","+sy+" "+(sx+step/2)+","+sy+" "+(sx+step/2)+","+by+" "+sx+","+by+"'>" + "<v:Extrusion on='t' foredepth='0' backdepth='15pt' color='"+Color[index]+"' rotationangle='0,0'></v:Extrusion></v:polyLine>";
        }
    }
}
//下面是使用的例子
    private void Page_Load(object sender, System.EventArgs e)
        {
            Chat1.Data="12.5,25,19.5,17.5,12.5,16,9,21,3,7,8,6.2,9";
            Chat1.DataInfor="杨浦区,闸北区,普陀区,虹口区,宝山区,嘉定区,黄浦区,闵行区,浦东区,静安,徐汇区,其他区,其他区";
            Chat1.ColorArr="#ff2222,#d0b83a,#ffdf00,green,#ff9e00,#799AE1,#99c741,#d0b83a,#319a00,#c27f34,#2222ff,#ff2222,#99c741";
        }
//保证程序运行正常请按下面做


下面是使用的时候,html代码中需要加入的,注意< html >标记处,和< Style >标记处。

<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