📄 jpgraph_pie.php
字号:
if ( is_numeric( $label ) )
{
if ( 0 < $label )
{
$w = $img->GetTextWidth( sprintf( $this->value->format, $label ) );
}
else
{
$w = $img->GetTextWidth( sprintf( $this->value->negformat, $label ) );
}
}
else
{
$w = $img->GetTextWidth( $label );
}
if ( 1 < $this->ilabelposadj && $this->ilabelposadj < 5 )
{
$r *= $this->ilabelposadj;
}
$r += $img->GetFontHeight( ) / 1.5;
$xt = round( $r * cos( $a ) + $xc );
$yt = round( $yc - $r * sin( $a ) );
while ( $a < 0 )
{
$a += 2 * M_PI;
}
while ( 2 * M_PI < $a )
{
$a -= 2 * M_PI;
}
if ( 7 * M_PI / 4 <= $a || $a <= M_PI / 4 )
{
$dx = 0;
}
if ( M_PI / 4 <= $a && $a <= 3 * M_PI / 4 )
{
$dx = ( $a - M_PI / 4 ) * 2 / M_PI;
}
if ( 3 * M_PI / 4 <= $a && $a <= 5 * M_PI / 4 )
{
$dx = 1;
}
if ( 5 * M_PI / 4 <= $a && $a <= 7 * M_PI / 4 )
{
$dx = 1 - ( $a - M_PI * 5 / 4 ) * 2 / M_PI;
}
if ( 7 * M_PI / 4 <= $a )
{
$dy = ( $a - M_PI - 3 * M_PI / 4 ) * 2 / M_PI;
}
if ( $a <= M_PI / 4 )
{
$dy = 1 - $a * 2 / M_PI;
}
if ( M_PI / 4 <= $a && $a <= 3 * M_PI / 4 )
{
$dy = 1;
}
if ( 3 * M_PI / 4 <= $a && $a <= 5 * M_PI / 4 )
{
$dy = 1 - ( $a - 3 * M_PI / 4 ) * 2 / M_PI;
}
if ( 5 * M_PI / 4 <= $a && $a <= 7 * M_PI / 4 )
{
$dy = 0;
}
$this->value->Stroke( $img, $label, $xt - $dx * $w, $yt - $dy * $h );
}
}
}
class PiePlotC extends PiePlot
{
private $imidsize = 0.5;
private $imidcolor = "white";
public $midtitle = "";
private $middlecsimtarget = "";
private $middlecsimalt = "";
public function PiePlotC( $data, $aCenterTitle = "" )
{
parent::pieplot( $data );
$this->midtitle = new Text( );
$this->midtitle->ParagraphAlign( "center" );
}
public function SetMid( $aTitle, $aColor = "white", $aSize = 0.5 )
{
$this->midtitle->Set( $aTitle );
$this->imidsize = $aSize;
$this->imidcolor = $aColor;
}
public function SetMidTitle( $aTitle )
{
$this->midtitle->Set( $aTitle );
}
public function SetMidSize( $aSize )
{
$this->imidsize = $aSize;
}
public function SetMidColor( $aColor )
{
$this->imidcolor = $aColor;
}
public function SetMidCSIM( $aTarget, $aAlt )
{
$this->middlecsimtarget = $aTarget;
$this->middlecsimalt = $aAlt;
}
public function AddSliceToCSIM( $i, $xc, $yc, $radius, $sa, $ea )
{
while ( 2 * M_PI < $sa )
{
$sa -= 2 * M_PI;
}
while ( 2 * M_PI < $ea )
{
$ea -= 2 * M_PI;
}
$sa = 2 * M_PI - $sa;
$ea = 2 * M_PI - $ea;
if ( abs( $sa - $ea ) < 0.0001 )
{
$sa = 2 * M_PI;
$ea = 0;
}
$xp = floor( $this->imidsize * $radius * cos( $ea ) + $xc );
$yp = floor( $yc - $this->imidsize * $radius * sin( $ea ) );
$coords = "{$xp}, {$yp}";
$a = $ea + 0.25;
if ( $sa < $ea )
{
while ( $a <= 2 * M_PI )
{
$xp = floor( $radius * cos( $a ) + $xc );
$yp = floor( $yc - $radius * sin( $a ) );
$coords .= ", ".$xp.", {$yp}";
$a += 0.25;
}
$a -= 2 * M_PI;
}
while ( $a < $sa )
{
$xp = floor( $this->imidsize * $radius * cos( $a ) + $xc );
$yp = floor( $yc - $this->imidsize * $radius * sin( $a ) );
$coords .= ", ".$xp.", {$yp}";
$a += 0.25;
}
$xp = floor( $this->imidsize * $radius * cos( $sa ) + $xc );
$yp = floor( $yc - $this->imidsize * $radius * sin( $sa ) );
$coords .= ", ".$xp.", {$yp}";
$xp = floor( $radius * cos( $sa ) + $xc );
$yp = floor( $yc - $radius * sin( $sa ) );
$coords .= ", ".$xp.", {$yp}";
$a = $sa - 0.25;
while ( $ea < $a )
{
$xp = floor( $radius * cos( $a ) + $xc );
$yp = floor( $yc - $radius * sin( $a ) );
$coords .= ", ".$xp.", {$yp}";
$a -= 0.25;
}
$xp = floor( $radius * cos( $ea ) + $xc );
$yp = floor( $yc - $radius * sin( $ea ) );
$coords .= ", ".$xp.", {$yp}";
$xp = floor( $this->imidsize * $radius * cos( $ea ) + $xc );
$yp = floor( $yc - $this->imidsize * $radius * sin( $ea ) );
$coords .= ", ".$xp.", {$yp}";
if ( !empty( $this->csimtargets[$i] ) )
{
$this->csimareas .= "<area shape=\"poly\" coords=\"".$coords."\" href=\"".$this->csimtargets[$i]."\"";
if ( !empty( $this->csimalts[$i] ) )
{
$tmp = sprintf( $this->csimalts[$i], $this->data[$i] );
$this->csimareas .= " title=\"".$tmp."\"";
}
$this->csimareas .= " alt=\"".$tmp."\" />\n";
}
}
public function Stroke( $img, $aaoption = 0 )
{
$tmp = $this->value->show;
$this->value->show = false;
parent::stroke( $img, $aaoption );
$this->value->show = $tmp;
$xc = round( $this->posx * $img->width );
$yc = round( $this->posy * $img->height );
$radius = floor( $this->radius * min( $img->width, $img->height ) );
if ( 0 < $this->imidsize && $aaoption !== 2 )
{
if ( $this->ishadowcolor != "" )
{
$img->SetColor( $this->ishadowcolor );
$img->FilledCircle( $xc + $this->ishadowdrop, $yc + $this->ishadowdrop, round( $radius * $this->imidsize ) );
}
$img->SetColor( $this->imidcolor );
$img->FilledCircle( $xc, $yc, round( $radius * $this->imidsize ) );
if ( $this->pie_border && $aaoption === 0 )
{
$img->SetColor( $this->color );
$img->Circle( $xc, $yc, round( $radius * $this->imidsize ) );
}
if ( !empty( $this->middlecsimtarget ) )
{
$this->AddMiddleCSIM( $xc, $yc, round( $radius * $this->imidsize ) );
}
}
if ( $this->value->show && $aaoption !== 1 )
{
$this->StrokeAllLabels( $img, $xc, $yc, $radius );
$this->midtitle->SetPos( $xc, $yc, "center", "center" );
$this->midtitle->Stroke( $img );
}
}
public function AddMiddleCSIM( $xc, $yc, $r )
{
$xc = round( $xc );
$yc = round( $yc );
$r = round( $r );
$this->csimareas .= "<area shape=\"circle\" coords=\"".$xc.",{$yc},{$r}\" href=\"".$this->middlecsimtarget."\"";
if ( !empty( $this->middlecsimalt ) )
{
$tmp = $this->middlecsimalt;
$this->csimareas .= " title=\"".$tmp."\"";
}
$this->csimareas .= " alt=\"".$tmp."\" />\n";
}
public function StrokeLabel( $label, $img, $xc, $yc, $a, $r )
{
if ( $this->ilabelposadj === "auto" )
{
$this->ilabelposadj = ( 1 - $this->imidsize ) / 2 + $this->imidsize;
}
parent::strokelabel( $label, $img, $xc, $yc, $a, $r );
}
}
define( "PIE_VALUE_ABS", 1 );
define( "PIE_VALUE_PER", 0 );
define( "PIE_VALUE_PERCENTAGE", 0 );
define( "PIE_VALUE_ADJPERCENTAGE", 2 );
define( "PIE_VALUE_ADJPER", 2 );
class PieGraph extends Graph
{
private $posx = NULL;
private $posy = NULL;
private $radius = NULL;
private $legends = array( );
public $plots = array( );
public $pieaa = false;
public function PieGraph( $width = 300, $height = 200, $cachedName = "", $timeout = 0, $inline = 1 )
{
$this->Graph( $width, $height, $cachedName, $timeout, $inline );
$this->posx = $width / 2;
$this->posy = $height / 2;
$this->SetColor( array( 255, 255, 255 ) );
}
public function Add( $aObj )
{
if ( is_array( $aObj ) && 0 < count( $aObj ) )
{
$cl = $aObj[0];
}
else
{
$cl = $aObj;
}
if ( $cl instanceof Text )
{
$this->AddText( $aObj );
}
else if ( class_exists( "IconPlot", false ) && $cl instanceof IconPlot )
{
$this->AddIcon( $aObj );
}
else if ( is_array( $aObj ) )
{
$n = count( $aObj );
$i = 0;
for ( ; do
{
$i < $n; ++$i, )
{
$this->plots[] = $aObj[$i];
break;
}
} while ( 1 );
}
else
{
$this->plots[] = $aObj;
}
}
public function SetAntiAliasing( $aFlg = true )
{
$this->pieaa = $aFlg;
}
public function SetColor( $c )
{
$this->SetMarginColor( $c );
}
public function DisplayCSIMAreas( )
{
$csim = "";
foreach ( $this->plots as $p )
{
$csim .= $p->GetCSIMareas( );
}
if ( preg_match_all( "/area shape=\"(\\w+)\" coords=\"([0-9\\, ]+)\"/", $csim, $coords ) )
{
$this->img->SetColor( $this->csimcolor );
$n = count( $coords[0] );
$i = 0;
for ( ; $i < $n; ++$i )
{
if ( $coords[1][$i] == "poly" )
{
preg_match_all( "/\\s*([0-9]+)\\s*,\\s*([0-9]+)\\s*,*/", $coords[2][$i], $pts );
$this->img->SetStartPoint( $pts[1][count( $pts[0] ) - 1], $pts[2][count( $pts[0] ) - 1] );
$m = count( $pts[0] );
$j = 0;
for ( ; do
{
$j < $m; ++$j, )
{
$this->img->LineTo( $pts[1][$j], $pts[2][$j] );
}
} while ( 1 );
break;
}
else if ( $coords[1][$i] == "rect" )
{
$pts = preg_split( "/,/", $coords[2][$i] );
$this->img->SetStartPoint( $pts[0], $pts[1] );
$this->img->LineTo( $pts[2], $pts[1] );
$this->img->LineTo( $pts[2], $pts[3] );
$this->img->LineTo( $pts[0], $pts[3] );
$this->img->LineTo( $pts[0], $pts[1] );
}
}
}
}
public function Stroke( $aStrokeFileName = "" )
{
$_csim = $aStrokeFileName === _CSIM_SPECIALFILE;
$this->iHasStroked = true;
$n = count( $this->plots );
if ( $this->pieaa )
{
if ( !$_csim )
{
if ( $this->background_image != "" )
{
$this->StrokeFrameBackground( );
}
else
{
$this->StrokeFrame( );
$this->StrokeBackgroundGrad( );
}
}
$w = $this->img->width;
$h = $this->img->height;
$oldimg = $this->img->img;
$this->img->CreateImgCanvas( 2 * $w, 2 * $h );
$this->img->SetColor( $this->margin_color );
$this->img->FilledRectangle( 0, 0, 2 * $w - 1, 2 * $h - 1 );
$ni = count( $this->iIcons );
$i = 0;
for ( ; $i < $ni; ++$i )
{
$this->iIcons[$i]['iScale'] *= 2;
if ( 1 < $this->iIcons[$i]->iX )
{
$this->iIcons[$i]['iX'] *= 2;
}
if ( 1 < $this->iIcons[$i]->iY )
{
$this->iIcons[$i]['iY'] *= 2;
}
}
$this->StrokeIcons( );
$i = 0;
for ( ; $i < $n; ++$i )
{
if ( 1 < $this->plots[$i]->posx )
{
$this->plots[$i]['posx'] *= 2;
}
if ( 1 < $this->plots[$i]->posy )
{
$this->plots[$i]['posy'] *= 2;
}
$this->plots[$i]->Stroke( $this->img, 1 );
if ( 1 < $this->plots[$i]->posx )
{
$this->plots[$i]['posx'] /= 2;
}
if ( 1 < $this->plots[$i]->posy )
{
$this->plots[$i]['posy'] /= 2;
}
}
$indent = $this->doframe ? $this->frame_weight + ( $this->doshadow ? $this->shadow_width : 0 ) : 0;
$indent += $this->framebevel ? $this->framebeveldepth + 1 : 0;
$this->img->CopyCanvasH( $oldimg, $this->img->img, $indent, $indent, $indent, $indent, $w - 2 * $indent, $h - 2 * $indent, 2 * ( $w - $indent ), 2 * ( $h - $indent ) );
$this->img->img = $oldimg;
$this->img->width = $w;
$this->img->height = $h;
$i = 0;
for ( ; do
{
$i < $n; ++$i, )
{
$this->plots[$i]->Stroke( $this->img, 2 );
$this->plots[$i]->Legend( $this );
break;
}
} while ( 1 );
}
else
{
if ( !$_csim )
{
if ( $this->background_image != "" )
{
$this->StrokeFrameBackground( );
}
else
{
$this->StrokeFrame( );
}
}
$this->StrokeIcons( );
$i = 0;
for ( ; $i < $n; ++$i )
{
$this->plots[$i]->Stroke( $this->img );
$this->plots[$i]->Legend( $this );
}
}
$this->legend->Stroke( $this->img );
$this->footer->Stroke( $this->img );
$this->StrokeTitles( );
if ( !$_csim )
{
if ( $this->texts != null )
{
$n = count( $this->texts );
$i = 0;
for ( ; $i < $n; ++$i )
{
$this->texts[$i]->Stroke( $this->img );
}
}
if ( _JPG_DEBUG )
{
$this->DisplayCSIMAreas( );
}
if ( $this->iImgTrans )
{
if ( !class_exists( "ImgTrans", false ) )
{
require_once( "jpgraph_imgtrans.php" );
}
$tform = new ImgTrans( $this->img->img );
$this->img->img = $tform->Skew3D( $this->iImgTransHorizon, $this->iImgTransSkewDist, $this->iImgTransDirection, $this->iImgTransHighQ, $this->iImgTransMinSize, $this->iImgTransFillColor, $this->iImgTransBorder );
}
if ( $aStrokeFileName == _IMG_HANDLER )
{
return $this->img->img;
}
$this->cache->PutAndStream( $this->img, $this->cache_name, $this->inline, $aStrokeFileName );
}
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -