Pie Charts with Google Charts

6

I'm trying to generate a pie chart with codeigniter and mysql but I get it I'm wrong I'd like your help Here's the code;

chartcontroller.php

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * 
 */
class ChartController extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('chartmodel', 'chart');
    }

    public function index() {
        $results = $this->chart->get_chart_data();
        $data['chart_data'] = $results['chart_data'];

        $this->load->view('chart', $data);
    }

}

/* End of file ChartController.php */
/* Location: ./application/controllers/ChartController.php */ 

chartmodel.php

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class ChartModel extends CI_Model {

 private $performance = 'cursos';

 function __construct() {

 }

 function get_chart_data() {
     $query = $this->db->get($this->performance);
     $results['chart_data'] = $query->result();



     return $results;
 }   }   /* End of file chartmodel.php */ /* Location: ./application/models/chartmodel.php */

chart.php

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);
      function drawChart() {
                var data = google.visualization.arrayToDataTable([
                    [{type: 'string', label: 'Nombre'}, {type: 'number', label: 'Horas'}],
<?php
foreach ($chart_data as $data) {
    echo '[' . $data->nombre . ',' . $data->horas. '],';
}
?>
                ]);
 
                var options = {
          title: 'My Daily Activities'
        };

 
               var chart = new google.visualization.PieChart(document.getElementById('piechart'));

        chart.draw(data, options);
      }
       </script>
  </head>
  <body>
    <div id="piechart" style="width: 900px; height: 500px;"></div>
  </body>
</html>

    
asked by Kpeski2814 04.10.2016 в 00:20
source

2 answers

1

You must pass the name String as a string, it is detecting it as a variable since it detects Mathematics and not 'Mathematics'.

I think it would be something like that, although I'm not much to touch PHP:

<?php
foreach ($chart_data as $data) {
    echo '[\'' . $data->nombre . '\',' . $data->horas. '],';
}
?>

Greetings,

    
answered by 30.12.2016 в 13:22
0

As ReyJuanjo said, you have to add it as a text string, that is, you have to put it in quotation marks. Also, there should not be a comma at the end of the last value. I add the corrected code.

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" 
           src="https://www.gstatic.com/charts/loader.js"></script>
        <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);
      function drawChart() {

      <?php
         $arrayToDataTable ="['Nombre', 'Horas']";
         foreach ($chart_data as $data) {
            $arrayToDataTable .= ", ["' . $data->nombre . '","' . $data->horas. '"]";
         }
      ?>

      var data = google.visualization.arrayToDataTable([
         "<?php echo $arrayToDataTable ?>"
      ]);

      var options = {
        title: 'My Daily Activities'
      };

      var chart = new google.visualization.PieChart(document.getElementById('piechart'));

    chart.draw(data, options);
   }
   </script>
  </head>
  <body>
     <div id="piechart" style="width: 900px; height: 500px;"></div>
  </body>
</html>
    
answered by 08.01.2019 в 16:38