I'm trying to dynamically create a bar graph where I am getting a set of historical data, comparing 4 groups for the past 12 months. Right now, I only have 3 months of data, but that doesn't really matter. Anyway, I have generated two "cousins" a line graph and side by side single graphs comparing each group. The error message I get is this:

One of the objects submitted to GroupBar is not a BarPlot. Make sure that you created the Group Bar plot from an array of BarPlot or AccBarPlot objects. (Class = )

This tells me I'm doing something wrong with my array. My code looks like this:

<?php

include("jpgraph/src/jpgraph.php");
include("jpgraph/src/jpgraph_bar.php");

// Create the basic graph
$graph = new Graph(750, 500,"auto");	
$graph->SetScale("textlin");
$graph->img->SetMargin(40,80,30,40);

// set color- code remove - irrelevant

// Get the data
$st = $_GET['start'];

$tg = $_GET['total_group'];
for ($i=0; $i < $tg; $i++) {
  $grup = "grp_".$i;
  $groupL[$i] = $_GET[$grup];
  $groupU[$i] = strtoupper($_GET[$grup]);
}

$tm = $_GET['total_month'];
for ($i=$st; $i < $tm; $i++) {
  $m = "m".$i;
  $mm[$i] = $_GET[$m];
  $j = $i - $st;
  $month[$j] = $_GET[$m];
}

for ($i=0; $i < $tg; $i++) {                     // total groups
  for ($m=$st; $m < $tm; $m++) {                 // total months
    $groupV = $groupL[$i]."_".$m;
    $j = $m - $st;
    $datay[$i][$j] = $_GET[$groupV];
  }
}

// Adjust the position of the legend box
$graph->legend->Pos(0.02,0.15);

// Adjust the color for theshadow of the legend - code remove - irrelevant
// Set axis titles and fonts - code remove - irrelevant

for ($i=0; $i < $tg; $i++) {                     // total groups
  $bplot[$i] = new BarPlot($datay[$i]);
  $bplot[$i]->SetFillColor($color[$i]);
  $bplot[$i]->SetLegend($groupU[$i]);        // Set the legends for the plots
}

$gbarplot = new GroupBarPlot(array($bplot));
$gbarplot->SetWidth(0.4);
$graph->Add($gbarplot);

$graph->Stroke();
?>

I figure it's something in the way I'm handling $bplot, but I'm not exactly sure what to do there. Any suggestions? Oh, btw, my input line looks like this:

<img src="all_bar.php?grp_0=mgroup&grp_1=kgroup&grp_2=jgroup&grp_3=ggroup
&mgroup_0=421&mgroup_1=385&mgroup_2=606
&kgroup_0=279&&kgroup_1=579&kgroup_2=170
&jgroup_0=272&jgroup_1=262&jgroup_2=150
&ggroup_0=409&ggroup_1=489&ggroup_2=682
&m0=Jul&m1=Aug&m2=Sep
&total_month=3&total_group=4&start=0" />

    In the example, the GroupBarPlot line looks like this:

    $gbplot = new GroupBarPlot(array($b1plot,$b2plot));

    but in my code, I have $bplot in an array of indeterminate length, so how do I translate that into the correct array format? What I have right now that isn't working is:

    $gbarplot = new GroupBarPlot(array($bplot));

    I've also tried:

    $gbarplot = new GroupBarPlot(array(array($bplot)));

    which doesn't work either.

      Okay, I've figured out what I need to do. I'm just not sure how to get there on the fly. What I need the line to look like is this:

      $gbarplot = new GroupBarPlot(array($bplot[0], $bplot[1], $bplot[2]));

      Where $bplot could have up to 12 elements. If I hard code this, it works. I just have to figure out how to re-create this same line on the fly... Any suggestions?

        I took care of the the clunky way with a series of if-elseif statements...

        if       ($tc == 1) {
          $gbarplot = new GroupBarPlot(array($bplot[0]));
        } elseif ($tc == 2) {
          $gbarplot = new GroupBarPlot(array($bplot[0],$bplot[1]));
        } elseif ($tc == 3) {
          $gbarplot = new GroupBarPlot(array($bplot[0],$bplot[1],$bplot[2]));
        } ...

        Clunky, but it works. I know there will be some where between 1 and 12 months worth of data so, anyway until a better solution presents it's self, this is resolved for now.

          Can you add additional datasets to an existing GroupBarPlot, or do you really have to add all of them at construction time?

            I think I have to add them all at construction time, but I could try it the other way, you mean like in a for loop:

            for ($i=0; $i < $tg; $i++) {
              $gbarplot = new GroupBarPlot(array($bplot[$i]));
            }

            right? It sure would be a lot "prettier" 😉

            BTW, I saw the best programmer line yesterday:

            There are only 10 types of people in the world, those that understand binary and those that don't.

              I was trying to make this way too hard. It was so easy :bemused: :rolleyes: :glare: 😃

              for ($i=0; $i < $tg; $i++) {                  // total groups
                $bplot[$i] = new BarPlot($datay[$i]);
                $bplot[$i]->SetFillColor($color[$i]);
                $bplot[$i]->SetLegend($groupU[$i]);
              }
              $gbarplot = new GroupBarPlot($bplot);
              
                Write a Reply...