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>