I am working on a little project on OpenCATS, developing some charts. In one of the charts, I need to add multiple plots to a PlotGroup and add Legends as well.
I have written a small function that creates the chart, however I have difficulty displaying the legends when I add them to a PlotGroup object.
Here is the code:
public function draw($format = false)
{
/* Make sure we have GD support. */
if (!function_exists('imagecreatefromjpeg'))
{
die();
}
if ($format === false)
{
$format = IMG_PNG;
}
$group = new PlotGroup();
$graph = new Graph($this->width, $this->height, NULL, 0, $this->width-135);
$graph->setFormat($format);
$graph->setBackgroundColor(new Color(0xF4, 0xF4, 0xF4));
$graph->shadow->setSize(3);
$graph->title->set($this->title);
$graph->title->setFont(new Tuffy(10));
$graph->title->setColor(new Color(0x00, 0x00, 0x8B));
$graph->border->setColor(new Color(187, 187, 187, 15));
$group->axis->bottom->setLabelText($this->xLabels);
$group->axis->bottom->label->setFont(new Tuffy(8));
$group->setPadding(25, 145, 10, 22);
$plotcount = 0;
$plot = array();
foreach ($this->xValues as $xVal)
{
$plotcount++;
$plot[$plotcount-1] = new LinePlot($xVal, LinePlot::LINE);
$plot[$plotcount-1]->setPadding(25, 145, 10, 22);
if ($plotcount % 5 ==0 )
$plot[$plotcount-1]->setColor(new Blue);
else if ($plotcount % 4 ==0 )
$plot[$plotcount-1]->setColor(new DarkGreen);
else if ($plotcount % 3 ==0 )
$plot[$plotcount-1]->setColor(new Red);
else if ($plotcount % 2 ==0 )
$plot[$plotcount-1]->setColor(new Orange);
$plot[$plotcount-1]->setThickness(2);
$plot[$plotcount-1]->legend->add($plot[$plotcount-1], $this->legends[$plotcount-1]);
$plot[$plotcount-1]->legend->setTextFont(new Tuffy(8));
$plot[$plotcount-1]->legend->setPadding(3, 3, 3, 3, 3);
$plot[$plotcount-1]->legend->setPosition(1, 0.825);
$plot[$plotcount-1]->legend->setBackgroundColor(new Color(0xFF, 0xFF, 0xFF));
$plot[$plotcount-1]->legend->border->setColor(new Color(0xD0, 0xD0, 0xD0));
$plot[$plotcount-1]->legend->shadow->setSize(0);
$group->add($plot[$plotcount-1]);
}
$graph->add($group);
$graph->draw();
}
}
If I draw only one LinePlot it works: So for instance if I change $graph->add($group); to $graph->add($plot[0]); then only one of the lines will show up with the legend next to it.