Bar Chart Integration with CodeIgniter 4 – HighCharts Js

Share this Article
Reading Time: 8 minutes

Inside this article, we will see the concept of Bar Chart Integration in CodeIgniter 4. This article will be step by step graph integration.

Bar chart represents the information in very graphical view which provides the complete idea about data. We will use jQuery Highcharts to add Bar chart into CodeIgniter 4 application.

To Learn about Pie Chart Integration in CodeIgniter 4, Click here.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.

Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system.

To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

 // Do it to 
CI_ENVIRONMENT = development

Now application is in development mode.

Create Database & Table in Application

We need to create a database. For database we will use MySQL. We have 2 options available to create database. Either we can use PhpMyAdmin Manual interface Or we can use command to create.

CREATE DATABASE codeigniter4_app;

Next, we need a table. That table will be responsible to store data. Let’s create table with some columns.

CREATE TABLE `students` (
 `name` varchar(50) NOT NULL,
 `term1_marks` int(11) NOT NULL,
 `term2_marks` int(11) NOT NULL,
 `term3_marks` int(11) NOT NULL,
 `term4_marks` int(11) NOT NULL,
 `remarks` varchar(50) NOT NULL,

Successfully, we have created a database and a table. Let’s connect with the application.

Database Connectivity to Application

Open .env file from project root. Search for DATABASE. You should see the connection environment variables.

Let’s set the value for those to connect with database.

 database.default.hostname = localhost
 database.default.database = codeigniter4_app
 database.default.username = root
 database.default.password = root
 database.default.DBDriver = MySQLi

Now, database successfully connected with application.

Create Model

Open project into terminal and run this spark command to create model.

$ php spark make:model Student --suffix

It will create StudentModel.php at /app/Models folder.

Open StudentModel.php and write this code.


namespace App\Models;

use CodeIgniter\Model;

class StudentModel extends Model
    protected $DBGroup = 'default';
    protected $table = 'students';
    protected $primaryKey = 'id';
    protected $useAutoIncrement = true;
    protected $insertID = 0;
    protected $returnType = 'array';
    protected $useSoftDelete = false;
    protected $protectFields = true;
    protected $allowedFields = [

    // Dates
    protected $useTimestamps = false;
    protected $dateFormat = 'datetime';
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';
    protected $deletedField = 'deleted_at';

    // Validation
    protected $validationRules = [];
    protected $validationMessages = [];
    protected $skipValidation = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert = [];
    protected $afterInsert = [];
    protected $beforeUpdate = [];
    protected $afterUpdate = [];
    protected $beforeFind = [];
    protected $afterFind = [];
    protected $beforeDelete = [];
    protected $afterDelete = [];

Create Data Seeder

Open project in terminal and run this spark command.

$ php spark make:seeder Student --suffix

It will create a file with name StudentSeeder.php at /app/Database/Seeds folder.

Open StudentSeeder.php and write this following code into it.


namespace App\Database\Seeds;

use App\Models\StudentModel;
use CodeIgniter\Database\Seeder;
use Faker\Factory;

class StudentSeeder extends Seeder
    public function run()
        $data = [];

        for ($i = 0; $i < 5; $i++) {

            $data[] = $this->generateTestStudent();

        $student_obj = new StudentModel();


    public function generateTestStudent()
        $faker = Factory::create();

        return [
            "name" => $faker->name(),
            "term1_marks" => $faker->numberBetween(100, 150),
            "term2_marks" => $faker->numberBetween(100, 150),
            "term3_marks" => $faker->numberBetween(100, 150),
            "term4_marks" => $faker->numberBetween(100, 150),
            "remarks" => $faker->randomElement([
                "Good Work",
                "Presented smartness in learning",
                "Amazing learning power"

Run Seeder File

Run seeder file by this spark command.

$ php spark db:seed StudentSeeder

Create Route

Open Routes.php from /app/Config folder. Add this route into it.

//.. Other routes

$routes->get("bar-graph", "StudentController::index");

Create Controller

Back to terminal and run this spark command to create application controller.

$ php spark make:controller Student --suffix

It will create StudentController.php file at /app/Controllers folder.


namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\StudentModel;

class StudentController extends BaseController
	public function index()
		$student_obj = new StudentModel();

		$students = $student_obj->findAll();

		$dataPoints = [];

		foreach ($students as $student) {
			$dataPoints[] = array(
				"name" => $student['name'],
				"data" => [

		return view("bar-graph", [
			"data" => json_encode($dataPoints),
			"terms" => json_encode(array(
				"Term 1",
				"Term 2",
				"Term 3",
				"Term 4",

Create View File & Render

Next, create a view file with name bar-graph.php at /app/Views folder. Open bar-graph.php and write this following code into it.

<!DOCTYPE html>
<html lang="en">
  <title>Bar Chart in CodeIgniter 4 - Online Web Tutor</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>

<div class="container">
  <h2 style="text-align:center;">Bar Chart in CodeIgniter 4 - Online Web Tutor</h2>
  <div class="panel panel-default">
    <div class="panel-heading">Bar Chart in CodeIgniter 4</div>
    <div class="panel-body">
        <div id="bar-chart"></div>

<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>

        Highcharts.chart('bar-chart', {
            chart: {
                type: 'column'
            title: {
                text: 'Student Term Wise Marks'
            xAxis: {
                categories: <?= $terms ?>,
                crosshair: true
            yAxis: {
                min: 0,
                title: {
                    text: 'Marks'
            tooltip: {
                headerFormat: '<span style="font-size:10px">{point.key} Marks</span><table>',
                pointFormat: '<tr><td style="color:{series.color};padding:0">{}: </td>' +
                    '<td style="padding:0"><b>{point.y}</b></td></tr>',
                footerFormat: '</table>',
                shared: true,
                useHTML: true
            plotOptions: {
                column: {
                    pointPadding: 0.2,
                    borderWidth: 0
            series: <?= $data ?>


Application Testing

Start development server:

$ php spark serve

URL: http://localhost:8080/bar-graph

We hope this article helped you to learn Bar Chart Integration with CodeIgniter 4 Tutorial in a very detailed way.

Learn about Line Chart Integration in CodeIgniter 4, Click here.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here