0

i would like to send my data from mssql via php to my chart. For this i'm using $.get function to receive data from mssql. but i'm getting an error: Uncaught TypeError: Cannot read property '0' of undefined obviously i can't access an object.

my php code:

if(isset($_GET['employeesChart'])) {
    getemployeesChart($conn);
}

function getemployeesChart($conn) {
  $sql ="SELECT * FROM employee;
  $result = sqlsrv_query($conn, $sql);
  $row = sqlsrv_fetch_array($result);
  $isAtWork = $row['Work'];
  $isAtHome = $row['Home'];
  $data = array('isAtWork' => $isAtWork, 'isAtHome' => $isAtHome);
  echo json_encode($data)
}

and my javascript code:

var data = {
    labels: [
        "Home",
        "Work",
        "Break"
    ],
    datasets: [{
        data: [],
        backgroundColor: ["#FF6384","#36A2EB","#FFCE56"],
        hoverBackgroundColor: ["#FF6384","#36A2EB","#FFCE56"]
    }]
};

$.get("employees.inc.php?employeesChart", function(data){
    var result = $.parseJSON(data);
    var test = data.datasets[0].data.push(result[0],result[1]);
    myDoughnutChart.update();
});

// Doughnut Chart
var myDoughnutChart = new Chart(ctx, {
    type: 'doughnut',
    data: data,
    options: {
            responsive: true,
            legend: {
                position: 'right',
            },
            animation: {
                animateScale: true,
                animateRotate: true
            }
        }
});

i don't understand why do i get an error. When i test it with console.log(result) im getting two integers but the code sample make data.datasets[0].data.push(result[0],result[1]) an error

Virb
  • 1,639
  • 1
  • 16
  • 25
Greg Ostry
  • 1,161
  • 5
  • 27
  • 52

0 Answers0