0

I'm using this script from Google to create a combo chart.

https://developers.google.com/chart/interactive/docs/gallery/combochart

Because of my situation, I'm using php to get the data from the database. Since the problem isn't about getting the data, for the code below I'm just hard coding $CT_Companies=4 to show the problem (in practice the number 4 comes from the database.

Here is the php function to create the configuration series needed to make the combo bar and line chart.

$CT_Companies=4;

function bar_series(){
   global $CT_Companies;
   $ser="";
   for ($i = 0; $i <= $CT_Companies; $i++) {
      if ($i==$CT_Companies )  { 
          $ser .= $i.":{type: 'line'}";
      } else {
          $ser .= $i.":{visibleInLegend: true}, ";
      }
   } 
return $ser;    
}

and here is the javascript I use to create the configuration series.

 var options = {
   series: {
            <?php echo bar_series(); ?>
        }
  }

This does not work since while it does show the graph as expected, there is no line graph. Now, if I put $CT_Companies=4 inside the bar_series function it works fine and the line graph shows as expected. So it looks like the global isn't working. One more piece of information...if I echo bar_series() in the php section (not the javascript section) in both cases, the series is echoed and looks as expected. I very confused why this isn't working.

Allen
  • 722
  • 4
  • 13
  • 31
  • If you view the page source from your browser, do you see the four series? I suspect a syntax error since you're not outputting a `,` between the individual series, but that assumes the `global` statement works fine. – rickdenhaan Jun 08 '21 at 19:26
  • I just looked at the output in the Chrome javascript developers console and saw that when I put in the global variable it constructed 6 series and with the variable inside the function it resulted in 5 series. I have 5 columns of data, so that was the problem. I changed my function accordingly. But it is still strange there would be any difference. – Allen Jun 08 '21 at 19:50
  • Never create JSON by hand. Ever. And don't use global variables. Pass arguments to your functions. – miken32 Jun 08 '21 at 20:42
  • Good point, I changed it to have it pass the argument to the function. But the same weird problem remains. The output of the function is 4 series, but chrome shows 5 series passed. That's why I had to change the function. – Allen Jun 08 '21 at 20:49
  • That's because you're using `<=`. If you want exactly `$CT_Companies` series, either start your loop with `$i = 1` or loop while `$i < $CT_Companies` rather than `<=` (and change your condition to `if ($i + 1 == $CT_Companies)`. – rickdenhaan Jun 08 '21 at 21:16
  • So <= doesn’t work anymore? – Allen Jun 08 '21 at 21:18
  • Sure it does. But a loop from 0 <= 4 will go 5 times: for the numbers 0, 1, 2, 3 and 4. – rickdenhaan Jun 08 '21 at 21:18
  • Yes. That’s what I had but when looking at chrome developer that was showing 0,1,2,3,4,5. That was what was so weird – Allen Jun 08 '21 at 21:20
  • 1
    Hmm, that's not how the code in your question behaves: https://3v4l.org/VNTFp -- are you sure there's no other code manipulating the data? – rickdenhaan Jun 08 '21 at 21:23
  • I’ll check it out and let you know. Thanks – Allen Jun 08 '21 at 21:26
  • you were right..I had variable in the php code that was causing the problem..thanks. – Allen Jun 09 '21 at 13:48

0 Answers0