0

I have a bar graph that I'm attempting to create with chart.js that takes a PHP array and loads via ajax. I am able to load the data with ajax (verified in the console) but I cannot get the data in the graph - here is the data in the console:

enter image description here

I have not received any error messages so I'm perplexed at this point. Here is all of the code:

HTML

<?php
include 'connect.php';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

</head>

<body>

</canvas><canvas id="myChart"></canvas>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="javascript/charts.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>

</body>

JS

$(document).ready(function(){
	$.ajax({
		url: "prod_agg.php",
		method: "GET",
		success: function(data) {
			console.log(data);
			var date = [];
            var output = [];

			for(var i in data) {
				date.push(data[i].date);
				output.push(data[i].output);
			}

			var chartdata = {
				labels: date,
				datasets : [
					{
						label: 'Date',
						backgroundColor: 'rgba(200, 200, 200, 0.75)',
						borderColor: 'rgba(200, 200, 200, 0.75)',
						hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
						hoverBorderColor: 'rgba(200, 200, 200, 1)',
						data: output
					}
				]
			};

			var ctx = $("#myChart");

			var barGraph = new Chart(ctx, {
				type: 'bar',
				data: chartdata
			});
		},
		error: function(data) {
			console.log(data);
		}
	});
});

I receive an empty graph:

enter image description here

Any help on this issue would be greatly valued!

2
  • do you know in what format, or exactly what the console data is supposed to look like>' Commented Jun 19, 2017 at 20:30
  • The photo of the Console output is the correct format as far as I can tell. My guess is that the issue stems from attempting to push the values into the 'date' and 'output' arrays. Commented Jun 19, 2017 at 20:35

1 Answer 1

2

The reason it's not working is because, you are getting the response data as a JSON string not JSON object.

So, to make it work with ChartJS, you need to parse it first, using JSON.parse() method ...

$(document).ready(function() {
   $.ajax({
      url: "prod_agg.php",
      method: "GET",
      success: function(data) {
         console.log(data);

         var data = JSON.parse(data);   //parse JSON string

         var date = [];
         var output = [];
         ...
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.