postition of left side of x-axis label always centered
Status: Beta
Brought to you by:
ayashisunyday
Postition of left side of x-axis label always centered making long text be positioned on the right half of the plot (no example code given, just take any plot and use a few words in x-axis to see the effect).
The math on the function to rite the X axis label was just dead wrong. I can explain what was wrong if you want, but here is a fix for "pChart.class" beginning around line 630 (with the comment "Write the X Axis caption if set"). Just replace the entire "if" block with this code:
/* Write the X Axis caption if set */
if ( isset($DataDescription["Axis"]["X"]) )
{
$Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["X"]);
//
// bugfix: incorrect coordinates were being used to calculate the text width
//
$TextWidth = abs($Position[3])+abs($Position[1]);
//
// bugfix: the math was wrong to determine the center point.
//
$RightMargin = ($this->XSize - abs($this->GArea_X2));
$LeftMargin = abs($this->GArea_X1);
$TextLeft = (($this->XSize / 2) - $RightMargin) - ($TextWidth / 2) + $LeftMargin;
imagettftext($this->Picture,$this->FontSize,0,$TextLeft,$YMax+$this->FontSize+5,$C_TextColor,$this->FontName,$DataDescription["Axis"]["X"]);
}
}
@blairblends -
You can accomplish the same thing (fixing the bug) without so many changes (only two lines). This fix applies to version 1.27d so the line #s may change in future versions.
Change line 633 to:
$Position = imageftbbox($this->FontSize,0,$this->FontName,$DataDescription["Axis"]["X"]);
Basically we just changed the "90" to "0" (because the text is horizontal, not vertical).
And change line 635 to:
$TextLeft = (($this->GArea_X2 - $this->GArea_X1) / 2) + $this->GArea_X1 - ($TextWidth/2);
(Changed the last "+" to a "-" so that the math is correct.)
The label should now be correctly centered. :)