使用FusionCharts+Jquery实现报表钻取
2009-12-11 00:00:00 来源:WEB开发网对应的后台代码:
using System;
namespace FusionCharts_Jquery打造交换报表
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Random rand = new Random();//保证得到最新的数据
RegisterStartupScript("c", "<script>ShowReport(" + 550 + "," + 600 + ",'Column','../Source/MonthReport.xml?id=" + rand.Next(6000) + "');</script>");
}
}
}
}
提交ajax请求,获取钻取报表数据的处理程序:
using System.Web;
using System.Data;
using System.Text;
using Newtonsoft.Json;
using System.IO;
namespace FusionCharts_Jquery打造交换报表.Model
{
public class GetDrill : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//获取参数,返回子报表数据,数据格式为Json
if (!string.IsNullOrEmpty(context.Request.QueryString.GetValues("ParMonth")[0]))
{
string month = context.Request.QueryString.GetValues("ParMonth")[0];
context.Response.Write(GetDrillData(month));
}
}
//获取子报表数据,返回JSON数据
private string GetDrillData(string month)
{
//构建数据
DataTable table = new DataTable();
table.Columns.Add("First",typeof(string));
table.Columns.Add("Second", typeof(string));
table.Columns.Add("Third", typeof(string));
table.Columns.Add("Fourth", typeof(string));
for (int n = 1; n < 5; n++)
{
DataRow row = table.NewRow();
row[0] = month + "月,第" + n.ToString() + "周,第1列!";
row[1] = month + "月,第" + n.ToString() + "周,第2列!";
row[2] = month + "月,第" + n.ToString() + "周,第3列!";
row[3] = month + "月,第" + n.ToString() + "周,第4列!";
table.Rows.Add(row);
}
//使用第三方控件Newtonsoft.Json,解析DataTable为Json数据格式
StringBuilder sb = new StringBuilder();
System.IO.StringWriter sw = new StringWriter(sb);
using (JsonWriter jw = new JsonTextWriter(sw))
{
JsonSerializer ser = new JsonSerializer();
jw.WriteStartObject();
jw.WritePropertyName("DrillTable");
jw.WriteStartArray();
foreach (DataRow dr in table.Rows)
{
jw.WriteStartObject();
foreach (DataColumn dc in table.Columns)
{
jw.WritePropertyName(dc.ColumnName);
ser.Serialize(jw, dr[dc].ToString());
}
jw.WriteEndObject();
}
jw.WriteEndArray();
jw.WriteEndObject();
sw.Close();
jw.Close();
}
return sb.ToString();
}
//// <summary>
// //自定义方法,将DataTable转化为Json数据格式
// //</summary>
// //<param name="dt">数据表</param>
// //<returns>JSON字符串</returns>
//public string CreateJsonParameters(DataTable dt)
//{
// StringBuilder JsonString = new StringBuilder();
// if (dt != null && dt.Rows.Count > 0)
// {
// JsonString.Append("{ ");
// JsonString.Append("\"TableInfo\":[ ");
// for (int i = 0; i < dt.Rows.Count; i++)
// {
// JsonString.Append("{ ");
// for (int j = 0; j < dt.Columns.Count; j++)
// {
// if (j < dt.Columns.Count - 1)
// {
// JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
// }
// else if (j == dt.Columns.Count - 1)
// {
// JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
// }
// }
// if (i == dt.Rows.Count - 1)
// {
// JsonString.Append("} ");
// }
// else
// {
// JsonString.Append("}, ");
// }
// }
// JsonString.Append("]}");
// return JsonString.ToString();
// }
// else
// {
// return null;
// }
//}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Tags:使用 FusionCharts Jquery
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接