How to addd JSON array daya into google column chart

I am trying to add data dynamically into a chart but the console keeps on sending a message:
“Uncaught SyntaxError: Unexpected token ;”
Here is the code:

////////       Convert PhP $data array to JavaScript array    //////////
	  var datum = <?php echo json_encode($data); ?>;
      google.charts.load('current', {'packages':['corechart', 'bar']});

	////////////////////////    Drawing the graph    //////////////////////
      function drawVisualization() {
         var data = google.visualization.arrayToDataTable([
			  data.addColumn('string', 'datum.profit');
              data.addColumn('number', 'datum.TotalBuyProfit');
			  data.addColumn('number', 'datum.TotalBuyLoss');
			  data.addColumn('number', 'datum.TotalSellProfit');
			  data.addColumn('number', 'datum.TotalSellLoss');

			  for (var i = 0; i < datum.length; i++){
         //['zzz', 'Profit', 'Loss', 'Profit', 'Loss'],
        // ['aaa', 165, -38, 165, -38],
         //['sss', 135, -120, 165, -38],
		// ['ccc', 165,-93, 165, -38],
        // ['ggg', 135, -110, 165, -38],
		// ['hhh', 135, -112, 165, -38],
		// ['rrr', 165, -98, 165, -98],
        // ['vvv', 135, -120, 165, -38]

Here is the JSOn file:



The data after the // is raw data to see that the chart works well
What am I doing wrong?
I find many examples on the web, all are different/
I can’t find a site where I can get a good explanation.

What is the way to make it work?

The error’s dead right. In the drawVisualization function, semicolons don’t belong in the data array.

To be a little more specific,

arrayToDataTable is expecting an array (and optionally a boolean second parameter to indicate whether the first row are column headers).
What you seem to be doing is trying to stuff all of your commands into the initial constructor. Don’t.

I’d suggest looking at the examples google provides In the API specification for this function.

You appear to be trying to use the “Version 2” example, mixed with Version 1.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.