[英]Chart.js is not showing data
My chart is not showing complete data. 我的图表未显示完整数据。 At the bottom is multiple names but it says "undefined" and at the side it starts at -1.0 then goes up to 1.0. 底部是多个名称,但显示“ undefined”,在侧面它以-1.0开头,然后上升到1.0。 I can get the item_name and the remaining in my console and it is complete it's just not showing in my chart. 我可以在控制台中获得item_name和其余的信息,并且它完全没有显示在图表中。 Below are my codes: 以下是我的代码:
MODEL 模型
public function get_charts(){
$str="select
I.item_name,
I.stocked,
ifnull(dispensed,0) as dispensed,
COALESCE (I.Stocked, 0) - COALESCE (S.dispensed, 0) AS remaining
FROM
(
SELECT
item_name AS 'item_name',
SUM(amount) AS 'stocked'
FROM
com_inv
GROUP BY
item_name
) I
LEFT JOIN (
SELECT
item_name,
order_status,
SUM(amount) AS dispensed
FROM
sls_ordrs
WHERE
order_status = 'received'
GROUP BY
item_name
) S ON I.item_name = S.item_name;";
$query=$this->db->query($str);
return $query->result();
}
CONTROLLER 控制器
public function get_charts(){
$result=$this->commissary_model->get_charts();
print json_encode($result);
}
VIEW 视图
var urlCharts="<?php echo site_url('commissary/get_charts');
?>";
$.ajax({
url:urlCharts,
method:"GET",
success:function(data){
console.log(data);
var item=[];
var remaining=[];
for(var i in data){
item.push(data[i].item_name);
remaining.push(data[i].remaining);
}
var chartdata={
labels: item,
datasets:[
{
label:'Remaining Items',
backgroundcolor:'rgba(200,200,200,0.75)',
borderColor:'rgba(200,200,200,0.75)',
hoverBackgroundColor:'rgba(200,200,200,1)',
hoverBorderColor:'rgba(200,200,200,1)',
data:remaining
}
]
};
var ctx=$("#mycanvas");
var barGraph = new Chart(ctx, {
type: 'bar',
data: chartdata
});
},
error:function(data){
console.log("data");
}
});
SAMPLE OF CONSOLE.LOG CONSOLE.LOG的示例
[{"item_name":"aaa","stocked":"23456","dispensed":"0","remaining":"23456"},{"item_name":"accumsan Curae","stocked":"889.74","dispensed":"123.5","remaining":"766.24"},{"item_name":"ad bibendum Donec","stocked":"659.12","dispensed":"0","remaining":"659.12"},{"item_name":"Aenean cursus","stocked":"169.63","dispensed":"0","remaining":"169.63"},{"item_name":"Aenean magna","stocked":"272.11","dispensed":"0","remaining":"272.11"}, ....etc...
Can you please tell me where I am getting it wrong. 您能告诉我哪里错了吗? Thank you. 谢谢。
The for loop does not look correct for循环看起来不正确
for(var i = 0; i < data.length; i++) {
item.push(data[i].item_name);
remaining.push(data[i].remaining);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.