HighChar 案例

Highchars

//前台



 

 

//后台

public string SqlConnectionString = ConfigurationManager.ConnectionStrings["sqlString"].ConnectionString;

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
var op = context.Request.QueryString["op"];
if (op == "pie")
{
context.Response.Write(new JavaScriptSerializer().Serialize(LoadPieData()));
}
else
{
context.Response.Write(new JavaScriptSerializer().Serialize(LoadOrderData()));
}


}

 

public bool IsReusable
{
get
{
return false;
}
}

/// 
/// 加载曲线图
/// 
/// 
public string LoadOrderData()
{
string sql = @"select convert(int,DAY(CreateTime),120) as 'day',sum(OrderAmount) 
as 'sum' from OrderInfo group by convert(int,DAY(CreateTime),120) 
order by convert(int,DAY(CreateTime),120) asc";

DataSet ds = SqlHelper.ExecuteDataset(SqlConnectionString, CommandType.Text, sql);
//调用方法将dataset转换为json格式的数据
var result = GetJsonByDataset(ds);

return result;
}


/// 
/// 统计每个订单类型的销售额
/// 
/// 
public string LoadPieData()
{
string sql = @"select SUM(OrderAmount) as totalMoney,OrdertType=Case OrdertType
when 1 then '家电'
when 2 then '生活用品'
else '其他' end 
from OrderInfo group by(OrdertType)
";

DataSet ds = SqlHelper.ExecuteDataset(SqlConnectionString, CommandType.Text, sql);
//调用方法将dataset转换为json格式的数据
var result = GetJsonByDataset(ds);
return result;
}


//StringBuilder 如果是长字符串拼接就用stringbuilder(比如sql拼接,html拼接)


//string 如果是短字符串拼接就用string

/// 把dataset数据转换成json的格式
/// 
/// dataset数据集
/// json格式的字符串
public static string GetJsonByDataset(DataSet ds)
{
if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)
{
//如果查询到的数据为空则返回标记ok:false
return "{\"ok\":false}";
}
StringBuilder sb = new StringBuilder();
sb.Append("{\"ok\":true,");
foreach (DataTable dt in ds.Tables)
{
sb.Append(string.Format("\"{0}\":[", dt.TableName));

foreach (DataRow dr in dt.Rows)
{
sb.Append("{");
for (int i = 0; i < dr.Table.Columns.Count; i++)
{
sb.AppendFormat("\"{0}\":\"{1}\",", dr.Table.Columns[i].ColumnName.Replace("\"", "\\\"").Replace("\'", "\\\'"), ObjToStr(dr[i]).Replace("\"", "\\\"").Replace("\'", "\\\'")).Replace(Convert.ToString((char)13), "\\r\\n").Replace(Convert.ToString((char)10), "\\r\\n");
}
sb.Remove(sb.ToString().LastIndexOf(','), 1);
sb.Append("},");
}

sb.Remove(sb.ToString().LastIndexOf(','), 1);
sb.Append("],");
}
sb.Remove(sb.ToString().LastIndexOf(','), 1);
sb.Append("}");
return sb.ToString();
}

/// 
/// 将object转换成为string
/// 
/// obj对象
/// 
public static string ObjToStr(object ob)
{
if (ob == null)
{
return string.Empty;
}
else
return ob.ToString();
}

 

你可能感兴趣的:(HighChar 案例)