Мне нужно следить за состоянием машины и составлять временную шкалу.Данные собираются из таблицы Mysql, конвертируются в формат Json и (теоретически) должна отображаться временная шкала.
Все работает хорошо, начиная с получения данных Mysql, заканчивая преобразованием в Json и распечаткой, чтобы проверить это: {"cols": [{ "метка": "Macchina", "тип": "строка"}, { "метка": "Стато", "тип": "строка"}, { "метка": "Inizio", "тип": "дата"}, { "метка": "Хорошо", "тип": "дата"}], "строка": [{ "с": [{ "v": "2"}, { "v": "PROD"}, {"v": "Дата (2019, 4, 9, 10, 43, 17)"}, {"v": "Дата (2019, 4, 10, 10, 43, 24)"}]}, {"c": [{"v": "1"}, {"v": "ALL"}, {"v": "Date (2019, 4, 6, 2, 3, 35)"}, {" v ":" Дата (2019, 4, 6, 2, 0, 0) "}]}, {" c ": [{" v ":" 2 "}, {" v ":"SET "}, {" v ":" Дата (2019, 4, 6, 16, 0, 51) "}, {" v ":" Дата (2019, 4, 6, 19, 0, 0) "}]}, {"c": [{"v": "3"}, {"v": "PROD"}, {"v": "Дата (2019, 4, 7, 14, 4, 43)"}, {"v": "Дата (2019, 4, 7, 14, 39, 41)"}]}]}
Но диаграмма не отображается.Нет ошибок Почему ???
<html>
<head>
<title>Grafico Stati Macchina</title>
<script type = "text/javascript" src = "https://www.gstatic.com/charts/loader.js">
</script>
<script type = "text/javascript" src = "https://www.google.com/jsapi">
</script>
<script type = "text/javascript">
google.charts.load('current', {packages: ['timeline']});
</script>
</head>
<body>
<?php
$// Initialize variable for database credentials
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'rp_rproject';
//Create database connection
$dblink = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
//Check connection was successful
if ($dblink->connect_errno) {
printf("Failed to connect to database");
exit();
}
//Initialize array variable
$dbdata = array();
// Aggiunta
$rows = array();
$table = array();
$table['cols'] = array(
array('label' => 'Macchina', 'type' => 'string'),
array('label' => 'Stato', 'type' => 'string'),
array('label' => 'Inizio', 'type' => 'date'),
array('label' => 'Fine', 'type' => 'date'),
);
//Fetch 3 rows from actor table
$result = $dblink->query("SELECT * FROM allarmi2");
/* Extract the information from $result */
foreach($result as $r)
{
// This conversion because JSON doesen't take Date Object
// assumes dates are patterned 'yyyy-MM-dd hh:mm:ss'
preg_match('/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})/', $r["Inizio"], $match);
//$date = date('D M d Y H:i:s O',$r['time-on'], $match);
$year = (int) $match[1];
$month = (int) $match[2] - 1; // convert to zero-index to match javascript's dates
$day = (int) $match[3];
$hours = (int) $match[4];
$minutes = (int) $match[5];
$seconds = (int) $match[6];
$temp = array();
$temp[] = array('v' => $r['Descrizione']);
$temp[] = array('v' => $r['Note']);
$temp[] = array('v' => "Date($year, $month, $day, $hours, $minutes, $seconds)");
preg_match('/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})/', $r["Fine"], $match);
//$date = date('D M d Y H:i:s O',$r['time-off'], $match);
$year = (int) $match[1];
$month = (int) $match[2] - 1; // convert to zero-index to match javascript's dates
$day = (int) $match[3];
$hours = (int) $match[4];
$minutes = (int) $match[5];
$seconds = (int) $match[6];
$temp[] = array('v' => "Date($year, $month, $day, $hours, $minutes, $seconds)");
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
// convert data into JSON format
$jsonTable = json_encode($table);
//Print array in JSON format
echo json_encode($table);
//close the connection
mysql_close($dbhandle);
?>
<div id = "container">
</div>
<script language = "JavaScript">
function drawChart() {
var dataTable = google.visualization.DataTable(<?php echo json_encode($table, JSON_NUMERIC_CHECK) ?>);
//var dataTable = new google.visualization.DataTable(<?php echo $jsonTable; ?>);
var options = { colors: ['orange','green','blue','yellow','violet'],
timeline: { showRowLabels: true ,groupByRowLabel:true, colorByRowLabel: false},
avoidOverlappingGridLines: false};
// Instantiate and draw the chart.
var chart = new google.visualization.Timeline(document.getElementById('container'));
chart.draw(dataTable, options);
}
google.charts.setOnLoadCallback(drawChart);
</script>
</body>
</html>