📄 jpgraph_pie.php
字号:
$i = 0;
for ( ; $i < $n; ++$i )
{
$result[$i] /= $tmp;
}
}
return $result;
}
public function Stroke( $img, $aaoption = 0 )
{
$expscale = $aaoption === 1 ? 2 : 1;
if ( $this->labeltype == 2 )
{
$this->adjusted_data = $this->AdjPercentage( $this->data );
}
$colors = array_keys( $img->rgb->rgb_table );
sort( &$colors );
$ta = $this->themearr[$this->theme];
$n = count( $this->data );
if ( $this->setslicecolors == null )
{
$numcolors = count( $ta );
}
else
{
$numcolors = count( $this->setslicecolors );
if ( $numcolors < $n )
{
$i = 2 * $numcolors;
while ( $i < $n )
{
$this->setslicecolors = array_merge( $this->setslicecolors, $this->setslicecolors );
$i += $n;
}
$tt = array_slice( $this->setslicecolors, 0, $n % $numcolors );
$this->setslicecolors = array_merge( $this->setslicecolors, $tt );
$this->setslicecolors = array_reverse( $this->setslicecolors );
}
}
$sum = 0;
$i = 0;
for ( ; $i < $n; ++$i )
{
$sum += $this->data[$i];
}
if ( $sum == 0 )
{
JpGraphError::raisel( 15009 );
}
if ( $this->radius <= 1 )
{
$radius = floor( $this->radius * min( $img->width, $img->height ) );
}
else
{
$radius = $aaoption === 1 ? $this->radius * 2 : $this->radius;
}
if ( $this->posx <= 1 && 0 < $this->posx )
{
$xc = round( $this->posx * $img->width );
}
else
{
$xc = $this->posx;
}
if ( $this->posy <= 1 && 0 < $this->posy )
{
$yc = round( $this->posy * $img->height );
}
else
{
$yc = $this->posy;
}
$n = count( $this->data );
if ( $this->explode_all )
{
$i = 0;
for ( ; $i < $n; ++$i )
{
$this->explode_radius[$i] = $this->explode_r;
}
}
if ( $this->ishadowcolor != "" && $aaoption !== 2 )
{
$accsum = 0;
$angle2 = $this->startangle;
$img->SetColor( $this->ishadowcolor );
$i = 0;
for ( ; 0 < $sum && $i < $n; ++$i )
{
$j = $n - $i - 1;
$d = $this->data[$i];
$angle1 = $angle2;
$accsum += $d;
$angle2 = $this->startangle + 2 * M_PI * $accsum / $sum;
if ( empty( $this->explode_radius[$j] ) )
{
$this->explode_radius[$j] = 0;
}
$la = 2 * M_PI - ( abs( $angle2 - $angle1 ) / 2 + $angle1 );
$xcm = $xc + $this->explode_radius[$j] * cos( $la ) * $expscale;
$ycm = $yc - $this->explode_radius[$j] * sin( $la ) * $expscale;
$xcm += $this->ishadowdrop * $expscale;
$ycm += $this->ishadowdrop * $expscale;
$img->CakeSlice( $xcm, $ycm, $radius, $radius, $angle1 * 180 / M_PI, $angle2 * 180 / M_PI, $this->ishadowcolor );
}
}
$accsum = 0;
$angle2 = $this->startangle;
$img->SetColor( $this->color );
$i = 0;
for ( ; 0 < $sum && $i < $n; ++$i )
{
$j = $n - $i - 1;
if ( empty( $this->explode_radius[$j] ) )
{
$this->explode_radius[$j] = 0;
}
$d = $this->data[$i];
$angle1 = $angle2;
$accsum += $d;
$angle2 = $this->startangle + 2 * M_PI * $accsum / $sum;
$this->la[$i] = 2 * M_PI - ( abs( $angle2 - $angle1 ) / 2 + $angle1 );
if ( !( $d == 0 ) )
{
if ( $this->setslicecolors == null )
{
$slicecolor = $colors[$ta[$i % $numcolors]];
}
else
{
$slicecolor = $this->setslicecolors[$i % $numcolors];
}
if ( $this->pie_interior_border && $aaoption === 0 )
{
$img->SetColor( $this->color );
}
else
{
$img->SetColor( $slicecolor );
}
$arccolor = $this->pie_border && $aaoption === 0 ? $this->color : "";
$xcm = $xc + $this->explode_radius[$j] * cos( $this->la[$i] ) * $expscale;
$ycm = $yc - $this->explode_radius[$j] * sin( $this->la[$i] ) * $expscale;
if ( $aaoption !== 2 )
{
$img->CakeSlice( $xcm, $ycm, $radius - 1, $radius - 1, $angle1 * 180 / M_PI, $angle2 * 180 / M_PI, $slicecolor, $arccolor );
}
if ( !$this->csimtargets && !( $aaoption !== 1 ) )
{
$this->AddSliceToCSIM( $i, $xcm, $ycm, $radius, $angle1, $angle2 );
}
}
}
if ( $aaoption !== 2 )
{
$i = 0;
for ( ; $i < $n; ++$i )
{
if ( $this->labeltype == 0 )
{
if ( $sum != 0 )
{
$l = 100 * $this->data[$i] / $sum;
}
else
{
$l = 0;
}
}
else if ( $this->labeltype == 1 )
{
$l = $this->data[$i] * 1;
}
else
{
$l = $this->adjusted_data[$i];
}
if ( isset( $this->labels[$i] ) && is_string( $this->labels[$i] ) )
{
$this->labels[$i] = sprintf( $this->labels[$i], $l );
}
else
{
$this->labels[$i] = $l;
}
}
}
if ( $this->value->show && $aaoption !== 1 )
{
$this->StrokeAllLabels( $img, $xc, $yc, $radius );
}
if ( $aaoption !== 1 )
{
$this->title->SetPos( $xc, $yc - $this->title->GetFontHeight( $img ) - $radius - $this->title->margin, "center", "bottom" );
$this->title->Stroke( $img );
}
}
public function NormAngle( $a )
{
while ( $a < 0 )
{
$a += 2 * M_PI;
}
while ( 2 * M_PI < $a )
{
$a -= 2 * M_PI;
}
return $a;
}
public function Quadrant( $a )
{
$a = $this->NormAngle( $a );
if ( 0 < $a && $a <= M_PI / 2 )
{
return 0;
}
if ( M_PI / 2 < $a && $a <= M_PI )
{
return 1;
}
if ( M_PI < $a && $a <= 1.5 * M_PI )
{
return 2;
}
if ( 1.5 * M_PI < $a )
{
return 3;
}
}
public function StrokeGuideLabels( $img, $xc, $yc, $radius )
{
$n = count( $this->labels );
$tresh_hold = 25 * M_PI / 180;
$incluster = false;
$clusters = array( );
$cidx = -1;
$i = 0;
for ( ; $i < $n - 1; ++$i )
{
$a1 = $this->la[$i];
$a2 = $this->la[$i + 1];
$q1 = $this->Quadrant( $a1 );
$q2 = $this->Quadrant( $a2 );
$diff = abs( $a1 - $a2 );
if ( $diff < $tresh_hold )
{
if ( $incluster )
{
++$clusters[$cidx][1];
if ( $q1 != $q2 )
{
if ( $q1 == 1 && $q2 == 0 && 75 * M_PI / 180 < $a2 )
{
if ( $i < $n - 2 )
{
$a3 = $this->la[$i + 2];
if ( $tresh_hold <= abs( $a3 - $a2 ) )
{
++$clusters[$cidx][1];
++$i;
}
}
}
else if ( $q1 == 3 && $q2 == 2 && 255 * M_PI / 180 < $a2 && $i < $n - 2 )
{
$a3 = $this->la[$i + 2];
if ( $tresh_hold <= abs( $a3 - $a2 ) )
{
++$clusters[$cidx][1];
++$i;
}
}
if ( $q1 == 2 && $q2 == 1 && 165 * M_PI / 180 < $a2 )
{
++$clusters[$cidx][1];
++$i;
}
$incluster = false;
}
}
else if ( $q1 == $q2 )
{
$incluster = true;
if ( $q1 == 0 && -1 < $cidx && $clusters[$cidx][1] == 1 && $this->Quadrant( $this->la[$clusters[$cidx][0]] ) == 0 )
{
++$clusters[$cidx][1];
}
else
{
++$cidx;
$clusters[$cidx][0] = $i;
$clusters[$cidx][1] = 1;
}
}
else
{
++$cidx;
$clusters[$cidx][0] = $i;
$clusters[$cidx][1] = 1;
}
}
else if ( $incluster )
{
++$clusters[$cidx][1];
$incluster = false;
}
else
{
++$cidx;
$clusters[$cidx][0] = $i;
$clusters[$cidx][1] = 1;
}
}
if ( $incluster )
{
++$clusters[$cidx][1];
}
else
{
++$cidx;
$clusters[$cidx][0] = $i;
$clusters[$cidx][1] = 1;
}
$this->value->ApplyFont( $img );
$fh = $img->GetFontHeight( );
$origvstep = $fh * $this->iGuideVFactor;
$this->value->SetMargin( 0 );
$nc = count( $clusters );
$i = 0;
for ( ; $i < $nc; ++$i )
{
$csize = $clusters[$i][1];
$a = $this->la[$clusters[$i][0]];
$q = $this->Quadrant( $a );
if ( $q == 0 )
{
$start = $csize - 1;
$idx = $start;
$step = -1;
$vstep = 0 - $origvstep;
}
else if ( $q == 1 )
{
$start = 0;
$idx = $start;
$step = 1;
$vstep = 0 - $origvstep;
}
else if ( $q == 2 )
{
$start = $csize - 1;
$idx = $start;
$step = -1;
$vstep = $origvstep;
}
else if ( $q == 3 )
{
$start = 0;
$idx = $start;
$step = 1;
$vstep = $origvstep;
}
$j = 0;
for ( ; $j < $csize; ++$j )
{
$a = $this->la[$clusters[$i][0] + $idx];
$r = $radius + $this->explode_radius[$n - 1 - ( $clusters[$i][0] + $idx )];
$x = round( $r * cos( $a ) + $xc );
$y = round( $yc - $r * sin( $a ) );
$r += $fh * $this->iGuideLineRFactor;
if ( $this->iGuideLineCurve )
{
$xt = round( $r * cos( $a ) + $xc );
}
if ( $idx == $start )
{
if ( !$this->iGuideLineCurve )
{
$xt = round( $r * cos( $a ) + $xc );
}
$yt = round( $yc - $r * sin( $a ) );
$prevcluster = ( $i + ( $nc - 1 ) ) % $nc;
$previdx = $clusters[$prevcluster][0] + $clusters[$prevcluster][1] - 1;
if ( $q == 1 && 160 * M_PI / 180 < $a )
{
$diff = abs( $a - $this->la[$previdx] );
if ( $diff < $tresh_hold )
{
$yt -= $fh;
}
}
else if ( $q == 3 && 340 * M_PI / 180 < $a )
{
$diff = abs( $a - $this->la[$previdx] - 360 * M_PI / 180 );
if ( $diff < $tresh_hold )
{
$yt += $fh;
}
}
}
else
{
$prev_a = $this->la[$clusters[$i][0] + ( $idx - $step )];
$dy = abs( $radius * ( sin( $a ) - sin( $prev_a ) ) * 1.2 );
if ( 0 < $vstep )
{
$yt += max( $vstep, $dy );
}
else
{
$yt += min( $vstep, 0 - $dy );
}
}
$label = $this->labels[$clusters[$i][0] + $idx];
if ( $csize == 1 )
{
$r = $radius + $this->explode_radius[$n - 1 - ( $clusters[$i][0] + $idx )];
$rr = $r + $img->GetFontHeight( ) / 2;
$xt = round( $rr * cos( $a ) + $xc );
$yt = round( $yc - $rr * sin( $a ) );
$this->StrokeLabel( $label, $img, $xc, $yc, $a, $r );
if ( $this->iShowGuideLineForSingle )
{
$this->guideline->Stroke( $img, $x, $y, $xt, $yt );
}
}
else
{
$this->guideline->Stroke( $img, $x, $y, $xt, $yt );
if ( $q == 1 || $q == 2 )
{
$this->guideline->Stroke( $img, $xt, $yt, $xt - $this->guidelinemargin, $yt );
$lbladj = 0 - $this->guidelinemargin - 5;
$this->value->halign = "right";
$this->value->valign = "center";
}
else
{
$this->guideline->Stroke( $img, $xt, $yt, $xt + $this->guidelinemargin, $yt );
$lbladj = $this->guidelinemargin + 5;
$this->value->halign = "left";
$this->value->valign = "center";
}
$this->value->Stroke( $img, $label, $xt + $lbladj, $yt );
}
$idx += $step;
}
}
}
public function StrokeAllLabels( $img, $xc, $yc, $radius )
{
$n = count( $this->la );
$i = 0;
for ( ; $i < $n; ++$i )
{
$this->la[$i] = $this->NormAngle( $this->la[$i] );
}
if ( $this->guideline->iShow )
{
$this->StrokeGuideLabels( $img, $xc, $yc, $radius );
}
else
{
$n = count( $this->labels );
$i = 0;
for ( ; $i < $n; ++$i )
{
$this->StrokeLabel( $this->labels[$i], $img, $xc, $yc, $this->la[$i], $radius + $this->explode_radius[$n - 1 - $i] );
}
}
}
public function StrokeLabel( $label, $img, $xc, $yc, $a, $r )
{
if ( $this->ilabelposadj === "auto" )
{
$this->ilabelposadj = 0.65;
}
if ( $this->ilabelposadj < 1 )
{
$this->value->SetAlign( "center", "center" );
$this->value->margin = 0;
$xt = round( $this->ilabelposadj * $r * cos( $a ) + $xc );
$yt = round( $yc - $this->ilabelposadj * $r * sin( $a ) );
$this->value->Stroke( $img, $label, $xt, $yt );
}
else
{
$this->value->halign = "left";
$this->value->valign = "top";
$this->value->margin = 0;
$this->value->ApplyFont( $img );
$h = $img->GetTextHeight( $label );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -