How can I create the JSON source expected by the google.visualization.datatable using C#? Obviously using the JavaScriptSerializer
is out of the question since the expected JSON has a weird structure as described on the documentation:
如何创建google.visualization所需的JSON源。datatable使用c#吗?显然,使用JavaScriptSerializer是不可能的,因为预期的JSON有一个奇怪的结构,如文档中描述的那样:
var dt = new google.visualization.DataTable(
{
cols: [{id: 'task', label: 'Task', type: 'string'},
{id: 'hours', label: 'Hours per Day', type: 'number'}],
rows: [{c:[{v: 'Work'}, {v: 11}]},
{c:[{v: 'Eat'}, {v: 2}]},
{c:[{v: 'Commute'}, {v: 2}]},
{c:[{v: 'Watch TV'}, {v:2}]},
{c:[{v: 'Sleep'}, {v:7, f:'7.000'}]}
]
},
0.6
)
4 个解决方案
#1
8
Though I'm not familiar in the .NET environment, there is a .NET helper for the Google Visualization API called bortosky-google-visualization. The library writes a JSON Google DataTable from a System.Data.DataTable
object.
虽然我对。net环境并不熟悉,但是有一个。net助手可以帮助实现谷歌可视化API,叫做bortosky- Google可视化。库从System.Data中写入JSON谷歌数据表。DataTable对象。
#2
3
Another way to achieve this is to use the Google DataTable .Net Wrapper (https://googledatatablelib.codeplex.com/) which gives a possibility to work with a strongly typed System.DataTable that can then be converted into the google.datatable visualization JSON format.
实现这一点的另一种方法是使用谷歌DataTable . net包装器(https://googledatatablelib.codeplex.com/),它提供了使用强类型系统的可能性。然后可以转换为谷歌的DataTable。datatable可视化JSON格式。
This server side code
这个服务器端代码
public string GetStatisticsForChart(string messageCode)
{
//some repository that returns data....
var data = _statisticsRepository.GetPerMessage(messageCode);
//It simply returns a list of objects with Year and Count properties.
var query = (from t in data
group t by new {t.TimeStamp.Year}
into grp
select new
{
grp.Key.Year,
Count = grp.Count()
}).ToList();
//let's instantiate the DataTable.
var dt = new Google.DataTable.Net.Wrapper.DataTable();
dt.AddColumn(new Column(ColumnType.String, "Year", "Year"));
dt.AddColumn(new Column(ColumnType.Number, "Count", "Count"));
foreach (var item in query)
{
Row r = dt.NewRow();
r.AddCellRange(new Cell[]
{
new Cell(item.Year),
new Cell(item.Count)
});
dt.AddRow(r);
}
//Let's create a Json string as expected by the Google Charts API.
return dt.GetJson();
}
would generate the following JSON output
会生成以下JSON输出吗
{
"cols": [
{"type": "string", "id": "Year", "label": "Year"},
{"type": "number", "id": "Count", "label": "Count"}
],
"rows": [
{"c": [{"v": "2011"}, {"v": "1860"}]},
{"c": [{"v": "2012"}, {"v": "2000"}]}
]
}
and this can be used either in Asp.NET WebAPI or directly in the ASP.NET MVC controller.
这可以在Asp中使用。NET WebAPI或直接在ASP中。净MVC控制器。
#3
1
The expected JSON in the example above is not JSON but a Javascript object literal. JSON is only a subset of Javascript object literal notation, but the example above should (and does) also work, if the Google DataTable is initialized with a similar looking JSON. (To get proper JSON just put double quotes around the keys.)
上面示例中期望的JSON不是JSON,而是Javascript对象文本。JSON只是Javascript对象文字表示法的一个子集,但是如果谷歌DataTable被初始化为类似的JSON,那么上面的示例也应该(而且确实)起作用。(要获得合适的JSON,只需在键周围加上双引号即可。)
So actually you can use the DataContractJsonSerializer
or JavaScriptSerializer
to construct JSON for the Google DataTable. However, if your starting point is a System.Data.DataTable
it is probably easier to use the library mentioned in the answer above.
实际上,您可以使用DataContractJsonSerializer或JavaScriptSerializer为谷歌DataTable构造JSON。但是,如果您的起点是System.Data。使用上述答案中提到的库可能更容易。
#4
0
The best way to create google charts datatable via c# is to iterate data and fill model class and return filled model as JSON , here is the model representation :-
通过c#创建谷歌图表数据表的最佳方法是迭代数据并填充模型类,并以JSON的形式返回填充的模型,这里是模型表示:-
public class GChartsDataTbl
{
public List<Col> cols { get; set; }
public List<Row> rows { get; set; }
}
public class Col
{
public string id { get; set; }
public string type { get; set; }
}
public class C
{
public string v { get; set; }
public object f { get; set; }
}
public class Row
{
public List<C> c { get; set; }
}
#1
8
Though I'm not familiar in the .NET environment, there is a .NET helper for the Google Visualization API called bortosky-google-visualization. The library writes a JSON Google DataTable from a System.Data.DataTable
object.
虽然我对。net环境并不熟悉,但是有一个。net助手可以帮助实现谷歌可视化API,叫做bortosky- Google可视化。库从System.Data中写入JSON谷歌数据表。DataTable对象。
#2
3
Another way to achieve this is to use the Google DataTable .Net Wrapper (https://googledatatablelib.codeplex.com/) which gives a possibility to work with a strongly typed System.DataTable that can then be converted into the google.datatable visualization JSON format.
实现这一点的另一种方法是使用谷歌DataTable . net包装器(https://googledatatablelib.codeplex.com/),它提供了使用强类型系统的可能性。然后可以转换为谷歌的DataTable。datatable可视化JSON格式。
This server side code
这个服务器端代码
public string GetStatisticsForChart(string messageCode)
{
//some repository that returns data....
var data = _statisticsRepository.GetPerMessage(messageCode);
//It simply returns a list of objects with Year and Count properties.
var query = (from t in data
group t by new {t.TimeStamp.Year}
into grp
select new
{
grp.Key.Year,
Count = grp.Count()
}).ToList();
//let's instantiate the DataTable.
var dt = new Google.DataTable.Net.Wrapper.DataTable();
dt.AddColumn(new Column(ColumnType.String, "Year", "Year"));
dt.AddColumn(new Column(ColumnType.Number, "Count", "Count"));
foreach (var item in query)
{
Row r = dt.NewRow();
r.AddCellRange(new Cell[]
{
new Cell(item.Year),
new Cell(item.Count)
});
dt.AddRow(r);
}
//Let's create a Json string as expected by the Google Charts API.
return dt.GetJson();
}
would generate the following JSON output
会生成以下JSON输出吗
{
"cols": [
{"type": "string", "id": "Year", "label": "Year"},
{"type": "number", "id": "Count", "label": "Count"}
],
"rows": [
{"c": [{"v": "2011"}, {"v": "1860"}]},
{"c": [{"v": "2012"}, {"v": "2000"}]}
]
}
and this can be used either in Asp.NET WebAPI or directly in the ASP.NET MVC controller.
这可以在Asp中使用。NET WebAPI或直接在ASP中。净MVC控制器。
#3
1
The expected JSON in the example above is not JSON but a Javascript object literal. JSON is only a subset of Javascript object literal notation, but the example above should (and does) also work, if the Google DataTable is initialized with a similar looking JSON. (To get proper JSON just put double quotes around the keys.)
上面示例中期望的JSON不是JSON,而是Javascript对象文本。JSON只是Javascript对象文字表示法的一个子集,但是如果谷歌DataTable被初始化为类似的JSON,那么上面的示例也应该(而且确实)起作用。(要获得合适的JSON,只需在键周围加上双引号即可。)
So actually you can use the DataContractJsonSerializer
or JavaScriptSerializer
to construct JSON for the Google DataTable. However, if your starting point is a System.Data.DataTable
it is probably easier to use the library mentioned in the answer above.
实际上,您可以使用DataContractJsonSerializer或JavaScriptSerializer为谷歌DataTable构造JSON。但是,如果您的起点是System.Data。使用上述答案中提到的库可能更容易。
#4
0
The best way to create google charts datatable via c# is to iterate data and fill model class and return filled model as JSON , here is the model representation :-
通过c#创建谷歌图表数据表的最佳方法是迭代数据并填充模型类,并以JSON的形式返回填充的模型,这里是模型表示:-
public class GChartsDataTbl
{
public List<Col> cols { get; set; }
public List<Row> rows { get; set; }
}
public class Col
{
public string id { get; set; }
public string type { get; set; }
}
public class C
{
public string v { get; set; }
public object f { get; set; }
}
public class Row
{
public List<C> c { get; set; }
}