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

原创 2004年12月30日 11:09:00

 

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>

C# 数据库系统中使用GDI+绘制柱状图

在C#+SQL Server数据库做系统中,通常需要对数据库中的数据进行绘制图形报表方便经理查看,虽然有很多实用的水晶报表控件和图表控件实现该功能,但我还是想讲讲如何使用GDI绘制简单的柱状图.(推荐...
  • Eastmount
  • Eastmount
  • 2013年09月14日 01:42
  • 4787

C# 绘制统计图(柱状图, 折线图, 扇形图)

一. 柱状图的绘制.绘制步骤如下: 定义绘图用到的类. 定义绘图类 int height = 500, width = 700; Bitmap image = new Bitmap(width, ...
  • liuyuehui110
  • liuyuehui110
  • 2017年04月01日 17:01
  • 2162

c#中连接数据库后绘制柱状图和饼状图

自己在项目中使用的绘制柱状图和饼状图的方法 简单实用,可直接调用 public class GongNeng {     System.Data.DataTable table1 = new ...
  • yue1183283159
  • yue1183283159
  • 2013年01月24日 12:32
  • 2377

C# 绘制统计图-图表_制图-报表(柱状图, 折线图, 扇形图)_制图篇(不用控件)_从简单到复杂_详细教程—追雨

  • 2012年07月26日 21:20
  • 1.66MB
  • 下载

ASP结合VML生成柱状图

  • 2007年06月26日 01:46
  • 9KB
  • 下载

C#图形控件柱状图 饼图

  • 2018年01月05日 16:09
  • 5.69MB
  • 下载

c# 画柱状图 dll自定义控件

  • 2011年05月30日 20:28
  • 24KB
  • 下载

vml+javascript实现的3d柱状图(仅适用于ie)

  • 2007年09月01日 11:29
  • 4KB
  • 下载

绘图控件(包括直方图,柱状图等)

  • 2010年03月29日 23:42
  • 6.67MB
  • 下载

winform中chart控件的简单应用,饼图、柱状图、曲线图

  • 2017年04月22日 09:55
  • 880KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:c#做的控件—用vml生成柱状图
举报原因:
原因补充:

(最多只允许输入30个字)