Google Charts Dynamic Arrays PHP Without SQL

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Jan 27, 2018 7:30 am

Sun Feb 11, 2018 9:46 am

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'-

Code: Select all

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'-

Code: Select all

Array ( [0] => 5811.63 [1] => 116135.97 [2] => 1564.1 [3] => 1053 [4] => 113.1 [5] => 521.4 [6] => 1960.2 [7] => 1100.4 )

Code: Select all

echo json_encode($name);
    echo json_encode($sumOf);	

 <script type="text/javascript" src=""></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});

      var ar = <?php echo json_encode($name) ?>;
      var ar1 = <?php echo json_encode($sumOf) ?>;

      function drawChart() {

        var data = google.visualization.arrayToDataTable([
  ['Name', 'Allocation'],
  [ar, ar1]

        var options = {
          title: 'Portfolio Allocation'

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));

        chart.draw(data, options);
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.

Post Reply