My Zingchart does not show, what is wrong?
Asked Answered
J

1

7

This is the code below,
First I get the data from database:

<?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"];
        }
?>

Then I proceed to making the data for the chart:

<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>

I have tried many ways to input the data, however the graph still does not load. What is causing this and how do I fix it?

Jollity answered 25/7, 2015 at 23:6 Comment(1)
If the answer solves your question, please accept it so others may see it.Trembly
T
5

The issue is how you're creating your dataBar array. Iterating over the values is fine but this is what you're actually outputting:

var dataBar=1,2,3,4,5,;

which is not a well-formed array. Try this instead:

var dataBar=[
    <?php 
        foreach($grouparray as $keys => $value){
            echo $value.',';
        }
    ?>];

Then reference it in your JSON like so:

"series":[
  {
    "values":dataBar
  }
]

I'm on the ZingChart team. Holler if you have more ZC questions.

Trembly answered 27/7, 2015 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.