使用以下代码,我正在尝试创建一个Morris Donut图表。但我没有按预期得到输出。 这是我第一次尝试通过从mysql获取数据来创建morris图表。所以,如果有任何重大错误,请原谅我。 这是代码" morris.php"
<?php
require_once('connection.php');
?>
<html>
<head>
<link rel="stylesheet" href="morris.css">
<script src="jquery.min.js"></script>
<script src="raphael-min.js"></script>
<script src="morris.min.js"></script>
<meta charset=utf-8 />
</head>
<body>
<?php
$sql= "select wlt_txn_cat as cat , sum(wlt_txn_amount) as amt from wallet_txns where wlt_txn_type = 'Expense' group by wlt_txn_amount desc";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
if ( mysqli_num_rows($result) >0)
while($row = mysqli_fetch_array($result))
{
?>
<div id="donut-example" style="height: 250px;"></div>
<script type="application/javascript">
Morris.Donut({
element: 'donut-example',
data: [
{label: "<?php echo $row['cat'] ?>", value: "<?php echo $row['amt'] ?>"}
]
});
</script>
<?php } ?>
</body>
</html>
屏幕截图....
获取正确输出的实际代码(所附图片的右侧是下面的...我正在尝试获取&#39;标签&#39;以及&#39;来自mysql db使用php。
Morris.Donut({
element: 'donut-example',
data: [
{label: "Download Sales", value: 15},
{label: "In-Store Sales", value: 30},
{label: "Mail-Order Sales", value: 20},
{label: "General Sales", value: 40}
],
backgroundColor: '#ccc',
labelColor: '#060',
colors: ['#DD4B39','#4486F7','#FAC504','#019C5A']
});
答案 0 :(得分:1)
最后,我得到了一个解决方案,在谷歌和Stackoverflow的帮助下(搜索文章和参考文献)。我不知道这是否是最好的解决方案。但它像我预期的那样对我有用。 这是使用的完整代码。
<html>
<head>
<link rel="stylesheet" href="morris.css">
<script src="jquery.min.js"></script>
<script src="raphael-min.js"></script>
<script src="morris.min.js"></script>
<meta charset=utf-8 />
</head>
<body>
<?php
$db=new PDO('mysql:dbname=mydb;host=localhost;','root','');
$row=$db->prepare ("SELECT wlt_txn_cat as cat,sum(wlt_txn_amount)as amt FROM wallet_txns where wlt_txn_cat <> 'Transfer' and wlt_txn_type = 'Expense' and wlt_txn_date between DATE (DATE_SUB(LAST_DAY(DATE_ADD(NOW(), INTERVAL 0 MONTH)),INTERVAL DAY(LAST_DAY(DATE_ADD(NOW(), INTERVAL 0 MONTH)))-1 DAY)) and date(CURDATE()) group by wlt_txn_cat ORDER BY wlt_txn_amount desc ");
$row->execute();
$json_data=array();
foreach($row as $rec)
{
$json_array['label']=$rec['cat'];
$json_array['value']=$rec['amt'];
array_push($json_data,$json_array);
}
{ ?>
<div id="donut-example" style="height: 250px;"></div>
<script type="application/javascript">
Morris.Donut({
element: 'donut-example',
data: <?php echo json_encode($json_data)?>
});
</script>
<?php } ?>
</body>
</html>
答案 1 :(得分:0)
如果有重音字母,请记住:放
$db->query('SET NAMES utf8');
在PHP文件中连接数据库后**
否则,您将获得null
值而不是标签。
感谢这篇文章以及以下内容: Accented characters in mySQL table