我需要监视机器状态并制定时间表。 从Mysql表收集数据,将其转换为Json格式,并且应该显示(理论上)时间轴。
一切正常,从获取Mysql数据,到转换为Json并打印出来进行验证: {“ cols”:[{“ label”:“ Macchina”,“ type”:“ string”},{“ label”:“ Stato”,“ type”:“ string”},{“ label”:“ Inizio” ,“ type”:“ date”},{“ label”:“精细”,“ type”:“ date”}],“行”:[{“ c”:[{“ v”:“ 2”}, {“ v”:“ PROD”},{“ v”:“ Date(2019,4,9,10,43,17)”},{“ v”:“ Date(2019,4,10,10,43 ,24)“}]},{” c“:[{” v“:” 1“},{” v“:” ALL“},{” v“:”日期(2019,4,6,2, 3,35)“},{” v“:”日期(2019,4,6,6,2,0,0)“}]},{” c“:[{” v“:” 2“},{” v“:” SET“},{” v“:” Date(2019,4,6,6,16,0,51)“},{” v“:” Date(2019,4,6,6,19,0,0 )“}]},{” c“:[{” v“:” 3“},{” v“:” PROD“},{” v“:” Date(2019,4,7,14,14,4, 43)“},{” v“:”日期(2019,4,7,14,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>