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]
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 likedata: [ <?= $data['service'] ?>, <?= $data['quality'] ?> ...etc.