In this article, we will explain to you how to Create Pie Chart In Codeigniter using AmCharts. today we will fetch data into MySQL, and plot a Pie graph.
There are many types of charts provide by AmCharts, like a bar chart, area chart, line chart, pie chart, etc. but here we use a Pie chart.
We can easily integrate AmCharts with TypeScript, PHP, Angular, React. here In this article, we are using AmCharts version 3. so you can see below the following steps.
Overview
Step 1: Create a Database in table
Step 2: Connect to Database
Step 3: Create Controller
Step 4: Create View File
Step 1: Create a Database in table
In this step, we have to create a table in the database, so we will create a database using the below code.
CREATE TABLE `login_history` ( `id` int(11) NOT NULL, `user_id` int(11) NOT NULL, `login_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1;
Step 2: Connect to Database
Go to the config folder and open database.php file some changes in this file like hostname, database username, database password, and database name.
$db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'enter here database name', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
Step 3: Create Controller
In this step, we will create an Amchart.php file in the “application/controller” directory and paste the below code in this controller. here we will count user login data and fetch data last 7 days of data from the database. that data will be pass in an Amchart and creating Pie graphs.
application/controller/Amchart.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Amchart extends CI_Controller { public function __construct() { parent::__construct(); // load model $this->load->database(); $this->load->helper(array('url','html','form')); } public function pie_chart() { $query = $this->db->query('SELECT DATE_FORMAT(login_date, "%Y-%m-%d") AS `date`, COUNT(`login_date`) as count FROM login_history WHERE date(login_date) > (DATE(NOW()) - INTERVAL 7 DAY) AND MONTH(`login_date`) = "'. date('m') .'" AND YEAR(`login_date`) = "' . date('Y') .'" GROUP BY DAYNAME(`login_date`) ORDER BY (`login_date`) ASC'); $records = $query->result_array(); // echo "<pre>"; // print_r($records); // echo "</pre>"; die; $data = []; foreach($records as $row) { $data[] = ['date' => date("l", strtotime($row['date'])), 'count' =>$row['count']]; } $data['chart_data'] = json_encode($data); $this->load->view('pie_chart',$data); } } ?>
Step 4: Create View File
So finally, we will create the pie_chart.php file in the “application/views/” directory.
application/views/pie_chart.php
<!DOCTYPE html> <html> <head> <title>How To Create Pie Chart In Codeigniter With AmChart - XpertPhp</title> <script src="https://www.amcharts.com/lib/3/amcharts.js"></script> <script src="https://www.amcharts.com/lib/3/pie.js"></script> <script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script> <link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" /> <script> var chartData = JSON.parse(`<?php echo $chart_data; ?>`); var chart = AmCharts.makeChart( "chartdiv", { "type": "pie", "theme": "none", "dataProvider": chartData, "valueField": "count", "titleField": "date", "balloon":{ "fixedPosition":true }, "export": { "enabled": true } } ); </script> </head> <body> <div id="chartdiv" style="width: 900px; height: 800px;"></div> </body> </html>
We think would you like this article, so you can click on the “Show Demo” button and you can see this demo article.