ASP.NET基于donetCHARTING的自动报表

时间:2023-12-06 09:29:50

1,首先需要添加引用ChartExtents.dll和donetCHARTING.dll,资源百度大把。

2,配置图片生成类。

 using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using dotnetCHARTING; namespace ting.Models.BLL
{
public class Charting
{
private string _phaysicalimagepath;//图片存放路径
private string _title; //图片标题
private string _xtitle;//图片x座标名称
private string _ytitle;//图片y座标名称
private string _seriesname;//图例名称
private int _picwidth;//图片宽度
private int _pichight;//图片高度
private SeriesType _type;//统计图类型(柱形,线形等)
private bool _use3d;//是否显示成3维图片
private SeriesCollection _dt;//统计图数据源
private string _filename;//统计图片的名称(不包括后缀名) /**/
/// <summary>
/// 图片存放路径
/// </summary>
public string PhaysicalImagePath
{
set { _phaysicalimagepath = value; }
get { return _phaysicalimagepath; }
}
/**/
/// <summary>
/// 图片标题
/// </summary>
public string Title
{
set { _title = value; }
get { return _title; }
}
/**/
/// <summary>
/// 图片x座标名称
/// </summary>
public string XTitle
{
set { _xtitle = value; }
get { return _xtitle; }
}
/**/
/// <summary>
/// 图片y座标名称
/// </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 SeriesType Type
{
set { _type = value; }
get { return _type; }
} /// <summary>
/// 是否将输出的图片显示成三维
/// </summary>
public bool Use3D
{
set { _use3d = value; }
get { return _use3d; }
} /// <summary>
/// 对比图形数据源
/// </summary>
public SeriesCollection DataSource
{ set { _dt = value; }
get { return _dt; }
} /// <summary>
/// 生成统计图片的名称
/// </summary>
public string FileName
{
set { _filename = value; }
get { return _filename; }
} /// <summary>
/// 生成统计图片
/// </summary>
/// <param name="chart"></param>
/// <param name="type">图形类别,如柱状,折线型</param>
public void CreateStatisticPic(dotnetCHARTING.Chart chart)
{
chart.Title = this.Title;
chart.XAxis.Label.Text = this.XTitle;
chart.YAxis.Label.Text = this.YTitle;
chart.TempDirectory = this.PhaysicalImagePath;
chart.FileManager.FileName = this.FileName;
chart.Width = this.PicWidth;
chart.Height = this.PicHight;
chart.Type = ChartType.Combo;
chart.DefaultSeries.Type = this.Type; //统一使用默认的序列图类型属性
chart.Series.Name = this.SeriesName;
chart.SeriesCollection.Add(this.DataSource);
chart.DefaultSeries.DefaultElement.ShowValue = true;
chart.ShadingEffect = true;
chart.Use3D = this.Use3D;
chart.Series.DefaultElement.ShowValue = true;
}
}
}

3,新建一个ASP.NET页面,前台代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Statistics.aspx.cs" Inherits="ting.Views.Statistics" %>
<%@ Register Assembly="dotnetCHARTING" Namespace="dotnetCHARTING" TagPrefix="dotnetCHARTING" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dotnetCHARTING:Chart ID="Chart1" runat="server">
</dotnetCHARTING:Chart>
</div>
<p>
请选择报表类型:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</p> </form>
</body>
</html>
<script type="text/javascript">
var obj = document.getElementsByTagName("map")[];
obj.parentNode.removeChild(obj); //屏蔽隐藏的链接
</script>

