1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Google Charts Dynamic Arrays PHP Without SQL

Discussion in 'PHP' started by Frenzyy, Feb 11, 2018.

  1. #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'-
    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 )
    Code (markup):
    Array 'sumOf'-
    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 (markup):
    
    <?php
    echo json_encode($name);
        echo json_encode($sumOf);   
    ?>
    
     <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
        <script type="text/javascript">
          google.charts.load('current', {'packages':['corechart']});
          google.charts.setOnLoadCallback(drawChart);
    
          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);
          }
        </script>
    
    Code (markup):
    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 wto arrays would be updated. I hope you understand what I mean and would really appreciate if someone could help me out with this.
    SEMrush
     
    Solved! View solution.
    Frenzyy, Feb 11, 2018 IP
    SEMrush
  2. #2
    Try the following (I don't think the chart would work via json_encode form what I can see here https://developers.google.com/chart/interactive/docs/gallery/piechart):

    
    <?php
    $array = array_combine($name, $sumOf);
    ?>
    function drawChart() {
    
    var data = google.visualization.arrayToDataTable([
    ['Name', 'Allocation'],
    <?php
    foreach ($array as $name => $allocation):
        echo "['$name', $allocation]";
        echo ($allocation != end($array)) ? ',' : '';
    endforeach;
    ?>
    ]);
    
    
    Code (markup):
    Not optimized or tested, might have to do some tweaking.
     
    ThePHPMaster, Feb 11, 2018 IP
  3. Frenzyy

    Frenzyy Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Wow! This is actually working! I've been looking for an answer and testing different things out for a few days yet nothing worked but this does. Thank you so much! This is exactly what I wanted.
    Just one last question. I've noticed that for some reason there's an option 'Other' on the pie chart, listed under names, yet there's no such result in the array. Is there any way to get rid of it or is it just the way this chart works?
    Thanks again!
     
    Frenzyy, Feb 12, 2018 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    51
    Best Answers:
    33
    Trophy Points:
    150
    #4
    I would keep it, won't hurt.
     
    ThePHPMaster, Feb 13, 2018 IP
  5. Frenzyy

    Frenzyy Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    If I have to, then I will, sure. I'm asking because it's just that it doesn't make much sense in this scenario because there simply can't be an option 'Other' given that it's a portfolio of stocks so it'll always be filled 100%, without any space for 'Other', if you know what I mean.
    Thanks again!
     
    Frenzyy, Feb 14, 2018 IP
  6. Frenzyy

    Frenzyy Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    Disregard- Thread can be closed. Thanks for the help.
     
    Frenzyy, Feb 17, 2018 IP