0

I have a chart where I want to get values from variables stored in Model. My Jquery Code:

var ctx = document.getElementById('myChart').getContext('2d');
   var myChart = new Chart(ctx, {
     type: 'bar',
     data: {
       labels: ['Quality', 'Service', 'Cleanliness', 'Staff'],
       datasets: [{
         label: 'Average Ratings',
         data: [0,1,2,4,5],
         backgroundColor: "rgba(153,255,62,0.6)"
       }]},

   });

My Model:

 public function chart_data()
    {
        $total = $this->db->query("SELECT 
                     ROUND(count((service_ans_1)+(service_ans_2)+(service_ans_3)+(quality_ans_1)+(quality_ans_2)+(quality_ans_3)+(quality_ans_4)+(cleanliness_ans_1)+(cleanliness_ans_2)+(cleanliness_ans_3)+(cleanliness_ans_4)+(cleanliness_ans_5)+(staff_ans_1)+(staff_ans_2)+(staff_ans_3))) as total from `dine_in_feedback` as f")->row()->total;

        $service    = $this->db->query("SELECT 
                        ROUND(avg(((service_ans_1)+(service_ans_2)+(service_ans_3))/3)) as service from `dine_in_feedback` as f")->row()->service;
        $quality    = $this->db->query("SELECT 
                    ROUND(avg(((quality_ans_1)+(quality_ans_2)+(quality_ans_3)+(quality_ans_4))/4)) as quality from `dine_in_feedback` as f")->row()->quality;
        $clean    = $this->db->query("SELECT 
                    ROUND(avg(((cleanliness_ans_1)+(cleanliness_ans_2)+(cleanliness_ans_3)+(cleanliness_ans_4)+(cleanliness_ans_5))/5)) as clean from `dine_in_feedback` as f")->row()->clean;
        $staff   = $this->db->query("SELECT 
                    ROUND(avg(((staff_ans_1)+(staff_ans_2)+(staff_ans_3))/15)) as staff from `dine_in_feedback` as f")->row()->staff;


        $result['service'] = $service / $total * 100;
        $result['quality'] = $quality / $total * 100;
        $result['clean'] = $clean / $total * 100;
        $result['staff'] = $staff / $total * 100;
        return $result;

    }

I want service,quality,clean and staff values in "data: [0,1,2,4,5]" like data:[value of service,value of quality,value of clean,value of staff]

2
  • You should have explained how you can access function chart_data() in your jquery, If your jquery is in view file, you should be able to access it with $data = $model->chart_data();, then use it in data like data: [ <?= $data['service'] ?>, <?= $data['quality'] ?> ... etc. Commented Aug 24, 2019 at 14:05
  • Yes it is in view. Got it bro. Working great. Thanks :) Commented Aug 25, 2019 at 8:35

1 Answer 1

1

First You need to call model function "chart_data" from your controller for example:

$chart_data= $this->model_name->chart_data();

Then send $chart_data variable to your view like this:

$this->load->view('your view name', $chart_data);

After that you can use chart_data variable in your view like this:

    var ctx = document.getElementById('myChart').getContext('2d');
   var myChart = new Chart(ctx, {
     type: 'bar',
     data: {
       labels: ['Quality', 'Service', 'Cleanliness', 'Staff'],
       datasets: [{
         label: 'Average Ratings',
         data: ['<?php echo $chart_data['service'] ?>','<?php echo $chart_data['quality'] ?>','<?php echo $chart_data['clean'] ?>','<?php echo $chart_data['staff'] ?>'],
         backgroundColor: "rgba(153,255,62,0.6)"
       }]},

   });
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks buddy. Working great :) . I must say the way you explained everything. Thanks alot once again.
One more thing if you can help. The labels are now dynamically showing the data but I want x and y labels to be set as fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.