2017-07-27 37 views
0

我試圖通過使用morris JavaScript的mysql傳遞值從而顯示動態條形圖。但只有值是x軸正在傳遞。因此該圖是空白的。我做了所有研究,但找不到它。我需要你的幫助。我是編程新手。在Y軸的值不通過莫里斯javascript

Morris.Bar({ 
 
element : 'chart', 
 
data:[<?php echo $chart_data; ?>], 
 
xkey:'gp_submit', 
 
ykeys:['cnt'], 
 
labels:['Submitted','cnt'], 
 
hideHover:'auto', 
 
stacked:true 
 
});
<?php $connect = mysqli_connect("localhost", "root", "", "hr"); 
 
$query = "SELECT gp_submit AS dt, name AS cnt FROM `emp_det` where gp_submit>=DATE_SUB(NOW(), INTERVAL 1 WEEK) GROUP BY gp_submit"; 
 
$result = mysqli_query($connect, $query); 
 
$chart_data = ''; 
 
while($row = mysqli_fetch_array($result)) 
 
{ 
 
$chart_data .= "{ gp_submit:'".$row["dt"]."', cnt:'".$row["cnt"]."'}, "; 
 
} 
 
$chart_data = substr($chart_data, 0, -2); 
 
?>
<div id="chart" style="width:100%; height:280px;"></div>

+0

您還可以添加您的查詢結果,以便我們可以看到您正在處理的數據嗎? –

回答

0

它,因爲你不及格有一個適當的JSON

嘗試像這樣

$chart_data = array(); 
while($row = mysqli_fetch_array($result)) 
{ 
$chart_data []['gp_submit']= $row["dt"]; 
$chart_data[]['cnt] = $row["cnt"]; 
} 

而且在JS:

Morris.Bar({ 
element : 'chart', 
data:<?php echo json_encode($chart_data, JSON_NUMERIC_CHECK); ?>, 
xkey:'gp_submit', 
ykeys:['cnt'], 
labels:['Submitted','cnt'], 
hideHover:'auto', 
stacked:true 
}); 

它應該工作;

+0

謝謝。但它不起作用。當我執行相同的X軸值不傳遞 – LinaM

0
new Morris.Bar({ 
    element: "bargraph", 
    data: d_data,//getBarData(data,periodvalue,year),//data, 
    xkey: "day", 
    ykeys: ["total","confirm"], 
    xLabels:'day Month', 
    labels: [total,confirm], 
    barColors: ['#0b62a4','#7BB661'] 
}); 

Morris.Bar({ 
    element : 'chart', 
    data:[<?php echo $chart_data; ?>], 
    xkey:'gp_submit', 
    ykeys:['here you haveto put value','cnt'], 
    labels:['Submitted','cnt'], 
    hideHover:'auto', 
    stacked:true 
});