[Chartjs]-Connecting Chart.js to MySQL database

3👍

If you have your data in a php array and your labels in another php array then you can just use the json_encode function to pass your data to chartjs.

With your $this->list_excercise you could do this :

<?php
    $data = array();
    $label = array();
    foreach ($this->list_excercise as $value) :
        $data[] = $value['data'];
        $label[] = $value['label'];
    endforeach;
?>

and then in your view/template :

var barChartData = {
   labels : <?php echo json_encode($label) ?>,
   datasets : [
      {
       fillColor : "rgba(23, 158, 3, 0.8)",
       strokeColor : "rgba(24, 107, 2, 0.8)",
       highlightFill: "rgba(24, 107, 2, 0.9)",
       highlightStroke: "rgba(24, 107, 2, 1)",
       data : <?php echo json_encode($data) ?>
     }
   ]
}

I haven’t run the code, but the idea is there as a snippet.
See if that helps.

Leave a comment