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.

11
  • 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. Commented Jun 8, 2021 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. Commented Jun 8, 2021 at 19:50
  • Never create JSON by hand. Ever. And don't use global variables. Pass arguments to your functions. Commented Jun 8, 2021 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. Commented Jun 8, 2021 at 20:49
  • 1
    Hmm, that's not how the code in your question behaves: 3v4l.org/VNTFp -- are you sure there's no other code manipulating the data? Commented Jun 8, 2021 at 21:23

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.