C# 使用D3js,如何从数据库创建flare.json结构?
实际上,这是一个相当主观的问题,但是我只是尝试探索一些选项来为我的D3js可视化创建flare.json输出 目前,我有一个D3js的JSON结构,如下所示:C# 使用D3js,如何从数据库创建flare.json结构?,c#,d3.js,visualization,data-visualization,C#,D3.js,Visualization,Data Visualization,实际上,这是一个相当主观的问题,但是我只是尝试探索一些选项来为我的D3js可视化创建flare.json输出 目前,我有一个D3js的JSON结构,如下所示: { "name": "Engage Stats", "children": [ { "name": "Unique Requests by Device", "children": [ {"nam
{
"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
正在使用存储过程从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
为了完整起见,为了结束这个问题,我最终选择了一种很好的老式方式。。我猜这不是最好的,但它确实起作用了
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