Jump to content

anyone help me with chartjs adn mysql, please


kiwari

Recommended Posts

i want to build a bar chart with chartJS and database mysql with two different query, but bar chart not showing. can anyone help me to correct the code?

this my query and javascript:

<?php

$a = mysqli_query("select count(a) as total1 from tbl");

$b = mysqli_query("select count(b) as total2 from tbl");

?>

<body>

<div>

<canvas id="chart"></canvas>

</div>

 

<script>

var ctx = document.getElementById("chart");

var chartdata = {

labels: 'CHART',

datasets: [{

label: "A",

data: [<?php while ($data1 = mysqli_fetch_array($a)) {echo '"''.$data1['total1'].'",';}?>],

backgroundColor: "#000000"

},{

data: [<?php while ($data2 = mysqli_fetch_array($b)) {echo '"''.$data2['total2'].'",';}?>],

backgroundColor: "#ffffff"

}]

};

 

var chart = new Chart(ctx, {

type: 'bar',

data: chartdata

});

</script>

</body>

Link to comment
Share on other sites

thanks for replying, i'm new in this programming world. in browser page there is no error messages but chart not showing. and in developer tools, it shows error uncaught TypeError at chart.bundle.js and at line var chart = new Chart(ctx, {

 

Link to comment
Share on other sites

Then that's where you need to look.  You need to look at the code that PHP is producing though, in your browser you need to view the source of the page and check to make sure that the Javascript code that you're having PHP output is valid code.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...