DEV Community

Robert Look
Robert Look

Posted on

How to Create Simple Bar Chart In Codeigniter using AmCharts?

There are many types of charts provide by AmCharts, like a bar chart, area chart, line chart, pie chart, amcharts line chart, etc. but here we use a bar chart. very easy to create amcharts line chart.

We can easily integrate AmCharts Codeigniter. here In this article, we are using AmCharts version 3. so you can see below the following steps.

How to Create Simple Bar Chart In Codeigniter using AmCharts?

<?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 bar_chart() {
      $query =  $this->db->query('SELECT DATE_FORMAT(login_date, "%Y-%m-%d") AS `date`, COUNT(`login_date`) as count FROM login_history WHERE `login_date` >= NOW() - INTERVAL 10 MONTH GROUP BY MONTH(`login_date`) ORDER BY `login_date` ASC'); 
      $records = $query->result_array();
      $data = [];
      foreach($records as $row) {
        $data[] = ['date' => date('Y-m-t',strtotime($row['date'])), 'count' =>$row['count']];
      }
      $data['chart_data'] = json_encode($data);
      $this->load->view('bar_chart',$data);
    }   
}
?>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)