最近碰到的一个项目必须生成一些报表统计图(柱形图、饼图),网上强烈推荐了使用DotNetCharting控件来实现,于是对DotNetCharting控件进行了简单的学习,下面简单介绍一下DotNetCharting控件及其使用。
DotNetCharting是一个非常棒的.NET图表控件,对中文支持非常好,而且操作方便,开发快速,既有for webform 也有for winform的,而且使用简单,只需引用一个dotnetCHARTING.dll。它的官方地址是http://www.dotnetcharting.com/。
DotNetCharting的简单使用方法:
1.把/bin/dotnetCHARTING.dll添加到工具箱,并且添加引用;
2.把控件拖到你的网页上,然后添加引用using dotnetCHARTING;就可以用了;
3.接下来是自己写的对DotNetCharting操作的封装类,以便于在程序里调用。
先给出一个效果图:
实现步骤如下:
1)对DotNetCharting操作的封装类的代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using dotnetCHARTING;
/// <summary>
///NewCharting 的摘要说明
/// </summary>
public class NewCharting
{
#region 共有变量
/// <summary>
/// 图片存放路径
/// </summary>
public string PhaysicalImagePath { get; set; }
/// <summary>
/// 标题
/// </summary>
public string Title { get; set; }
/// <summary>
/// X轴名称
/// </summary>
public string XName { get; set; }
/// <summary>
/// Y轴名称
/// </summary>
public string YName { get; set; }
/// <summary>
/// 图例名称
/// </summary>
public string SeriseName { get; set; }
/// <summary>
/// 图片宽度
/// </summary>
public int PicWidth { get; set; }
/// <summary>
/// 图片高度
/// </summary>
public int PicHight { get; set; }
/// <summary>
/// 数据源
/// </summary>
public DataTable DataSoure { get; set; }
/// <summary>
/// 图片名称
/// </summary>
public string FileName { get; set; }
public bool IsUse3D { get; set; }
public ChartType type { get; set; }
#endregion
public NewCharting()
{
}
/// <summary>
/// 柱形图
/// </summary>
/// <returns></returns>
public void CreateCombo(dotnetCHARTING.Chart Chart1)
{
Chart1.Title = Title;
Chart1.FileManager.FileName=FileName;
Chart1.XAxis.Label.Text = XName;
Chart1.YAxis.Label.Text = this.YName;
Chart1.TempDirectory = this.PhaysicalImagePath;
Chart1.Width = this.PicWidth;
Chart1.Height = this.PicHight;
Chart1.Type = ChartType.Combo;
Chart1.Series.Type = SeriesType.Cylinder;
Chart1.Series.Name = this.SeriseName;
Chart1.Series.Data = this.DataSoure;
Chart1.SeriesCollection.Add();
Chart1.DefaultSeries.DefaultElement.ShowValue = true;
Chart1.ShadingEffect = true;
Chart1.Use3D = IsUse3D;
Chart1.Series.DefaultElement.ShowValue = true;
}
/// <summary>
/// 饼图
/// </summary>
/// <returns></returns>
public void CreatePie(dotnetCHARTING.Chart Chart1)
{
Chart1.Title = Title;
Chart1.FileManager.FileName=FileName;
Chart1.XAxis.Label.Text = XName;
Chart1.YAxis.Label.Text = this.YName;
Chart1.TempDirectory = this.PhaysicalImagePath;
Chart1.Width = this.PicWidth;
Chart1.Height = this.PicHight;
Chart1.Type = ChartType.Pie;
Chart1.Series.Type = SeriesType.Cylinder;
Chart1.Series.Name = this.SeriseName;
Chart1.ShadingEffect = true;
Chart1.Use3D = IsUse3D;
Chart1.DefaultSeries.DefaultElement.Transparency = 20;
Chart1.DefaultSeries.DefaultElement.ShowValue = true;
Chart1.PieLabelMode = PieLabelMode.Outside;
Chart1.SeriesCollection.Add(getArrayData());
Chart1.Series.DefaultElement.ShowValue = true;
}
private SeriesCollection getArrayData()
{
SeriesCollection SC = new SeriesCollection();
DataTable dt = this.DataSoure;
for (int i = 0; i < dt.Rows.Count; i++)
{
Series s = new Series();
s.Name = dt.Rows[i][0].ToString();
Element e = new Element();
// 每元素的名称
e.Name = dt.Rows[i][0].ToString();
// 每元素的大小数值
e.YValue = Convert.ToDouble(dt.Rows[i][1].ToString());
s.Elements.Add(e);
SC.Add(s);
}
return SC;
}
/// <summary>
/// 曲线图
/// </summary>
/// <returns></returns>
public void CreateLine(dotnetCHARTING.Chart Chart1)
{
Chart1.Title = Title;
Chart1.FileManager.FileName=FileName;
Chart1.XAxis.Label.Text = XName;
Chart1.YAxis.Label.Text = this.YName;
Chart1.TempDirectory = this.PhaysicalImagePath;
Chart1.Width = this.PicWidth;
Chart1.Height = this.PicHight;
Chart1.Type = ChartType.Combo;
//此处一定要用DefaultSeries.Type = SeriesType.Line 否则没效果
Chart1.DefaultSeries.Type = SeriesType.Line;
Chart1.Series.Name = this.SeriseName;
Chart1.Series.Data = this.DataSoure;
Chart1.SeriesCollection.Add();
Chart1.DefaultSeries.DefaultElement.ShowValue = true;
Chart1.ShadingEffect = false;
Chart1.Use3D = IsUse3D;
Chart1.Series.DefaultElement.ShowValue = true;
}
}
2)在页面的Page_Load事件中编写如下的代码:
protected void Page_Load(object sender, EventArgs e)
{
NewCharting ch = new NewCharting();
ch.Title = "学生成绩柱形图";
ch.XName = "姓名";
ch.YName = "成绩";
ch.SeriseName= "语文成绩";
ch.DataSoure = DbHelperOleDb.Query("select name,chinese FROM StudentScore").Tables[0]; //产生DataTable
ch.PicHight = 500;
ch.PicWidth = 700;
ch.PhaysicalImagePath = "images";
ch.FileName = "NewStudentScore";
ch.CreateCombo(this.Chart1);
ch.SeriseName = "数学成绩";
ch.DataSoure = DbHelperOleDb.Query("select name,math FROM StudentScore").Tables[0]; ;
ch.CreateCombo(this.Chart1);
}
3)引用的dotnetcharting.dll有隐藏的链接,就是生成出来的统计图的上边和下边的,当鼠标移上去时有指向http://www.dotnetcharting.com/ 网站的链接,简单用JS脚本移除链接的map标签:
<script type="text/javascript">
var obj = document.getElementsByTagName("map")[0];
obj.parentNode.removeChild(obj); //屏蔽隐藏的链接
</script>
程序在VS2008 SP1下调试通过。部分资料来自网络,谢谢!