php - Morris Donut chart with data from mysql -
with below code trying create morris donut chart. not getting output expected. first time trying create morris chart fetching data mysql. if big mistake, please forgive me. here code "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 cat , sum(wlt_txn_amount) amt wallet_txns wlt_txn_type = 'expense' group 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>
the screen shots....
the actual code getting correct output (right side of image attached below one... trying 'label' , 'value' mysql db using 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'] });
at last got solution, of google , stackoverflow ( searching articles , references). dont know whether best solution or not. works me expected. here full code used.
<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 cat,sum(wlt_txn_amount)as amt wallet_txns wlt_txn_cat <> 'transfer' , wlt_txn_type = 'expense' , 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)) , date(curdate()) group wlt_txn_cat order wlt_txn_amount desc "); $row->execute(); $json_data=array(); foreach($row $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>
Comments
Post a Comment