后台代码如下:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
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 System.Drawing;
using dotnetCHARTING;
using ting.Models.BLL; namespace ting.Views
{
public partial class Statistics : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Drawing("Bar"); DropDownList1.Items.Add(new ListItem("AreaLine", "AreaLine"));
DropDownList1.Items.Add(new ListItem("Bar", "Bar"));
DropDownList1.Items.Add(new ListItem("Column", "Column"));
DropDownList1.Items.Add(new ListItem("Cylinder", "Cylinder"));
DropDownList1.Items.Add(new ListItem("Line", "Line"));
DropDownList1.Items.Add(new ListItem("Marker", "Marker"));
DropDownList1.Items.Add(new ListItem("Spline", "Spline"));
}
}
private void Drawing(string type)
{
Charting c = new Charting(); c.Title = "2015年月销售统计图";
c.XTitle = "2015年各月份";
c.YTitle = "销售额(千元)/销售量(束)";
c.PicHight = ;
c.PicWidth = ;
c.SeriesName = "合计";//仅对于DataTable类型做数据源时,此属性有效
c.PhaysicalImagePath = "/images/ChartImages";//统计图片存放的文件夹名称,缺少对应的文件夹生成不了统计图片
c.FileName = "Statistics51aspx";
if (type == "AreaLine")
c.Type = SeriesType.AreaLine;
else if (type == "Bar")
c.Type = SeriesType.Bar;
else if (type == "Column")
c.Type = SeriesType.Column;
else if (type == "Cylinder")
c.Type = SeriesType.Cylinder;
else if (type == "Line")
c.Type = SeriesType.Line;
else if (type == "Marker")
c.Type = SeriesType.Marker;
else
c.Type = SeriesType.Spline; c.Use3D = true; //3D模式
c.DataSource = GetDataSource();
c.CreateStatisticPic(this.Chart1); }
/// <summary>
/// 生成统计图片的数据源模型(单一或对比图都可以)
/// </summary>
/// <returns></returns>
private SeriesCollection GetDataSource()
{
ting.Models.BLL.AdminOPT adopt = new ting.Models.BLL.AdminOPT();
SeriesCollection SC = new SeriesCollection();
Random rd = new Random(); // 生成对比图
for (int a = ; a <= ; a++) //对比的项数,如年各月的月销售额和月利润载客量数据对比就相当于有两个数据项
{
Series s = new Series();
s.Name = (a == ? "月销售额合计(千元)" : "销售量合计(束)");//各个数据项代表的名称,如月销售额和月利润12个月载客量走势图,则一条表示月销售额,一条表示月利润
if (a == )
{
for (int b = ; b <= ; b++) //X轴尺度个数,如12个月表示有12个尺度数
{
Element e = new Element();
e.Name = b.ToString();//对应于X轴个尺度的名称
e.YValue = Convert.ToDouble(adopt.Get_MonthCountMoney(b)/999.00);//与X轴对应的Y轴的数值
s.Elements.Add(e);
}
}
if(a==)
{
for (int b = ; b <= ; b++) //X轴尺度个数,如12个月表示有12个尺度数
{
Element e = new Element();
e.Name = b.ToString();//对应于X轴个尺度的名称
e.YValue = Convert.ToDouble(adopt.Get_MonthCountNum(b));//与X轴对应的Y轴的数值
s.Elements.Add(e);
}
}
SC.Add(s);
}
//可自定义填充图的填充色,系统采取默认分配各数据项的填充色
//SC[0].DefaultElement.Color = Color.Blue;
//SC[1].DefaultElement.Color = Color.Red;
//SC[2].DefaultElement.Color = Color.FromArgb(255, 99, 49);
//SC[3].DefaultElement.Color = Color.FromArgb(0, 156, 255);
return SC;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Drawing(DropDownList1.SelectedValue);
}
}
}

解释:我用的是MVC,所以ASP界面我用iframe调出。关键代码都有注释。

分别把销售量和销售额给算出来。首先定义报表类SeriesCollection。

Series 为一个报表汇总。Element顾名思义是报表里的元数据。X轴为月份。Y轴为自定义数据。

<div style="float: left; width: 100%; margin: 0 auto; border: solid 1px #DDDDDD;">
<iframe id="no1" width="700" height="440" scrolling="no" src="../../Statistics.aspx"></iframe>
</div>

效果图如下:

ASP.NET基于donetCHARTING的自动报表