How to Implement Google line Chart using Codeigniter – onlinecode

How to Implement Google line Chart using Codeigniter – onlinecode

In this post we will give you information about How to Implement Google line Chart using Codeigniter – onlinecode. Hear we will give you detail about How to Implement Google line Chart using Codeigniter – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this article, we will explain to you how to Implement Google line Chart using Codeigniter. today we will fetch data into MySQL, and plot a Line graph.

There are many types of charts provide by Google, like a bar chart, area chart, line chart, pie chart, etc. but here we use a Line chart.

In this article, we will plot a line graph using Google API. so you can see below the following steps.

Overview

Step 1: Create a Database in tableStep 2: Connect to DatabaseStep 3: Create ControllerStep 4: Create View File

Step 1: Create a Database in tableIn 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 DatabaseGo 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 ControllerIn this step, we will create a Googlechart.php file in the “application/controller” directory and paste the below code in this controller. here we will fetch data Day Wise Count Login Users Of Current Month from the database. that data will be pass in a chart and creating a Line graph.application/controller/Googlechart.php

<?php
 
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Googlechart extends CI_Controller {
 
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->database();
        $this->load->helper(array('url','html','form'));
    }       
    public function line_chart() {
      $query =  $this->db->query('SELECT DATE_FORMAT(login_date, "%Y-%m-%d") AS 'date', COUNT('login_date') as count FROM login_history WHERE MONTH(login_date) = "' . date('m') . '" AND YEAR(login_date) = "' . date('Y') . '" GROUP BY DAY(login_date)'); 
      $records = $query->result_array();
      $data['chart_data'] = $records;
      $this->load->view('line_chart',$data);
    }	

}
?>

Step 4: Create View FileSo finally, we will create the line_chart.php file in the “application/views/” directory.application/views/line_chart.php

<!Doctype html>
<html>
<head>
  <title>How to Implement Google line Chart using Codeigniter - onlinecode</title>
  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 
  <script type="text/javascript">
      google.charts.load('visualization', "1", {
          packages: ['corechart']
      });
  </script>
</head>
<body>
<div id="line_date_wise" style="width: 900px; height: 500px; margin: 0 auto"></div>
<script language="JavaScript">
  // Draw the Line chart 
  google.charts.setOnLoadCallback(lineChart);   
    function lineChart() {
        /* Define the chart to be drawn.*/
        var data = google.visualization.arrayToDataTable([
            ['Date', 'Login Count'],
            <?php 
             foreach ($chart_data as $row){
             echo "['".$row['date']."',".$row['count']."],";
             }
             ?>
        ]);
        var options = {
          title: 'Day Wise Count Login Users Of Current Month',
          curveType: 'function',
          legend: { position: 'bottom' }
        };
        /* Instantiate and draw the chart.*/
        var chart = new google.visualization.LineChart(document.getElementById('line_date_wise'));
        chart.draw(data, options);
    }
</script>
</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.

Show Demo

Please follow and like us:

Hope this code and post will helped you for implement How to Implement Google line Chart using Codeigniter – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  18  =  24

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US