我正在使用ASP.NET MVC和谷歌图表来尝试生成一个简单的线图和两个数据记录。我从数据库中成功提取数据,但数据未显示在我的图表上。数据由两个记录组成:两个字段:WeekOfEntry
(DateTime)和Weight
(十进制)。图表出现,但数据点不存在。我猜我的数据格式不正确?谷歌图表json数据没有加载
这里是我的javascript:
<script type="text/javascript">
//Load the Visualization API library and the linechart library.
google.load('visualization', '1.0', { 'packages': ['corechart'] });
//Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawLoseATonLineChart);
//Callback that creates and populates a data table, instantiates the line chart,
//passes in the data, and draws it.
function drawLoseATonLineChart() {
var url = "@Url.Action("GetChartStatistics")";
var jsonData = $.ajax({
method: 'GET',
url: url,
dataType: 'JSON',
async: false
}).responseText;
var data = new google.visualization.DataTable();
data.addColumn('string', 'WeekOfEntry');
data.addColumn('number', 'Weight');
for (var i = 0; i < data.length; i++) {
data.addRow([jsonData[i].WeekOfEntry, jsonData[i].Weight]);
}
var options = {
title: 'Weight Progression',
legend: {
position: 'right',
alignment: 'center'
},
vAxis: {
title: 'Weight'
},
hAxis: {
title: 'Week',
slantedText: true,
slantedTextAngle: 45
},
colors: ['E81A00']
};
var chart = new google.visualization.LineChart(document.getElementById('lose-a-ton-line-chart'));
chart.draw(data, options);
}
</script>
这里是我的GetChartStatistics()
方法的一部分:
var lineChartData = (from a in db.Associates
join aw in db.AssociateWeights
on a.RegistrationId equals aw.RegistrationId
where a.EventId == eventId &&
a.Username == currentuser
select new LineChartData
{
Weight = aw.Weight,
WeekOfEntry = aw.WeekOfEntry
});
return Json(lineChartData, JsonRequestBehavior.AllowGet);
编辑:这里是如何,当它被退回我的JSON数据被格式化:
"[{"Weight":190.0,"WeekOfEntry":"\/Date(1431921600000)\/"},{"Weight":121.0,"WeekOfEntry":"\/Date(1432526400000)\/"}]"
关于为什么数据未加载的任何想法ING?
我尝试这样做,我接收到一个错误, “第一行不是数组”。 –