我的 Zingchart 不显示,怎么了?
My Zingchart does not show, what is wrong?
这是下面的代码,
首先我从数据库中获取数据:
<?php
//getDBConnect function
require 'dbfunction.php';
//Get ID from form
$id = $_GET['staffid'];
//connect to database
$con = getDBConnect();
if(!mysqli_connect_errno($con)){
$sqlQueryStr =
"SELECT a.ai_Name, r.duration " .
"FROM report AS r, academicinstitution AS a " .
"WHERE r.staff_Id = '$id' " .
"AND r.ai_Id = a.ai_Id ";
$result = mysqli_query($con,$sqlQueryStr);
mysqli_close($con);
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Get data into array
$emparray = array();
while ($row = mysqli_fetch_assoc($result)) {
$emparray[] = $row;
}
//Group array by ai_Name
$grouparray = array();
foreach($emparray as $item)
{
if(!isset($grouparray[$item["ai_Name"]]))
$grouparray[$item["ai_Name"]] = 0;
$grouparray[$item["ai_Name"]] += $item["duration"];
}
?>
然后我开始为图表制作数据:
<script>
var dataBar=
<?php
foreach($grouparray as $keys => $value){
echo $value.',';
}
?>;
window.onload=function(){
zingchart.render({
id:'chartBar',
height:400,
width:600,
data:{
"graphset":[
{
"type":"bar",
"title":{"text":"BarChart"},
"series":[
{
"values":[dataBar]
}
]
}
]
}
});
};
</script>
<div id="chartBar"></div>
我已经尝试了很多方法来输入数据,但是图表仍然无法加载。是什么原因造成的,我该如何解决?
问题在于您如何创建 dataBar
数组。迭代这些值很好,但这是您实际输出的内容:
var dataBar=1,2,3,4,5,;
这不是一个格式正确的数组。试试这个:
var dataBar=[
<?php
foreach($grouparray as $keys => $value){
echo $value.',';
}
?>];
然后像这样在您的 JSON 中引用它:
"series":[
{
"values":dataBar
}
]
我在 ZingChart 团队。如果您有更多 ZC 问题,请大声说。
这是下面的代码,
首先我从数据库中获取数据:
<?php
//getDBConnect function
require 'dbfunction.php';
//Get ID from form
$id = $_GET['staffid'];
//connect to database
$con = getDBConnect();
if(!mysqli_connect_errno($con)){
$sqlQueryStr =
"SELECT a.ai_Name, r.duration " .
"FROM report AS r, academicinstitution AS a " .
"WHERE r.staff_Id = '$id' " .
"AND r.ai_Id = a.ai_Id ";
$result = mysqli_query($con,$sqlQueryStr);
mysqli_close($con);
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Get data into array
$emparray = array();
while ($row = mysqli_fetch_assoc($result)) {
$emparray[] = $row;
}
//Group array by ai_Name
$grouparray = array();
foreach($emparray as $item)
{
if(!isset($grouparray[$item["ai_Name"]]))
$grouparray[$item["ai_Name"]] = 0;
$grouparray[$item["ai_Name"]] += $item["duration"];
}
?>
然后我开始为图表制作数据:
<script>
var dataBar=
<?php
foreach($grouparray as $keys => $value){
echo $value.',';
}
?>;
window.onload=function(){
zingchart.render({
id:'chartBar',
height:400,
width:600,
data:{
"graphset":[
{
"type":"bar",
"title":{"text":"BarChart"},
"series":[
{
"values":[dataBar]
}
]
}
]
}
});
};
</script>
<div id="chartBar"></div>
我已经尝试了很多方法来输入数据,但是图表仍然无法加载。是什么原因造成的,我该如何解决?
问题在于您如何创建 dataBar
数组。迭代这些值很好,但这是您实际输出的内容:
var dataBar=1,2,3,4,5,;
这不是一个格式正确的数组。试试这个:
var dataBar=[
<?php
foreach($grouparray as $keys => $value){
echo $value.',';
}
?>];
然后像这样在您的 JSON 中引用它:
"series":[
{
"values":dataBar
}
]
我在 ZingChart 团队。如果您有更多 ZC 问题,请大声说。