.net之生成图表的控件(柱状图,曲线图,饼状图)
近来项目中需要用饼状图表示一些东东,我才想起来,曾经我还改过一个生成图表的控件,翻开电脑找了半天终于找到了,幸亏上次格盘时保存了下来,他可以生成柱状图,曲线图,饼状图,只要你给他一个datatable,在这声明一下这个控件源码是我从网上找的,然后自己又改了30%的代码,优化了一下,现在就与大家分享一下这个控件的源码
using System;
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;
using System.Collections;
namespace hya.Control
{
/// <summary>
/// 柱状图控件
/// 需要传入列表项名,值,超级连接地址
/// </summary>
[ToolboxData("<{0}:Columniation runat=server></{0}:Columniation>")]
public class Columniation : System.Web.UI.WebControls.WebControl
{
private const int _colorLimit = 12; //颜色列表
private Color[] _color =
{
Color.Chocolate,
Color.YellowGreen,
Color.Olive,
Color.DarkKhaki,
Color.Sienna,
Color.PaleGoldenrod,
Color.Peru,
Color.Tan,
Color.Khaki,
Color.DarkGoldenrod,
Color.Maroon,
Color.OliveDrab
};
private DataTable items;//列表项名称和值
private string text = "数据";
private string datastd = "标准值";
private string data = "实时数据";
int kds = 10; //刻度数
float kddw = 100; //没刻度大小
int zmheight = 500; //真个图区高
int zmwidth = 740; //真个图区宽
int height = 400;//呈现区高
int width = 730;
int cxtop = 30;//呈现区距顶距离
int cxleft = 30;//呈现区左边距离
Color bzlink = Color.Black;//标准线颜色
int Chart_Flag = 1;
Bitmap bm;
int Displacement = 0; //获取负坐标刻度数
[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>
public DataTable Items
{
set
{ items = value; }
}
/// <summary>
/// 需要显示的刻度量
/// </summary>
public int Kdcount
{
set { kds = value; }
}
/// <summary>
/// 刻度大小
/// </summary>
public float Kddw
{ set { kddw = value; } }
public int ChatStyle
{ set { this.Chart_Flag = value; } }
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param text="output"> 要写出到的 HTML 代码 </param>
protected override void Render(HtmlTextWriter output)
{
//if(dt==null)
//{
// return "没有数据";
//}
//设计样式
kd(items);
output.Write(makeimage(items, "c:/"));
}
private string makeimage(DataTable dt, string imagefile)
{
string url = "";
switch (Chart_Flag)
{
case 1:
{
this.Draw_X_Y_Bar(dt);
url = this.Drar_Bar(dt);
break;
}
case 2:
{
this.Draw_X_Y(dt);
url = this.Drow_Lin(dt);
break;
}
case 3:
{
url = this.Draw_Pie(dt);
break;
}
}
return url;
}
/// <summary>
/// 换算成实际值
/// </summary>
/// <param text="kd">提供的值</param>
/// <returns>返回换算后的实际值</returns>
private float bl(float kd)
{
float bls = 1;
bls = (float)height / ((float)kds * (float)kddw);
return (float)kd * bls;
}
//通过数据计算刻度,和负坐标数
//
绘制折线图
画住状图
画 X,Y 轴 线和刻度
画 柱状 x,y 刻度
饼状图
}
}
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;
using System.Collections;
namespace hya.Control
{
/// <summary>
/// 柱状图控件
/// 需要传入列表项名,值,超级连接地址
/// </summary>
[ToolboxData("<{0}:Columniation runat=server></{0}:Columniation>")]
public class Columniation : System.Web.UI.WebControls.WebControl
{
private const int _colorLimit = 12; //颜色列表
private Color[] _color =
{
Color.Chocolate,
Color.YellowGreen,
Color.Olive,
Color.DarkKhaki,
Color.Sienna,
Color.PaleGoldenrod,
Color.Peru,
Color.Tan,
Color.Khaki,
Color.DarkGoldenrod,
Color.Maroon,
Color.OliveDrab
};
private DataTable items;//列表项名称和值
private string text = "数据";
private string datastd = "标准值";
private string data = "实时数据";
int kds = 10; //刻度数
float kddw = 100; //没刻度大小
int zmheight = 500; //真个图区高
int zmwidth = 740; //真个图区宽
int height = 400;//呈现区高
int width = 730;
int cxtop = 30;//呈现区距顶距离
int cxleft = 30;//呈现区左边距离
Color bzlink = Color.Black;//标准线颜色
int Chart_Flag = 1;
Bitmap bm;
int Displacement = 0; //获取负坐标刻度数
[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>
public DataTable Items
{
set
{ items = value; }
}
/// <summary>
/// 需要显示的刻度量
/// </summary>
public int Kdcount
{
set { kds = value; }
}
/// <summary>
/// 刻度大小
/// </summary>
public float Kddw
{ set { kddw = value; } }
public int ChatStyle
{ set { this.Chart_Flag = value; } }
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param text="output"> 要写出到的 HTML 代码 </param>
protected override void Render(HtmlTextWriter output)
{
//if(dt==null)
//{
// return "没有数据";
//}
//设计样式
kd(items);
output.Write(makeimage(items, "c:/"));
}
private string makeimage(DataTable dt, string imagefile)
{
string url = "";
switch (Chart_Flag)
{
case 1:
{
this.Draw_X_Y_Bar(dt);
url = this.Drar_Bar(dt);
break;
}
case 2:
{
this.Draw_X_Y(dt);
url = this.Drow_Lin(dt);
break;
}
case 3:
{
url = this.Draw_Pie(dt);
break;
}
}
return url;
}
/// <summary>
/// 换算成实际值
/// </summary>
/// <param text="kd">提供的值</param>
/// <returns>返回换算后的实际值</returns>
private float bl(float kd)
{
float bls = 1;
bls = (float)height / ((float)kds * (float)kddw);
return (float)kd * bls;
}
//通过数据计算刻度,和负坐标数
//
绘制折线图
画住状图
画 X,Y 轴 线和刻度
画 柱状 x,y 刻度
饼状图
}
}
在调用时很简单, 首先将它编译成控件,在aspx页添加引用
<%@ Register TagPrefix="cc1" Namespace="hya.Control" Assembly="Columniation" %>
在你需要显示图表的地方添加如下标签:
<cc1:Columniation id="Columniation1" runat="server"></cc1:Columniation>
在对应的.cs页写下如下的代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "name";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Int32");
dc.ColumnName = "db";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Int32");
dc.ColumnName = "df";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Int32");
dc.ColumnName = "dz";
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr["name"] = "点1";
dr["db"] = "1000";
dr["df"] = "500";
dr["dz"] = "240";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "点2";
dr["db"] = "200";
dr["df"] = "200";
dr["dz"] = "240";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "点3";
dr["db"] = "300";
dr["df"] = "600";
dr["dz"] = "240";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "点4";
dr["db"] = "200";
dr["df"] = "1500";
dr["dz"] = "240";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "点5";
dr["db"] = "400";
dr["df"] = "2400";
dr["dz"] = "240";
dt.Rows.Add(dr);
Columniation1.Items = dt;
Columniation1.ChatStyle = 3; //1 为柱状,2 为折线,3为屏状
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "name";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Int32");
dc.ColumnName = "db";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Int32");
dc.ColumnName = "df";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Int32");
dc.ColumnName = "dz";
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr["name"] = "点1";
dr["db"] = "1000";
dr["df"] = "500";
dr["dz"] = "240";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "点2";
dr["db"] = "200";
dr["df"] = "200";
dr["dz"] = "240";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "点3";
dr["db"] = "300";
dr["df"] = "600";
dr["dz"] = "240";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "点4";
dr["db"] = "200";
dr["df"] = "1500";
dr["dz"] = "240";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "点5";
dr["db"] = "400";
dr["df"] = "2400";
dr["dz"] = "240";
dt.Rows.Add(dr);
Columniation1.Items = dt;
Columniation1.ChatStyle = 3; //1 为柱状,2 为折线,3为屏状
}
}
其生成的饼状图如下:
其生成的柱状图如下:
其生成的折线图如下: