asp.net使用DotNetCharting控件生成报表统计图

DotNetCharting是一个非常棒的.NET图表控件,对中文支持非常好,而且操作方便,开发快速,既有for webform 也有for winform的,而且.net1.1和2.0都有支持。它的官方地址是http://www.dotnetcharting.com/

本站也提供了DotNetCharting破解版本下载: http://files.cnblogs.com/dreamof/dotnetcharting.rar

强烈推荐一下DotNetCharting的demo地址:
这个是所有的 DEMO 演示 http://www.dotnetcharting.com/demo.aspx
这个是 Online Documentation http://www.dotnetcharting.com/documentation/v4_4/webframe.html 里面会有详细的说明和用法。

DotNetCharting的简单使用方法:
1.把/bin/dotnetCHARTING.dll添加到工具箱,并且添加引用;

2.把控件拖到你的网页上,然后添加引用using dotnetCHARTING;就可以用了;

3.接下来是自己写的对DotNetCharting操作的封装类,以便于在程序里调用。

ShowData.cs
using System;
using System.Data;
using System.Text;
using dotnetCHARTING;

namespace FLX.ComplexQuery
{
/** <summary>
/// 彭建军
/// 根据数据动态生成图形(柱形图、饼图、曲线图)
/// 2008-06-19
/// </summary>
public class ShowData
{

属性#region 属性
private string _phaysicalimagepath;//图片存放路径
private string _title; //图片标题
private string _xtitle;//图片x座标名称
private string _ytitle;//图片y座标名称
private string _seriesname;//图例名称
private int _picwidth;//图片宽度
private int _pichight;//图片高度
private DataTable _dt;//图片数据源

/** <summary>
/// 图片存放路径
/// </summary>
public string PhaysicalImagePath
{
set{_phaysicalimagepath=value;}
get{return _phaysicalimagepath;}
}
/** <summary>
/// 图片标题
/// </summary>
public string Title
{
set{_title=value;}
get{return _title;}
}
/** <summary>
/// 图片标题
/// </summary>
public string XTitle
{
set{_xtitle=value;}
get{return _xtitle;}
}
/** <summary>
/// 图片标题
/// </summary>
public string YTitle
{
set{_ytitle=value;}
get{return _ytitle;}
}

/** <summary>
/// 图例名称
/// </summary>
public string SeriesName
{
set{_seriesname=value;}
get{return _seriesname;}
}
/** <summary>
/// 图片宽度
/// </summary>
public int PicWidth
{
set{_picwidth=value;}
get{return _picwidth;}
}
/** <summary>
/// 图片高度
/// </summary>
public int PicHight
{
set{_pichight=value;}
get{return _pichight;}
}
/** <summary>
/// 图片数据源
/// </summary>
public DataTable DataSource
{
set{_dt=value; }
get{return _dt;}
}
#endregion

构造函数#region 构造函数
public ShowData()
{
//
// TOD 在此处添加构造函数逻辑
//
}

public ShowData(string PhaysicalImagePath,string Title,string XTitle,string YTitle,string SeriesName)
{
_phaysicalimagepath=PhaysicalImagePath;
_title=Title;
_xtitle=XTitle;
_ytitle=YTitle;
_seriesname=SeriesName;
}
#endregion

输出柱形图#region 输出柱形图
/** <summary>
/// 柱形图
/// </summary>
/// <returns></returns>
public void CreateColumn(dotnetCHARTING.Chart chart)
{
chart.Title=this._title;
chart.XAxis.Label.Text=this._xtitle;
chart.YAxis.Label.Text=this._ytitle;
chart.TempDirectory =this._phaysicalimagepath;
chart.Width = this._picwidth;
chart.Height = this._pichight;
chart.Type = ChartType.Combo ;
chart.Series.Type =SeriesType.Cylinder;
chart.Series.Name = this._seriesname;
chart.Series.Data = this._dt;
chart.SeriesCollection.Add();
chart.DefaultSeries.DefaultElement.ShowValue = true;
chart.ShadingEffect = true;
chart.Use3D = false;
chart.Series.DefaultElement.ShowValue =true;
}
#endregion

输出饼图#region 输出饼图
/** <summary>
/// 饼图
/// </summary>
/// <returns></returns>
public void CreatePie(dotnetCHARTING.Chart chart)
{
chart.Title=this._title;
chart.TempDirectory =this._phaysicalimagepath;
chart.Width = this._picwidth;
chart.Height = this._pichight;
chart.Type = ChartType.Pie;
chart.Series.Type =SeriesType.Cylinder;
chart.Series.Name = this._seriesname;

chart.ShadingEffect = true;
chart.Use3D = false;
chart.DefaultSeries.DefaultElement.Transparency = 20;
chart.DefaultSeries.DefaultElement.ShowValue = true;
chart.PieLabelMode = PieLabelMode.Outside;
chart.SeriesCollection.Add(getArrayData());
chart.Series.DefaultElement.ShowValue = true; 
}

private SeriesCollection getArrayData()        
{
SeriesCollection SC = new SeriesCollection();
DataTable dt = this._dt;

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.ToInt32(dt.Rows[i][1].ToString());
          
s.Elements.Add(e);
SC.Add(s);
}
return SC;
}
#endregion

输出曲线图#region 输出曲线图
/** <summary>
/// 曲线图
/// </summary>
/// <returns></returns>
public void CreateLine(dotnetCHARTING.Chart chart)
{
chart.Title=this._title;
chart.XAxis.Label.Text=this._xtitle;
chart.YAxis.Label.Text=this._ytitle;
chart.TempDirectory =this._phaysicalimagepath;
chart.Width = this._picwidth;
chart.Height = this._pichight;
chart.Type = ChartType.Combo ;
chart.Series.Type =SeriesType.Line;
chart.Series.Name = this._seriesname;
chart.Series.Data = this._dt;
chart.SeriesCollection.Add();
chart.DefaultSeries.DefaultElement.ShowValue = true;
chart.ShadingEffect = true;
chart.Use3D = false;
chart.Series.DefaultElement.ShowValue =true;
}
#endregion

调用说明及范例#region 调用说明及范例
// 在要显示统计图的页面代码直接调用,方法类似如下:
//
// ShowData show=new ShowData();
// show.Title ="2008年各月消费情况统计";
// show.XTitle ="月份";
// show.YTitle ="金额(万元)";
// show.PicHight =300;
// show.PicWidth =600;
// show.SeriesName ="具体详情";
// show.PhaysicalImagePath ="ChartImages";
// show.DataSource =this.GetDataSource();
// show.CreateColumn(this.Chart1);
#endregion

}
}

this.GetDataSource()是你的数据源,以DataTable为例,第一列表示图例中的X坐标值,第二列表示图例的Y坐标值.

更详细的图标样式及相关代码见:http://www.dotnetcharting.com/gallery/

 

需要主意的是:

生成统计图无法自动删除的问题。每次生成统计图,都会生成一个文件,那么将会生成大量的无用的图片。这时就既要手动删除这些文件了,包括不在TempDirectory设置文件夹里的No Data图片,需要过一会清除。但是如果访问量大的话,也不是办法。现在只有这个办法了。
    protected void Page_Load(object sender, EventArgs e)
    {
        string dirPath;
        if (!Page.IsPostBack)
       
        {
            dirPath = Server.MapPath("../") + @"admin/temp/";
            if (Directory.Exists(dirPath))
            {
                DirectoryInfo dir = new DirectoryInfo(dirPath);
                if (dir.GetFiles().Length > 50)
                {
                    foreach (FileInfo f in dir.GetFiles())
                    {
                        f.Delete();
                    }
                }
            }
            dirPath = Server.MapPath("../") + @"admin/";
            DirectoryInfo adminDir = new DirectoryInfo(dirPath);
            foreach (FileInfo fi in adminDir.GetFiles())
            {
                if (fi.Extension.ToLower() == ".png")
                {
                    fi.Delete();
                }               
            }
        }
       
    }

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值