Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I have following code

$objDbResultByModel = $objDatabase->Query($modelQuery);
$return_arr = array();
echo "<h3>Total Model No<br></h3><strong>";
while ($mixRow = $objDbResultByModel->FetchArray()) {
    $row_array['Model'] = $mixRow['Manu']." - " .$mixRow['model'];
    $row_array['Quantity'] = $mixRow['total'];

        array_push($return_arr,$row_array);
$jsonTable = json_encode($return_arr);
echo $jsonTable;

I get following results in my browser.

[ 
  {"Model No.":"HP - 3120-MT","Quantity":"1"},
  {"Model No.":"IBM - 6087-CN8","Quantity":"1"},
  {"Model No.":"Fujitsu - D2594","Quantity":"4"},
  {"Model No.":"Fujitsu - D2750","Quantity":"15"},
  {"Model No.":"HP - DC 7800","Quantity":"43"},
  {"Model No.":"HP - DC7700","Quantity":"1"},
  {"Model No.":"HP - DC7900","Quantity":"8"},
  {"Model No.":"Fujitsu - E5720","Quantity":"1"},
  {"Model No.":"RM - RM","Quantity":"5"},
  {"Model No.":"HP - XW4400","Quantity":"1"},
  {"Model No.":"HP - XW4600","Quantity":"2"}
]

But if I use jsonTable with google charts api it does not work. What I am doing wrong? See below.

<script type="text/javascript">

      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Model');
        data.addColumn('number', 'Quantity');
        data.addRows([

<?php echo $jsonTable; ?>

        ]);

        // Set chart options
        var options = {'title':'Model Quantity in this Shipment',
                       'width':400,
                       'height':300};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>

Can you please help me with what the problem is here?

share|improve this question
    
what happens if you remove the [] brackets around addRows. Also try pasting the results directly into addRows so you can confirm that the code works and it is indeed a data issue –  Jacob Raccuia Jul 24 '14 at 13:16
    
I have tried that and it does not work –  user2107349 Jul 24 '14 at 13:20
    
Even when you hardcode the JSON? data.addRows([{'Model':'abc','Quantity':10}]); If so, we know the error is in your datatables –  Jacob Raccuia Jul 24 '14 at 13:27
    
How can I convert my json results to work with google charts api. –  user2107349 Jul 24 '14 at 13:32
    
you are not answering my questions, which makes it difficult to help you. Try taking the code directly from here: developers.google.com/chart/interactive/docs/gallery/piechart, copy and paste the entire thing and start from scratch. Slowly change the information and retest so you can see exactly what you're doing that breaks it. –  Jacob Raccuia Jul 24 '14 at 13:49

1 Answer 1

The DataTable cannot use the data format you are inputting. Change this:

$row_array['Model'] = $mixRow['Manu']." - " .$mixRow['model'];
$row_array['Quantity'] = $mixRow['total'];

to this:

$row_array = array($mixRow['Manu'] . " - " . $mixRow['model'], $mixRow['total']);
share|improve this answer
    
Tried this but it does not work, browser does not even render the page –  user2107349 Jul 24 '14 at 14:17
    
Open the page and view the source; if you get nothing, then there is a syntax error in your PHP; if you see your HTML, then there is likely an error in javascript. In the former case, you need to debug your PHP; in the latter case, update your question with the generated javascript and I'll see if I can figure out why it isn't working. –  asgallant Jul 24 '14 at 14:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.