Codeigniter 4 Morris Area & Line Chart Example

In this tutorial, we will show you how to create Codeigniter 4 Morris Area & Line Chart Example.

Morris.js is a lightweight library that uses jQuery and Raphaël to make drawing time-series graphs and charts easy.

We will use morris.js library to create a Codeigniter 4 Morris Area & Line Chart Example.

Step 1 – Create Database Table

First, we will create a database table “sales” using the following SQL query.

CREATE TABLE `sales` ( `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `month` varchar(50) NOT NULL, `sales` int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step 2 – Insert Data

Now, we will insert some dummy data into the “sales” table.

INSERT INTO `sales` (`id`, `month`, `sales`) VALUES (1, ‘January’, 1000), (2, ‘February’, 2000), (3, ‘March’, 3000), (4, ‘April’, 4000), (5, ‘May’, 5000), (6, ‘June’, 6000), (7, ‘July’, 7000), (8, ‘August’, 8000), (9, ‘September’, 9000), (10, ‘October’, 10000), (11, ‘November’, 11000), (12, ‘December’, 12000);

Step 3 – Create Routes

Open app/Config/Routes.php file and add the following routes.

$routes->get(‘morris-area-chart’, ‘MorrisController::areaChart’);

Step 4 – Create Controller

Now, we will create a new controller MorrisController.php in app/Controllers folder.

findAll(); return view(‘morris-area-chart’, $data); } }

Step 5 – Create Model

Now, we will create a new model SalesModel.php in app/Models folder.

<?php echo $title; ?>

Now, we will run the example.

http://localhost:8080/morris-area-chart

It will display the following output.

Codeigniter 4 Morris Area & Line Chart Example

Conclusion

In this tutorial, we have shown you how to create Codeigniter 4 Morris Area & Line Chart Example.

Recommended Articles

This is a guide to Codeigniter 4 Morris Area & Line Chart Example. Here we discuss the introduction to Codeigniter 4 Morris Area & Line Chart Example and its Steps to create. You may also have a look at the following articles to learn more –
[ad_1]

Codeigniter 4 morris area and line charts tutorial. In this example tutorial, you will learn how to get month records of date wise from MySQL in codeigniter 4 app and implement morris area chart and line chart in codeigniter 4 app using morris js.

This tutorial will guide you step by step on how to get date wise data of the month from the MySQL database and how to show the data on morris line and area charts with the Codeigniter 4 app.

This tutorial will implement line chart and area chart in Codeigniter 4 app using morris chart js and fetch date wise record of current month from MySQL database. And display it on area and line charts.

How To Create Morris Line and Area Chart In Codeigniter 4 App

By using the following steps, you can create dynamic line and area chart using morris in codeIgniter 4 projects:

  • Step 1: Setup Codeigniter 4 Project
  • Step 2: Setup Basic Configurations
  • Step 3: Create a Table in the Database
  • Step 4: Setup Database Credentials
  • Step 5: Create a Controller
  • Step 6: Create a View
  • Step 7: Define Routes
  • Step 8: Start Development Server

Step 1: Setup Codeigniter 4 Project

In this step, you will download the latest version of Codeigniter 4, Go to this link https://codeigniter.com/download Download Codeigniter 4 fresh new setup and unzip the setup in your local system xampp/htdocs/ . And change the download folder name “demo”

Step 2: Setup Basic Configurations

Next, you will set some basic configuration on the app/config/app.php file, so let’s go to application/config/config.php and open this file on text editor.

Set Base URL like this

public $baseURL = 'http://localhost:8080';
To
public $baseURL = 'http://localhost/demo/';

Step 3: Create a Table in the Database

In this step, you need to create table in database and as well as insert some data for line and area chart in codeiginter 4. So visit your phpmyadmin panel and execute the following sql query in it:

CREATE TABLE users (
    id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
    name varchar(100) NOT NULL COMMENT 'Name',
    email varchar(255) NOT NULL COMMENT 'Email Address',
    contact_no varchar(50) NOT NULL COMMENT 'Contact No',
    created_at varchar(20) NOT NULL COMMENT 'Created date',
    PRIMARY KEY (id)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=1;

  INSERT INTO users (id, name, email, contact_no, created_at) VALUES
  (1, 'Team', '[email protected]', '9000000001', '2019-01-01'),
  (2, 'Admin', '[email protected]', '9000000002', '2019-01-02'),
  (3, 'User', '[email protected]', '9000000003', '2019-01-03'),
  (4, 'Editor', '[email protected]', '9000000004', '2019-01-04'),
  (5, 'Writer', '[email protected]', '9000000005', '2019-01-05'),
  (6, 'Contact', '[email protected]', '9000000006', '2019-01-06'),
  (7, 'Manager', '[email protected]', '9000000007', '2019-01-07'),
  (8, 'John', '[email protected]', '9000000055', '2019-01-08'),
  (9, 'Merry', '[email protected]', '9000000088', '2019-01-09'),
  (10, 'Keliv', '[email protected]', '9000550088', '2019-01-10'),
  (11, 'Herry', '[email protected]', '9050550088', '2019-01-11'),
  (12, 'Mark', 'm[email protected]', '9050550998', '2019-01-12');

Step 4: Setup Database Credentials

In this step, you need to connect our project to the database. you need to go app/Config/Database.php and open database.php file in text editor. After opening the file in a text editor, you need to set up database credentials in this file like below.

	public $default = [
		'DSN'      => '',
		'hostname' => 'localhost',
		'username' => 'root',
		'password' => '',
		'database' => 'demo',
		'DBDriver' => 'MySQLi',
		'DBPrefix' => '',
		'pConnect' => false,
		'DBDebug'  => (ENVIRONMENT !== 'production'),
		'cacheOn'  => false,
		'cacheDir' => '',
		'charset'  => 'utf8',
		'DBCollat' => 'utf8_general_ci',
		'swapPre'  => '',
		'encrypt'  => false,
		'compress' => false,
		'strictOn' => false,
		'failover' => [],
		'port'     => 3306,
	];

Step 5: Create a Controller

In this step, Visit app/Controllers and create a controller name MorrisChart.php. In this controller, you need to add the following methods into it:

<?php namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\RequestInterface;


class MorrisChart extends Controller
{

    public function index() {

        $db      = \Config\Database::connect();
        $builder = $db->table('users');   

        $query = $builder->query("SELECT  DAY(created_at) as y, COUNT(id) as a FROM users WHERE MONTH(created_at) = '" . date('m') . "'
        AND YEAR(created_at) = '" . date('Y') . "'
      GROUP BY DAY(created_at)");

      $data['chart_data'] = $query->getResult();
        
      return view('home',$data);
    }

}

Step 6: Create a View

In this step, you need to create one view files name home.php and update the following code into your file:

<head>
<meta charset=utf-8 />
<title>Codeigniter 4 Morris Line and Area Chart Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.2/raphael-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.0/morris.min.js"></script>
</head>
<body>
  <h3 class="text-primary text-center">
    Morris charts with Codeigniter
  </h3>
  <div class"row">
  
    <div  class="col-sm-12 text-center">
       <label class="label label-success">Line Chart</label>
      <div id="line-chart" ></div>
    </div>
     <div class="col-sm-12 text-center">
       <label class="label label-success">Area Chart</label>
      <div id="area-chart" ></div>
    </div>
    
  </div>
</body>

Implement Javascript code

Finally, need to implement javascript code for showing a data on morris line and area chart. Now you update the code on script tag after the closing of body tag.

<script>
  var serries = JSON.parse(`<?php echo $chart_data; ?>`);
  console.log(serries);
  var data = serries,
    config = {
      data: data,
      xkey: 'y',
      ykeys: ['a'],
      labels: ['Current Month Date Wise Total Registered Users'],
      fillOpacity: 0.6,
      hideHover: 'auto',
      behaveLikeLine: true,
      resize: true,
      pointFillColors:['#ffffff'],
      pointStrokeColors: ['black'],
      lineColors:['gray','red']
  };

 config.element = 'area-chart';
 Morris.Area(config);

 config.element = 'line-chart';
 Morris.Line(config);
</script>

Step 7: Define Routes

In this step, you need to create a route that renders the table into the view, place the following code in app/Config/Routes.php file.

$routes->get('/', 'MorrisChart::index');

Step 8: Start Development Server

In this step, open your terminal and execute the following command to start development sever:

php spark serve

Then, Go to the browser and hit below the URL:

http://localhost:8080

Conclusion

In this codeigniter 4 morris line chart and area chart tutorial, you have successfully fetched the record from a database and display it on the morris line and area charts in codeigniter 4 app.

If you have any questions or thoughts to share, use the comment form below to reach us.

Recommended CodeIgniter 4 Tutorial

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00