422,530 Members | 1,071 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,530 IT Pros & Developers. It's quick & easy.

Google Charts Dynamic Arrays PHP Without SQL

P: 1
I'm asking this question because all the answers I could find for similar problems were using MySQL whereas I'm not, just a JSON API to get the data, which I then put into arrays that I want to display as a Google graph. All I know is that I have to somehow format the arrays properly in order to get them to display but I have no idea how to do it in my case. I would just like to have a simple pie chart, based on the arrays below. So far I'm getting a blank space on the site. I tried something with Json_encode before but it didn't work so I decided to leave it as it is and come here instead. Here are the arrays after I do print_r:
Array 'name'-
Expand|Select|Wrap|Line Numbers
  1. Array ( [0] => Facebook Inc [1] => Alphabet Class A [2] => Apple Inc [3] => Ford Motor Company [4] => Adv Micro Devices [5] => Morgan Stanley [6] => Berkshire Hath Hld B [7] => JP Morgan Chase & Co )
Array 'sumOf'-
Expand|Select|Wrap|Line Numbers
  1. Array ( [0] => 5811.63 [1] => 116135.97 [2] => 1564.1 [3] => 1053 [4] => 113.1 [5] => 521.4 [6] => 1960.2 [7] => 1100.4 )
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo json_encode($name);
  3.     echo json_encode($sumOf);    
  4. ?>
  5.  
  6.  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  7.     <script type="text/javascript">
  8.       google.charts.load('current', {'packages':['corechart']});
  9.       google.charts.setOnLoadCallback(drawChart);
  10.  
  11.       var ar = <?php echo json_encode($name) ?>;
  12.       var ar1 = <?php echo json_encode($sumOf) ?>;
  13.  
  14.       function drawChart() {
  15.  
  16.         var data = google.visualization.arrayToDataTable([
  17.   ['Name', 'Allocation'],
  18.   [ar, ar1]
  19. ]);
  20.  
  21.         var options = {
  22.           title: 'Portfolio Allocation'
  23.         };
  24.  
  25.         var chart = new google.visualization.PieChart(document.getElementById('piechart'));
  26.  
  27.         chart.draw(data, options);
  28.       }
  29.     </script>
  30.  
The thing with mine is that these arrays are generated dynamically. Meaning that at any point in time, something can be added or taken away from them by the user. Therefore, these graphs would have to be appropriately adjusted whenever input to arrays would be updated. I hope you understand what I mean and would really appreciate if someone could help me out with this.
Feb 11 '18 #1
Share this Question
Share on Google+
1 Reply


P: 3
array_combine was needed to join the two arrays together and then inside of 'function drawChart' a simple foreach loop was required, like this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. foreach ($array as $name => $allocation):
  3.     echo "['$name', $allocation]";
  4.     echo ($allocation != end($array)) ? ',' : '';
  5. endforeach;
  6. ?>
Feb 14 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.