How To Create Chart Using Codeigniter and Morris
How To Create Chart Using Codeigniter and Morris
Because today, I will share with you how to create chart using
STEP 1. PREPARATION
This is important!
If you missed this step, it means you missed the whole of this tutorial.
The better your preparation, the more likely you will succeed in
1. Codeigniter
website: www.codeigniter.com
2. Jquery
website: www.jquery.com
3. Morris.js
looking charts.
website: http://morrisjs.github.io/morris.js/
4. Raphael.js
https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js
Then select all the codes (Ctrl +A) then copy and save with the
name raphael-min.js.
Then create a table named "account" with the structure like the
picture below:
To create a table with a structure like the picture above, you could
1
CREATE TABLE account(
2 id INT(11) PRIMARY KEY AUTO_INCREMENT,
3 year YEAR(4),
4 purchase INT(11),
5 sale INT(11),
profit INT(11)
6
)ENGINE=INNODB;
7
query:
4 ('2015','3000','4500','1500'),
5 ('2016','2000','3000','1000'),
('2017','2000','4000','2000'),
6
7 ('2018','2200','3000','800'),
8 ('2019','5000','7000','2000');
The query above will input 7 records data into the ”account” table.
And then copy the morris.css file into the css folder and copy
the js folder.
STEP 5. CODEIGNITER
CONFIGURATION
Next step is the configuration on the codeigniter.
1. Autoload.php
application/config/autoload.php
like this:
1 $autoload['libraries'] = array();
2 $autoload['helper'] = array();
1 $autoload['libraries'] = array('database');
2 $autoload['helper'] = array('url');
2. Config.php
application/config/config.php
Open config.php file using text editor, and then find the code below:
1 $config['base_url'] = '';
1 $config['base_url'] = 'http://localhost/chart/';
3. Database.php
application/config/database.php
Open database.php file using text editor, and then find the code
below:
1 $active_group = 'default';
2 $query_builder = TRUE;
4 $db['default'] = array(
23
24
1 $active_group = 'default';
$query_builder = TRUE;
2
3
$db['default'] = array(
4
'dsn' => '',
5
'hostname' => 'localhost',
6
'username' => 'root',
7
'password' => '',
8 'database' => 'chart_db',
9 'dbdriver' => 'mysqli',
10 'dbprefix' => '',
23
24
4. Routes.php
application/config/routes.php
Open routes.php file using text editor, and then find the code below:
1 $route['default_controller'] = 'welcome';
1 $route['default_controller'] = 'chart';
STEP 6. MODEL
The Model represents your data structures. Typically your model
classes will contain functions that help you retrieve, insert, and
1
<?php
2
class Chart_model extends CI_Model{
3
6 $this->db->select('year,purchase,sale,profit');
7 $result = $this->db->get('account');
return $result;
8
}
9
10
}
11
function get_data().
STEP 7. CONTROLLER
The Controller serves as an intermediary between the Model, the
View, and any other resources needed to process the HTTP request
code below:
1 <?php
3 function __construct(){
parent::__construct();
4
//load chart_model from model
5
$this->load->model('chart_model');
6
}
7
8
function index(){
9
$data = $this->chart_model->get_data()->result();
10 $x['data'] = json_encode($data);
11 $this->load->view('chart_view',$x);
12 }
13 }
14
STEP 8. VIEW
The View is the information that is being presented to a user.
code below:
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5 <title>Chart using codeigniter and morris.js</title>
6 <link rel="stylesheet" href="<?php echo base_url().'assets/css/morris.css'?>">
7 </head>
8 <body>
10
<div id="graph"></div>
11
12
<script src="<?php echo base_url().'assets/js/jquery.min.js'?>"></script>
13
<script src="<?php echo base_url().'assets/js/raphael-min.js'?>"></script>
14
<script src="<?php echo base_url().'assets/js/morris.min.js'?>"></script>
15
<script>
16 Morris.Bar({
17 element: 'graph',
18 data: <?php echo $data;?>,
19 xkey: 'year',
1
<script>
2
Morris.Bar({
3 element: 'graph',
4 data: <?php echo $data;?>,
5 xkey: 'year',
The above code will display a bar chart on the graph element. Where
displayed.
In this case, I want to display the chart into a div tag that
1 <div id="graph"></div>
STEP 9. TESTING
To do the test, please open your browser and type the following URL:
http://localhost/chart/
Awesome right?
You can also change the style from the chart above to a line
be Line.
Like this:
1
<script>
2
Morris.Bar({
3 element: 'graph',
4 data: <?php echo $data;?>,
5 xkey: 'year',
1
<script>
2
Morris.Line({
3 element: 'graph',
4 data: <?php echo $data;?>,
5 xkey: 'year',
http://localhost/chart/
if it's running well, it will look like the following picture:
Beside that, You can also change the style to Area chart just by
Like this:
1
<script>
2
Morris.Area({
3 element: 'graph',
4 data: <?php echo $data;?>,
5 xkey: 'year',
http://localhost/chart/
if it's running well, it will look like the following picture:
That’s it!.
CONCLUSION
In today's tutorial is about how to create a chart with codeigniter and
morris.js.
In this tutorial, you have learned how to create charts with codeigniter
Not only that, you have also learned how to change the style of chart