【发布时间】:2017-08-04 13:48:15
【问题描述】:
我需要使用chartJS创建一个从mysql获取数据的图表,下面是mysql表的示例输出:
+----+------------+----------+-----------------+----------
| ID | DATE | Time | Create | Delete | Product |
+----+------------+----------+-----------------+---------+
| 1 | 03/12/2017 | 09:00:00 | 28 | 1264 | 59 |
我设法将数据转换为 JSON 格式,使用 toturial here,下面是 PHP 文件:
get.php
<?php
$DB_NAME = 'testDB';
$DB_HOST = 'localhost';
$db_port = '3306';
$DB_USER = 'test';
$DB_PASS = 'mysql';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$result = $mysqli->query('Select * from (SELECT * FROM Stats ORDER BY id DESC LIMIT 48)t ORDER BY id ASC');
$rows = array();
$table = array();
$table['cols'] = array(
array('label' => 'Time', 'type' => 'string'),
array('label' => 'ProductCreate', 'type' => 'number'),
array('label' => 'ProductDelete', 'type' => 'number'),
array('label' => 'ProductRenew', 'type' => 'number')
);
foreach($result as $r) {
$temp = array();
$temp[] = array('v' => (string) $r['Time']);
$temp[] = array('v' => (int) $r['Create']);
$temp[] = array('v' => (int) $r['Delete']);
$temp[] = array('v' => (int) $r['Renew']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
// convert data into JSON format
$SubsStats = json_encode($table);
echo $SubsStats;
?>
我现在的问题是如何将 PHP 输出包含到我的 HTML 文件中并使用 ChartJS 绘制图表?
谢谢, 阿里
【问题讨论】:
-
使用上面的“SubsStats”json数据并传递给javacript函数,在chartJS支持的函数的帮助下创建图表。