我使用 Google.DataTable.Net.Wrapper 将 JSON 数据传递到 Google 图表并尝试创建折线图。对我的 Web 服务的调用正在传回 JSON,这对我来说看起来完全有效,但我只收到来自 Google 的“表没有列”错误。我一直在看这个太久了,没有看到问题是什么,所以任何帮助将不胜感激。
我渲染的 JS 代码如下:
<script type=text/javascript>
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
type: "POST",
url: "ChartData.asmx/getChartData",
data: "{'numberToReturn': 'AvgPriceSqFt', 'parish': 'EBR', 'sub': 'AZALEA LAKES', 'timeFrame': '1Year'}",
dataType: "json",
contentType: "application/json; charset=utf-8",
async: false
}).responseText;
var data = new google.visualization.DataTable(jsonData, 0.6);
var options = { width: 1000, curveType: "function", vAxis: { maxValue: 200 },
title: "Average Price Per Square Foot"
};
var chart = new google.visualization.LineChart(document.getElementById('AvgPriceSqFt'));
chart.draw(data, options);
}
</script>
<div id="AvgPriceSqFt"></div>
返回的 JSON 如下(对我来说看起来完全有效)
{
"cols":
[
{"type": "string" ,"id": "Date" },
{"type": "number" ,"id": "AZALEA LAKES" }
],
"rows" :
[
{"c" : [{"v": "01-2013"}, {"v": 128.6}]},
{"c" : [{"v": "02-2013"}, {"v": 115.84}]},
{"c" : [{"v": "03-2013"}, {"v": 113.7}]},
{"c" : [{"v": "04-2013"}, {"v": 118.09}]},
{"c" : [{"v": "06-2013"}, {"v": 97.01}]},
{"c" : [{"v": "07-2013"}, {"v": 128.57}]},
{"c" : [{"v": "08-2013"}, {"v": 98.57}]},
{"c" : [{"v": "09-2013"}, {"v": 125.54}]}
]
}
使用 .NET 包装器生成数据的 .NET 代码如下:
[WebMethod]
public string getChartData(string numberToReturn, string parish, string sub, string timeFrame)
{
google.DataTable gdt = new google.DataTable();
sql.DataTable sdt = new sql.DataTable();
gdt.AddColumn(new google.Column(google.ColumnType.String, "Date", "Date"));
gdt.AddColumn(new google.Column(google.ColumnType.Number, sub, "Subdivision"));
sdt = GetSqlData(numberToReturn, parish, sub, timeFrame);
for (int i = 0; i <= sdt.Rows.Count - 1; i++)
{
var row = gdt.NewRow();
row.AddCellRange(new[] {
new google.Cell(sdt.Rows[i]["date"].ToString()),
new google.Cell(sdt.Rows[i]["number"].ToString())
});
gdt.AddRow(row);
}
return gdt.GetJson();
}