pChart - Is possible to combine a stacked bar chart and a regular bar chart?

526 Views Asked by At

I'm trying to use pChart to create a combo chart, using 3 series. Two series would be for a stacked bar and the remaining series would be for a bar chart. What I'm trying to achieve is put a stacked bar column with a regular bar on the side (like a total bar). Is there any way to achieve this? I know that pChart has the option to combo charts, but it seems that it only works really nice when you combine different kinds of charts, like bar and line for example. But when I try to combine similar graphs, like bar and stacked bar, they seem to overwrite each other. pChart documentation isn't very clear on the subject (or I'm really missing something there). My code:

$MyData = new pData();
$Mydata->addPoints(array(0,97,149,167),"Previous");
$Mydata->addPoints(array(97,52,18,10),"Current");
$Mydata->addPoints(array(97,149,167,177),"Total");

$Mydata->setAxisName(0,"Quantity");
$Mydata->addPoints(array('1','2','3','4'),"Period");
$Mydata->setSerieDescription("Period","Period");
$Mydata->setAbscissa("Period");
$Mydata->setAbscissaName("Period");

$MyChart = new pImage(975,520,$MyData);
$MyChart->setFontProperties(array("FontName"=>"pChart/fonts/verdana.ttf","FontSize"=>11));

$MyChart->setGraphArea(270,100,940,390);
$MyChart->drawFilledRectangle(270,100,940,390,array("R"=>255,"G"=>255,"B"=>255,"Surrounding"=>-200,"Alpha"=>10));
$MyChart->drawScale(array('CycleBackground'=>TRUE,'Mode'=>SCALE_MODE_START0,'Factors'=>array(20),"Pos"=>SCALE_POS_TOPBOTTOM,"DrawSubTicks"=>FALSE,"GridR"=>128,"GridG"=>128,"GridB"=>128,"GridAlpha"=>20));
$MyChart->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
$MyChart->setFontProperties(array("FontName"=>"pChart/fonts/verdana.ttf","FontSize"=>11));

$MyData->setSerieDrawable("Total",FALSE);
$MyChart->drawStackedBarChart(array("DisplayValues"=>TRUE,"DisplayColor"=>DISPLAY_AUTO,"Rounded"=>TRUE,"Surrounding"=>60));

$MyData->setSerieDrawable("Previous",FALSE);
$MyData->setSerieDrawable("Current",FALSE);
$MyData->setSerieDrawable("Total",TRUE);
$MyChart->drawBarChart(array("DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"DisplayColor"=>DISPLAY_AUTO,"Rounded"=>TRUE,"Surrounding"=>60));

$MyChart->setShadow(FALSE);

$MyData->setSerieDrawable("Previous",TRUE);
$MyData->setSerieDrawable("Current",TRUE);
$MyChart->drawLegend(30,20,array("Alpha"=>20,"Mode"=>LEGEND_VERTICAL));

$MyChart->Render("Graph.png");

Thanks for any help.

1

There are 1 best solutions below

0
On
$MyData->setSerieDrawable("Received",true);
$MyData->setSerieDrawable("Despatched",true);

pData::setSerieDrawable does not work in PHP 5.2.6 - it might work in later versions but my OOP knowledge is limited. Previously, when $myPicture = new pImage($imgW,400,$MyData); was called with an instance of $MyData, this is added to by pImage I think.

The problem is that $MyData->setSerieDrawable("Despatched",true); affects $MyData and not the data in pImage. My solution was to add a new function in pImage:

function SetisDrawable($SerieName='', $isDrawable=false) {
    if(!empty($SerieName)){
        $this->DataSet->Data["Series"][$SerieName]["isDrawable"] =  $isDrawable;
    }
}

$myPicture->SetisDrawable("Received", FALSE);
$myPicture->SetisDrawable("Despatched", FALSE);
$myPicture->SetisDrawable("WIP", TRUE);
$myPicture->drawLineChart(array("DisplayValues"=>TRUE, "DisplayOffset"=>"15"));