我从控制器发送到Google图表视图的Json文件未显示。我认为其格式为json文件。我还将显示返回的json字符串显示在<code></code>
块中。
如果我手动对返回的json字符串进行硬编码,则会显示图表。控制器方法MakeStringDataTable()返回一个stringbuilder对象。同样在这篇文章的结尾是提琴手的价值观。
客户端代码
function GetChartData() {
alert("GetChartData()");
var url = '@Url.Action("GetChartData", "Graphing")';
var stockitems = $('#SelectedStockItemID'); // cache it
var stockGroups = $('#SelectedStockGroupID'); // cache it
var customers = $('#SelectedCustomerID'); // cache it
// public JsonResult GetChartData(int ? customerID, int ? stockItemID, int ? stockGroupID)
$.getJSON(url, { StockGroupID: stockGroups.val(), stockItemID: stockitems.val(), customerID: customers.val() },
function (response) {
alert("call back from getJSON()");
$("#codeDisplay").text(response);
// Load google charts
var data = new google.visualization.arrayToDataTable(response, false);
//var data = new google.visualization.arrayToDataTable([["Month", "2015", "2016"], ["Jan", 0, 14264.00], ["Feb", 0, 9465.70], ["Mar", 0, 1069.50], ["Apr", 0, 17061.00], ["May", 0, 14656.60], ["Dec", 6858.65, 0]], false);
alert("after arraytoDataTable");
// Optional; add a title and set the width and height of the chart
var options = { 'width': 1000, 'height': 600 };
// Display the chart inside the <div> element with id="piechart"
var chart = new google.visualization.LineChart(document.getElementById('SalesChart'));
chart.draw(data, options);
});
后面的代码
public JsonResult GetChartData(int? customerID, int? stockItemID, int? stockGroupID)
{
GraphSalesByYearMonth gsbym = DoGraphing(customerID, stockItemID, stockGroupID);
string s = gsbym.MakeStringDataTable().ToString();
//return Json(gsbym.MakeStringDataTable(), JsonRequestBehavior.AllowGet);
//string xx = JsonConvert.SerializeObject(gsbym.MakeStringDataTable());
return Json(s, JsonRequestBehavior.AllowGet);
}
json数据
"[[\"Month\",\"2015\",\"2016\"],[\"Jan\",0,5377.15],[\"Feb\",0,10770.50],[\"Mar\",0,17610.00],[\"Apr\",0,17529.90],[\"May\",0,6663.80],[\"Oct\",514.00,0],[\"Dec\",1506.00,0]]"