javascript - PHP Displaying site visits in a line graph VIA Morris.js -
i following tutorial: codediesel trying create line graph show amount of views per day on page of website. have database set like:
create table `pageviews` ( `id` int(11) not null auto_increment, `date` date not null, `views` bigint(20) not null, primary key (`id`) ) engine=innodb default charset=latin1
i have data in table, 3 days, data looks following:
array(3) { [0]=> array(3) { ["id"]=> int(3) ["date"]=> string(10) "2015-06-19" ["views"]=> int(49) } [1]=> array(3) { ["id"]=> int(2) ["date"]=> string(10) "2015-06-18" ["views"]=> int(103) } [2]=> array(3) { ["id"]=> int(1) ["date"]=> string(10) "2015-06-17" ["views"]=> int(18) } }
my problem page gives me no graph, console produces 2 errors:
uncaught typeerror: cannot read property 'match' of undefined uncaught error: graph container element not found
my morris javascript looks when rendered page:
morris.line({ element: 'morris-line-chart', data: [{"id":3,"date":"2015-06-19","views":49},{"id":2,"date":"2015-06-18","views":103},{"id":1,"date":"2015-06-17","views":18}], xkey: 'cron_time', ykeys: ['page views'], labels: ['page views'], linecolors: ['#0b62a4'], xlabels: 'date', smooth: true, resize: true });
without rendering looks like:
<div id="morris-line-chart"> <?php $db = new pdo('mysql:host=localhost;dbname=**********;charset=utf8', '*********', '********'); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); $db->setattribute(pdo::attr_emulate_prepares, false); $stmt = $db->prepare("select * pageviews order `id` desc limit 15"); $stmt->execute(); $row = $stmt->fetchall(pdo::fetch_assoc); ?> <script> morris.line({ element: 'morris-line-chart', data: <?php echo json_encode($row);?>, xkey: 'cron_time', ykeys: ['page views'], labels: ['page views'], linecolors: ['#0b62a4'], xlabels: 'date', smooth: true, resize: true }); </script> </div>
i not sure put php , , when copied code here had between tags have placed above , below try , rule out other possibilities. there see can me working. feel though close can't quite find last few errors. trying provide code , detail possible, if there you'd see forgetting please let me know.
***edit:
i fixed issue, needed change xkey , ykey work, works now!
change xkey date relates json array , change ykeys, placement goes after
Comments
Post a Comment