c# - Dynamically add rows to Google Charts using JQuery loop
问题描述
I want to loop through a JSON return from C# to dynamically add rows to a google chart. I have used this same code, time and again, and it will not load into the chart.
I can see the Json coming back to the browser and have tested it with RestClient so I am happy the C# bit works, but I am not seeing why the Json won't load into the Google line chart and create the line. There is an error showing on the chart Cannot read property 'getTime' of null
. I have tried a few different ways of creating the array for data.AddRow.
JQuery Ajax call:
$.ajax({
url: '@Url.Action("GetJsonChartData", "ProjectCharts")',
datatype: 'json',
type: 'get',
async: false,
data: { serial: serial, uid: uid, from: from, to: to },
contentType: 'application/json; charset=utf-8',
success: function (d) {
$.each(d, function (index, item) {
data.addRow([new Date(item.ReadingDate), Number(item.ReadingValue), item.ToolTip]);
});
},
error: function () {
alert("Error loading chart data.")
}
});
and my Json looks like:
[{
"ReadingDate": "2018-12-04 09:43:39",
"ReadingValue": "17.5",
"ToolTip": "Tue, 04 Dec 18 09:43\r\n Value: 17.5"
},
{
"ReadingDate": "2018-12-04 09:45:39",
"ReadingValue": "16.8",
"ToolTip": "Tue, 04 Dec 18 09:45\r\n Value: 16.8"
},
{
"ReadingDate": "2018-12-04 09:47:39",
"ReadingValue": "16.1",
"ToolTip": "Tue, 04 Dec 18 09:47\r\n Value: 16.1"
},
{
"ReadingDate": "2018-12-04 09:49:39",
"ReadingValue": "15.7",
"ToolTip": "Tue, 04 Dec 18 09:49\r\n Value: 15.7"
}]
I create the Json as follow:
public JsonResult GetJsonChartData(string serial, string uid, string from, string to) {
var g = new GetChartData(_configuration);
var items = g.GetChartDataFromSqlServer(serial, uid, f, t);
return Json(JsonConvert.SerializeObject(items, Formatting.Indented));
}
TIA
解决方案
图表似乎画得很好,使用相同的数据,只是在这里硬编码......
请参阅以下工作片段...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn({type: 'date', label: 'Date'});
data.addColumn({type: 'number', label: 'Value'});
data.addColumn({type: 'string', role: 'tooltip'});
var d = [{
"ReadingDate": "2018-12-04 09:43:39",
"ReadingValue": "17.5",
"ToolTip": "Tue, 04 Dec 18 09:43\r\n Value: 17.5"
},
{
"ReadingDate": "2018-12-04 09:45:39",
"ReadingValue": "16.8",
"ToolTip": "Tue, 04 Dec 18 09:45\r\n Value: 16.8"
},
{
"ReadingDate": "2018-12-04 09:47:39",
"ReadingValue": "16.1",
"ToolTip": "Tue, 04 Dec 18 09:47\r\n Value: 16.1"
},
{
"ReadingDate": "2018-12-04 09:49:39",
"ReadingValue": "15.7",
"ToolTip": "Tue, 04 Dec 18 09:49\r\n Value: 15.7"
}];
$.each(d, function (index, item) {
data.addRow([new Date(item.ReadingDate), Number(item.ReadingValue), item.ToolTip]);
});
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
推荐阅读
- android - 如何检查由单独 Android 应用程序中的服务启动的 Android 应用程序是否已完成加载?
- html - 为什么 GMail 在餐厅预订时忽略此微数据?
- jetty - 无法在码头嵌入式服务器中设置 MaxFormContentSize
- meteor - Meteor Google OAuth 错误:“无法完成与 Google 的 OAuth 握手”错误 401 Unauthorized
- jooq - JOOQ 连接两个具有相同列名的表
- mysql - 是否可以创建一个触发器,该触发器在将记录插入一个数据库但最终插入另一个数据库时执行?
- android - 更改 primaryKeys 顺序会更改 ROOM 中保存的数据的顺序
- c++ - 初始化自动变量时可能出现 gcc 一致性问题?
- javascript - Javascript从像素坐标计算数据索引
- python - 循环直到页面上的消息更改硒python