现在的位置: 首页 > 综合 > 正文

改编的图表控件,可以支持饼图,欢迎扩充

2012年09月10日 ⁄ 综合 ⁄ 共 5238字 ⁄ 字号 评论关闭
/// ///////////////////////////////////////////////////////////////////////
/// 控件说明:图表控件支持直线,矩形,饼图三种样式
///         Items:需要图像化的数据(传入DataTable)
///         Text:图表名称
///         DataStdName:标准值名称
///         DataName:对比值名称
///         ChatStyle :图表类型(ColumniationStyle)
///         Kddw:刻度单位,这里柱状图用的刻度单位是100
///         KdCount:刻度数,这里柱状图用的是5
/// 日期:20050623
/// 修改说明:from CNblogs
///////////////////////////////////////////////////////////////////////////


using System;
using System.Collections;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.ComponentModel;

[ assembly: TagPrefix(
"WebControl""ci") ]
namespace WebControl
{
    
public enum ColumniationStyle
    
{
        histogram, beeline, caky
    }


    
/// <summary>
    
///    柱状图控件
    
///    需要传入列表项名,值,超级连接地址
    
/// </summary>

    [ToolboxData("<{0}:Columniation runat=server></{0}:Columniation>")]
    
public class Columniation: System.Web.UI.WebControls.WebControl
    
{
        
private DataTable items;//列表项名称和值
       
        
private string text="实时数据";
        
private string datastd="标准值";
        
private string data="实时数据";
        
private ColumniationStyle _showType = ColumniationStyle.histogram;

        
int kds=5//刻度数
        int kddw=100;  //没刻度大小

        
int zmheight=300//真个图区高
        int zmwidth=640;  //真个图区宽

        
int height=250;//呈现区高
        int width=530;
        
int cxtop=30;//呈现区距顶距离
        int cxleft=30;//呈现区左边距离

        Color bzlink
=Color.Black;//标准线颜色
        Color bz=Color.Coral;//标准柱颜色
        Color ss=Color.CornflowerBlue;//实时柱颜色

        
//int cx=1;
    

        [Bindable(
true),Category("Appearance"),DefaultValue("")]
        
public string Text
        
{
            
get
            
{
                
return text;
            }


            
set
            
{
                text 
= value;
            }

        }

        
        [Bindable(
true),Category("Appearance"),DefaultValue("")]
        
public string DataStdName
        
{
            
get
            
{
                
return datastd;
            }


            
set
            
{
                datastd 
= value;
            }

        }


        [Bindable(
true),Category("Appearance"),DefaultValue("")]
        
public string DataName
        
{
            
get
            
{
                
return data;
            }


            
set
            
{
                data 
= value;
            }

        }


        
/// <summary>
        
/// 文本框中显示值还是名称。缺省显示值。
        
/// </summary>

        [Bindable(true), Category("Schema"), DefaultValue(ColumniationStyle.histogram),
        Description(
"图表样式,默认为柱状图"), DesignOnly(true)]
        
public ColumniationStyle ShowType
        
{
            
get
            
{
                
return this._showType ;
            }

            
set
            
{
                
this._showType = value;
            }

        }
    

        
/// <summary>
        
/// 需要呈现的数据
        
/// </summary>

        public DataTable Items
        
{
            
set
            
{items=value;}
        }


        
/// <summary>
        
/// 需要显示的刻度量
        
/// </summary>

        public int Kdcount
        
{
            
set {kds=value;}
        }


        
/// <summary>
        
/// 刻度大小
        
/// </summary>


        
public int Kddw
        
{set{kddw=value;}}
        


        
public ColumniationStyle ChatStyle
        
{set{this._showType=value;}}


        
/// <summary> 
        
/// 将此控件呈现给指定的输出参数。
        
/// </summary>
        
/// <param text="output"> 要写出到的 HTML 代码 </param>

        protected override void Render(HtmlTextWriter output)
        
{
            
//设计样式
            output.Write(makeimage(items,@"c:/"));
            
        }


        
private string makeimage(DataTable dt,string imagefile)
        
{
            
if(dt==null)
            
{
                
return "没有数据";
            }


            
//调整宽度
            width=30*dt.Rows.Count+10;
            zmwidth
=width+cxleft+80;
            
//Font
            Font fontlegend = new Font("verdana",9);
            
//创建一个画布
            Bitmap bm=new Bitmap(zmwidth,zmheight);
            
//在新建的画布上画一个图
            Graphics bp=Graphics.FromImage(bm);
            
//填充背景
            bp.Clear(Color.White);
            
//填充图表呈现区背景(画刷,起点x,起点Y,高,宽)
            bp.FillRectangle(new SolidBrush(Color.WhiteSmoke),cxleft,cxtop,width,height);
            
//描绘呈现区边框
            bp.DrawRectangle(Pens.Black,cxleft,cxtop,width,height);
            
//绘制图表名称
            bp.DrawString(text,fontlegend,new SolidBrush(Color.Black),new PointF(zmwidth/2,10));
            
            
if(this._showType!=ColumniationStyle.caky)
            
{
                
//绘制图表说明
                bp.DrawRectangle(Pens.Black,cxleft+width+10,zmheight/2,60,30);
                
//标准图例
                bp.FillRectangle(new SolidBrush(bz),cxleft+width+10+2,zmheight/2+4,8,8);
                
//文字说明
                bp.DrawString(datastd,fontlegend,new SolidBrush(Color.Black),new PointF(cxleft+width+10+2+8,zmheight/2+4));
                
//实时图例
                bp.FillRectangle(new SolidBrush(ss),cxleft+width+10+2,zmheight/2+16,8,8);
                
//文字说明
                bp.DrawString(data,fontlegend,new SolidBrush(Color.Black),new PointF(cxleft+width+10+2+8,zmheight/2+16));
                
//通过循环绘制标准线
                for(int i=1;i<=kds;i++)
                

抱歉!评论已关闭.