1

I'm using Chart.js plugin to create a bar chart with sales and purchases values by year. These values are stores in mysql database and I bring it via PHP/AJAX.

HTML

<canvas id="mybarChart"></canvas>

PHP

$sql = "SELECT YEAR(date) as years, SUM(amount) AS total FROM purchases GROUP BY YEAR(date)";
$res = mysql_query($sql);
$totalpurchases = [];

$sqll = "SELECT YEAR(date) as years, SUM(amount) AS total FROM sales GROUP BY YEAR(date)";
$ress = mysql_query($sqll);
$totalsales = [];

while($row = mysql_fetch_array($res)){
    $totalpurchases[] = [$row['total']];
}

while($roww = mysql_fetch_array($ress)){
    $totalsales[] = [$roww['total']];
}

echo json_encode(array($totalpurchases,$totalsales));

And I made my JS code like this:

function show_chartbar(lbl01,lbl02){
    var ctx = document.getElementById("mybarChart");
    var mybarChart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: ["2013", "2014", "2015", "2016", "2017"],
            datasets: [{
            label: '# Total Purchase',
            backgroundColor: "#26B99A",
            data: lbl01
            }, {
            label: '# Total Sales',
            backgroundColor: "#03586A",
            data: lbl02
            }]
        },

        options: {
            scales: {
               yAxes: [{
                ticks: {
                    beginAtZero: true
                     }
                }]
            }
        }
    });
}

if ($('#mybarChart').length ){    

    $.ajax({
        url: "scripts/values.php",
        type: "GET",                    
        dataType: "json",               
        success: function(resp)
        {
            var totpurchases = resp[0];
            var totsales = resp[1];
            console.log("Total Purchases: "+totpurchases);
            console.log("Total Sales: "+totsales);
            show_chartbar(totpurchases,totsales);
        }
    });

}

In console it's showing values correctly but it doesn't display in the chart:

enter image description here

I tried to add additional brackets in data option but it's showing the same.

UPDATE

console.dir(resp);

enter image description here

How can I fix it? I'd like some help.

1
  • I would imagine your data is looking for an array, and your getting back an object from the json_encode since arrays work on Key value pairs. Can you console.dir(resp) in your ajax success function and show us the results. Thanks. Commented Apr 2, 2018 at 18:57

1 Answer 1

1

You are creating an Array of Arrays. I believe this will fix this issue.

while($row = mysql_fetch_array($res)){
    $totalpurchases[] = $row['total'];
}

while($roww = mysql_fetch_array($ress)){
    $totalsales[] = $roww['total'];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.