实际上,这是一个主观的问题,但是我只是想探索一些选项,以为我的D3js可视化创建flare.json输出。
目前,我具有D3js的JSON结构,如下所示:
{
"name": "Engage Stats",
"children":
[
{
"name": "Unique Requests by Device",
"children":
[
{"name": "Android", "size": 80},
{"name": "IOS", "size": 366}
]
},
{
"name": "Overall Requests by Device",
"children":
[
{"name": "Android", "size": 2645},
{"name": "IOS", "size": 11703}
]
},
.... etc etc
我的数据正在使用存储的proc从MS-SQL检索。显然,一种方法是简单地通读我的数据集(我应该说我在后端使用C#/。NET)并逐行构建JSON结构。主意!
我猜是因为我将flare.json格式用于更多的可视化效果,所以我可以创建一个库来完成繁重的工作-再次,只是对实现此目标的其他观点感兴趣。
只是为了完整起见,并为了结束这个问题,我最终还是采用了一种老式的方法。.我猜不是最好的方法,但是它确实起作用了!
using (SqlDataReader dr = command.ExecuteReader())
{
// Check if we have data or not - no need to create the excel file if no data
if (!dr.HasRows)
{
JSONdata = null;
return false;
}
StringBuilder json = new StringBuilder();
string LastGroup = null;
bool Init = true;
json.AppendLine("{");
while (dr.Read())
{
if (Init)
{
json.AppendLine("\"name\": \"" + dr["Organisation"] + "\",");
json.AppendLine("\"children\": ");
json.AppendLine("[");
}
if (LastGroup != dr["GroupIdentifier"].ToString())
{
if (Init == true)
{
json.AppendLine("{");
json.AppendLine("\"name\": \"" + dr["GroupIdentifier"] + "\",");
json.AppendLine("\"children\":");
json.AppendLine("[");
Init = false;
}
else
{
var index = json.ToString().LastIndexOf(',');
if (index >= 0)
{
json.Remove(index, 1);
}
json.AppendLine("]");
json.AppendLine("},");
json.AppendLine("{");
json.AppendLine("\"name\": \"" + dr["GroupIdentifier"] + "\",");
json.AppendLine("\"children\":");
json.AppendLine("[");
}
LastGroup = dr["GroupIdentifier"].ToString();
}
json.AppendLine("{\"name\": \"" + dr["Measure"] + "\", \"size\": " + dr["MeasureValue"].ToString() + "},");
}
var index2 = json.ToString().LastIndexOf(',');
if (index2 >= 0)
{
json.Remove(index2, 1);
}
json.AppendLine("]");
json.AppendLine("}");
json.AppendLine("]");
json.AppendLine("}");
JSONdata = json.ToString();
return true;
} // end SqlRdr
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句