Question : php pchart problem: pie graph with PIE_PERCENTAGE_LABEL

Hi Experts,

1. I have strange problems with pchart. If I try to create a simple pie graph with the percentage and labels visible using the PIE_PERCENTAGE_LABEL property, it doesn't show anything anymore, no %numbers nor the labels. While only using the "PIE_PERCENTAGE" property, it works, but not with together. Why could this be?

2. Other problems include crashing whenever I try to load a color palette from an external file - With a right path to the file!

3. Third thing is, that is there any way to remove the round edged rectangle background from the pie legend?

Here's the code I have been testing.


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
<?php
	// Standard inclusions   
	include("includes/lib/pchart/pData.class");
	include("includes/lib/pchart/pChart.class");
	
	session_start();
	
	// Dataset definition 
	$DataSet = new pData;
	$DataSet->AddPoint(array(30,20,20,20,20),"Serie1");
	$DataSet->AddPoint(array($_SESSION['user']['rank'],"Feb","Mar","Apr","May"),"Serie2");
	$DataSet->AddAllSeries();
	$DataSet->SetAbsciseLabelSerie("Serie2");
	
	// Initialise the graph
	$Test = new pChart(300,300);
	$Test->setColorPalette(0,178,31,98);
	$Test->setColorPalette(1,100,100,100);
	$Test->setColorPalette(2,150,150,150);
	$Test->setColorPalette(3,180,180,180);
	$Test->setColorPalette(4,200,200,200);
//	$Test->loadColorPalette("sample/softtones.txt"); // CRASHES if uncommented. The path is correct.

	$Test->drawFilledRectangle(0,0,300,300,250,250,250);
	
	
	// Write the title
 	$Test->setFontProperties("fonts/HMH-font.ttf",15);
 	$Test->drawTitle(10,20,"Sales per month",100,100,100);
	
	// Draw the pie chart
	$Test->setFontProperties("fonts/tahoma.ttf",8);
	$Test->drawFlatPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),120,150,80,PIE_PERCENTAGE_LABEL,4);
	$Test->drawPieLegend(230,55,$DataSet->GetData(),$DataSet->GetDataDescription(),250,250,250);
	$Test->Stroke();
?>

Answer : php pchart problem: pie graph with PIE_PERCENTAGE_LABEL

1. PIE_PERCENTAGE_LABEL works. I have tested using version pChart v1.27  and (PHP 5.2.13)  on W32 (windows XP) and Linux kernel 2.6.27.29 (Opensuse 11.1) . Both worked fine however I noticed you did not specify the complete path to your Fonts folder (just if u are working with the one withing pChart you must do it)
2. Remember you have to remove lines referring to ColorPalette if you want to load it from file. Oh, the file contain for softtones.txt should look like this:

0,178,31,98
1,100,100,100
2,150,150,150
3,180,180,180
4,200,200,200


3. Yes, but you have to add a parameter to function drawPieLegend within file pChart/pChart.class in order to do that. Just go to line ~1028 and $Border to it so you have:

function drawPieLegend($XPos,$YPos,$Data,$DataDescription,$R,$G,$B,$Border=TRUE)

then find these declarations:

$this->drawFilledRoundedRectangle($XPos+1,$YPos+1,$XPos+$MaxWidth+1,$YPos+$MaxHeight+1,5,$R-30,$G-30,$B-30);     $this->drawFilledRoundedRectangle($XPos,$YPos,$XPos+$MaxWidth,$YPos+$MaxHeight,5,$R,$G,$B);

at line ~1054 (may be different)...and add if($Border) so you have the above declarations enclosed within that condition:

if($Border)
{
$this->drawFilledRoundedRectangle($XPos+1,$YPos+1,$XPos+$MaxWidth+1,$YPos+$MaxHeight+1,5,$R-30,$G-30,$B-30);     $this->drawFilledRoundedRectangle($XPos,$YPos,$XPos+$MaxWidth,$YPos+$MaxHeight,5,$R,$G,$B);

}

then just add false to you drawPieLegend declaration in your file:

$Test->drawPieLegend(230,55,$DataSet->GetData(),$DataSet->GetDataDescription(),250,250,250,false);

Take a look at my working code...I have change few things:

-the path to pChart is now in a variable
-error_reporting(E_ERROR); . Just to be sure it only displays errors(if you want). Otherwise it will not render the image.

Remember to leave the font name to the previous u used. I changed it coz I don't have it.






1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
<?php
    error_reporting(E_ERROR);
    session_start();
    
    $pathTopChart='pchart/';

	// Standard inclusions   
	include("$pathTopChart/pchart/pData.class");
	include("$pathTopChart/pchart/pChart.class");	
	
    
    $_SESSION['user']['rank']=1;
	
	// Dataset definition 
	$DataSet = new pData;
	$DataSet->AddPoint(array(30,20,20,20,20),"Serie1");
	$DataSet->AddPoint(array($_SESSION['user']['rank'],"Feb","Mar","Apr","May"),"Serie2");
	$DataSet->AddAllSeries();
	$DataSet->SetAbsciseLabelSerie("Serie2");
	
	// Initialise the graph
	$Test = new pChart(300,300);
	$Test->loadColorPalette("softtones.txt");
	$Test->drawFilledRectangle(0,0,300,300,250,250,250);
	// Write the title
 	/*$Test->setFontProperties("fonts/HMH-font.ttf",15);*/
    $Test->setFontProperties("$pathTopChart/Fonts/tahoma.ttf",15);    
 	$Test->drawTitle(10,20,"Sales per month",100,100,100);	
	// Draw the pie chart
	$Test->setFontProperties("$pathTopChart/Fonts/tahoma.ttf",8);
	$Test->drawFlatPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),120,150,80,PIE_PERCENTAGE_LABEL,4);
	$Test->drawPieLegend(230,55,$DataSet->GetData(),$DataSet->GetDataDescription(),250,250,250,false);
	$Test->Stroke();
?>
Random Solutions  
 
programming4us programming4us