0

I have a controller function where it gets my json data for my bar graph

The json code from controller is

{"xkey":[["0","Sun"],["1","Mon"],["2","Tue"],["3","Wed"],["4","Thu"],["5","Fri"],["6","Sat"]],"user":[[2,"1"],[3,"1"]]}

Question from the controller function how am I able to pass the json['xkey'] and json['user'] to my script

I have looked at Morris Js and Codeigniter pass data from a controller but not work for me.

Here is what I have tried

<script type="text/javascript">
$( document ).ready(function() {
    $('#range').on('click', function(e) {
        e.preventDefault();
        $.ajax({
            type: 'get',
            url: "<?php echo base_url('admin/dashboard/chart/chart_data');?>/" + $( "#range" ).val(),
            dataType: 'json',
            success: function(json) {
            if (typeof Morris != 'undefined') {
                Morris.Bar({
                    element: 'bar-example',
                    resize: true,
                    stacked: false,
                    xLabelAngle: 50,
                    grid: true,
                    gridTextSize: 10,
                    data: [{m: json['xkey'] a: json['user']}],
                    xkey: 'm',
                    ykeys: ['a'],
                    labels: ['Users']
                });
            }
            }
        });
    }); 
});
</script>

Controller

<?php

class Chart extends MX_Controller {

public function index() {
    return $this->load->view('template/dashboard/chart_view');
}

public function chart_data() {
    $json = array();

    $json['xkey'] = array();

    $json['user'] = array();

    $uri_segment = $this->uri->segment(5);

    if (isset($uri_segment)) {
        $range = $uri_segment;
    } else {
        $range = 'month';
    }

    switch ($range) {
        case "week":
            $results = $this->getUserTotalByWeek();

            foreach ($results as $key => $value) {
                $json['user'][] = array($key, $value['total']);
            }

            $date_start = strtotime('-' . date('w') . ' days');

            for ($i = 0; $i < 7; $i++) {
                $date = date('Y-m-d', $date_start + ($i * 86400));

                $json['xkey'][] = array(date('w', strtotime($date)), date('D', strtotime($date)));
            }
            break;
        default:
            echo "Your favorite color is neither red, nor green!";
    }

    $this->output->set_header('Content-Type: application/json; charset=utf-8');
    $this->output->set_output(json_encode($json));
}

public function getUserTotalByWeek() {
    $user_data = array();

    $date_start = strtotime('-' . date('w') . ' days');

    for ($i = 0; $i < 7; $i++) {
        $date = date('Y-m-d', $date_start + ($i * 86400));

        $customer_data[date('w', strtotime($date))] = array(
            'day'   => date('D', strtotime($date)),
            'total' => 0
        );
    }

    $this->db->select('COUNT(*) AS total, date_reg');
    $this->db->from('user');
    $this->db->where('date_reg >=', date('Y-m-d', $date_start));
    $this->db->group_by('DAYNAME(date_reg)');
    $query = $this->db->get();

    foreach ($query->result_array() as $result) {
        $user_data[date('w', strtotime($result['date_reg']))] = array(
            'day'   => date('D', strtotime($result['date_reg'])),
            'total' => $result['total']
        );
    }

    return $user_data;
}
}
Community
  • 1
  • 1
  • What is the output on `base_url('admin/dashboard/chart/chart_data/week')` url? – Tpojka Aug 21 '16 at 12:52
  • your data doesn't make sense, `xkey` contains the days of the week, and `user` contains the amount of users per week (by some key). And neither does the wild processing of the DB-data in your PHP-code. for `$range == 'week'` return a simple Array of `array( 'day'=>date('D',strtotime($result['date_reg'])), 'total'=>$result['total'] )`, then your `data` for morris is the returned array, the `xkey` is `'day'`, and `ykey` is `'total'`. – Thomas Aug 21 '16 at 12:53
  • maybe you need to sum the totals, if `$query->result_array()` contains data for more than one week, but that's pretty much all processing that needs to be done to the data. – Thomas Aug 21 '16 at 12:59
  • @Tpojka the out put of the URL is the json code above question. –  Aug 21 '16 at 13:35
  • Morris [data](http://jsbin.com/uzosiq/258/embed?js,output) expects array of objects but you provide array of arrays. solve that first. – Tpojka Aug 21 '16 at 13:43
  • OK will work on it better –  Aug 21 '16 at 14:03

0 Answers0