I have a JSON array passed via ajax and am having issues charting the data. I need to be able to pass the array to the chart dynamically adding columns without duplicates.
I've tried passing different values using arrayToDataTable() and DataTable() then using the data.addColumns() and data.addRow() however each time errors.
ajax call fired:
$.ajax({
type: "POST",
url: "/file.php",
data: {
data: dataHere,
},
dataType: "JSON",
success: function(result) {
var div = mydiv;
drawInterfaceChart(result,div);
},
});
JSON encoded from PHP using:
$SQL = "SELECT DATE_FORMAT(date, 'new Date(%Y, %c, %d, %H, %i, %s)') as date, ifDesc, ifInOctets FROM tablename WHERE date between (CURDATE() - INTERVAL 1 MONTH ) and CURDATE()";
$Results = mysqli_query($db, $SQL) or die("Mysql cannot run Query");
$SQLRows = mysqli_num_rows($Results);
if ($SQLRows > 0) {
$rows = array();
while ($row = MySQLi_fetch_assoc($Results)) {
$rows[] = $row;
}
echo json_encode($rows);
}
Example of a passed JSON array:
0: {date: "new Date(2019, 3, 26, 16, 13, 15)", ifDesc: "lo", ifInOctets: "2147483647"}
1: {date: "new Date(2019, 3, 26, 16, 13, 15)", ifDesc: "Port1", ifInOctets: "2147483647"}
2: {date: "new Date(2019, 3, 26, 16, 13, 15)", ifDesc: "Port2", ifInOctets: "2147483647"}
3: {date: "new Date(2019, 3, 26, 16, 13, 16)", ifDesc: "Port6", ifInOctets: "2147483647"}
4: {date: "new Date(2019, 3, 26, 16, 13, 16)", ifDesc: "imq0", ifInOctets: "906413834"}
5: {date: "new Date(2019, 3, 26, 16, 17, 31)", ifDesc: "lo", ifInOctets: "2147483647"}
6: {date: "new Date(2019, 3, 26, 16, 17, 49)", ifDesc: "Port1", ifInOctets: "2147483647"}
7: {date: "new Date(2019, 3, 26, 16, 17, 53)", ifDesc: "Port2", ifInOctets: "171330279"}
8: {date: "new Date(2019, 3, 26, 16, 17, 57)", ifDesc: "Port6", ifInOctets: "2147483647"}
9: {date: "new Date(2019, 3, 26, 16, 17, 57)", ifDesc: "imq0", ifInOctets: "1103910085"}
10: {date: "new Date(2019, 3, 26, 16, 20, 38)", ifDesc: "lo", ifInOctets: "2147483647"}
11: {date: "new Date(2019, 3, 26, 16, 20, 39)", ifDesc: "Port1", ifInOctets: "2147483647"}
12: {date: "new Date(2019, 3, 26, 16, 20, 40)", ifDesc: "Port2", ifInOctets: "194386054"}
13: {date: "new Date(2019, 3, 26, 16, 20, 41)", ifDesc: "Port6", ifInOctets: "2147483647"}
14: {date: "new Date(2019, 3, 26, 16, 20, 42)", ifDesc: "imq0", ifInOctets: "1128562685"}
My draw chart function:
function drawInterfaceChart(array,divR) {
var dataSet = [];
$.each(array, function (data, value) {
dataSet.push([value.date, value.ifDesc, value.ifInOctets]);
});
var data = google.visualization.arrayToDataTable(dataSet);
var chart = new google.visualization.LineChart(document.getElementById(divR));
var options = {
title: '',
legend: { position: 'right' }
};
chart.draw(data, options);
}
This layout returns the error: Data column(s) for axis #0 cannot be of type string
Column 0 needs to be a datetime, and a line chart presented with each column and it's data per 'ifDesc' value. I am looking to add more data into the chart so creating the columns needs to be dynamic.
Thanks in advance!
EDIT:
I forgot to mention my page does include: google.charts.load('current', {'packages':['corechart', 'gauge']}); and I have other working charts on the same page.