Chart.js data array using PHP, MySQL. How to determine the data source from a JSON array?

This is my first question. I apologize for any errors.

I am trying to draw a chart using chart.js with data from PHP and MySQL. The graph that I intend to draw is a simple vertical velvet, birth_year and the number of people born. When I show the $ BIRTH_YEAR and $ COUNTS arrays, I could see the values. I was able to get to json_encode point ($ data_array). When I try to use this encoded array in javascript, I get no output, a blank page! Here is my code.

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $data[] = array( $row['BIRTH_YEAR']=>$row['counts'], ); $BIRTH_YEAR[]=$row['BIRTH_YEAR']; $COUNTS[]=$row['counts']; } // JSON arrays for labels and counts $js_labels = json_encode($BIRTH_YEAR,true); $js_cols = json_encode($COUNTS,true); var barChartData = { labels : '<?php echo $js_labels; ?>', datasets : [ { fillColor : "rgba(220,220,220,0.5)", strokeColor : "rgba(220,220,220,1)", data : '<?php echo $js_cols; ?>' } ] } var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Bar(barChartData); 

I have included all the other necessary HTML elements on my page. When I use the chart.js sample file, I could see graphs. The only problem is that I'm not sure how to include arrays in javascript: part data. Thank you in advance.

+6
source share
1 answer

You can use $js_cols = json_encode($COUNTS,JSON_NUMERIC_CHECK);

and then

 data : <?php echo print_r($js_cols,true); ?> 
+2
source

Source: https://habr.com/ru/post/950774/


All Articles