📄 jpgraph_pie.php
字号:
<?php/*=======================================================================// File: JPGRAPH_PIE.PHP// Description: Pie plot extension for JpGraph// Created: 2001-02-14// Author: Johan Persson (johanp@aditus.nu)// Ver: $Id: jpgraph_pie.php 575 2006-03-04 11:04:59Z ljp $//// Copyright (c) Aditus Consulting. All rights reserved.//========================================================================*/// Defines for PiePlot::SetLabelType()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 PiePlot// Description: Draws a pie plot//===================================================class PiePlot { public $posx=0.5,$posy=0.5; protected $radius=0.3; protected $explode_radius=array(),$explode_all=false,$explode_r=20; protected $labels=null, $legends=null; protected $csimtargets=null; // Array of targets for CSIM protected $csimareas=''; // Generated CSIM text protected $csimalts=null; // ALT tags for corresponding target protected $data=null; public $title; protected $startangle=0; protected $weight=1, $color="black"; protected $legend_margin=6,$show_labels=true; protected $themearr = array( "td" => array(136,22,40,45,49,74,119,149,430,105,333,4,141,168,180,209,218,395,89), "earth" => array(136,34,40,45,46,62,63,134,74,10,120,136,141,168,180,77,209,218,346,395,89,430), "pastel" => array(27,415,128,59,66,79,105,110,42,147,152,230,236,240,331,337,405,38), "water" => array(8,370,24,40,335,56,213,237,268,14,326,387,10,388), "sand" => array(27,168,34,170,19,50,65,72,131,209,46,393)); protected $theme="earth"; protected $setslicecolors=array(); protected $labeltype=0; // Default to percentage protected $pie_border=true,$pie_interior_border=true; public $value; protected $ishadowcolor='',$ishadowdrop=4; protected $ilabelposadj=1; protected $legendcsimtargets = array(); protected $legendcsimalts = array(); protected $adjusted_data = array(); public $guideline = null; protected $guidelinemargin=10,$iShowGuideLineForSingle = false; protected $iGuideLineCurve = false,$iGuideVFactor=1.4,$iGuideLineRFactor=0.8; //---------------// CONSTRUCTOR function PiePlot($data) { $this->data = array_reverse($data); $this->title = new Text(""); $this->title->SetFont(FF_FONT1,FS_BOLD); $this->value = new DisplayValue(); $this->value->Show(); $this->value->SetFormat('%.1f%%'); $this->guideline = new LineProperty(); }//---------------// PUBLIC METHODS function SetCenter($x,$y=0.5) { $this->posx = $x; $this->posy = $y; } // Enable guideline and set drwaing policy function SetGuideLines($aFlg=true,$aCurved=true,$aAlways=false) { $this->guideline->Show($aFlg); $this->iShowGuideLineForSingle = $aAlways; $this->iGuideLineCurve = $aCurved; } // Adjuste the distance between labels and labels and pie function SetGuideLinesAdjust($aVFactor,$aRFactor=0.8) { $this->iGuideVFactor=$aVFactor; $this->iGuideLineRFactor=$aRFactor; } function SetColor($aColor) { $this->color = $aColor; } function SetSliceColors($aColors) { $this->setslicecolors = $aColors; } function SetShadow($aColor='darkgray',$aDropWidth=4) { $this->ishadowcolor = $aColor; $this->ishadowdrop = $aDropWidth; } function SetCSIMTargets($targets,$alts=null) { $this->csimtargets=array_reverse($targets); if( is_array($alts) ) $this->csimalts=array_reverse($alts); } function GetCSIMareas() { return $this->csimareas; } function AddSliceToCSIM($i,$xc,$yc,$radius,$sa,$ea) { //Slice number, ellipse centre (x,y), height, width, start angle, end angle while( $sa > 2*M_PI ) $sa = $sa - 2*M_PI; while( $ea > 2*M_PI ) $ea = $ea - 2*M_PI; $sa = 2*M_PI - $sa; $ea = 2*M_PI - $ea; // Special case when we have only one slice since then both start and end // angle will be == 0 if( abs($sa - $ea) < 0.0001 ) { $sa=2*M_PI; $ea=0; } //add coordinates of the centre to the map $xc = floor($xc);$yc=floor($yc); $coords = "$xc, $yc"; //add coordinates of the first point on the arc to the map $xp = floor(($radius*cos($ea))+$xc); $yp = floor($yc-$radius*sin($ea)); $coords.= ", $xp, $yp"; //add coordinates every 0.2 radians $a=$ea+0.2; // If we cross the 360-limit with a slice we need to handle // the fact that end angle is smaller than start 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.2; } $a -= 2*M_PI; } while ($a < $sa) { $xp = floor($radius*cos($a)+$xc); $yp = floor($yc-$radius*sin($a)); $coords.= ", $xp, $yp"; $a += 0.2; } //Add the last point on the arc $xp = floor($radius*cos($sa)+$xc); $yp = floor($yc-$radius*sin($sa)); $coords.= ", $xp, $yp"; if( !empty($this->csimtargets[$i]) ) { $this->csimareas .= "<area shape=\"poly\" coords=\"$coords\" href=\"".$this->csimtargets[$i]."\""; $tmp=""; if( !empty($this->csimalts[$i]) ) { $tmp=sprintf($this->csimalts[$i],$this->data[$i]); $this->csimareas .= " title=\"$tmp\""; } $this->csimareas .= " alt=\"$tmp\" />\n"; } } function SetTheme($aTheme) { if( in_array($aTheme,array_keys($this->themearr)) ) $this->theme = $aTheme; else JpGraphError::RaiseL(15001,$aTheme);//("PiePLot::SetTheme() Unknown theme: $aTheme"); } function ExplodeSlice($e,$radius=20) { if( ! is_integer($e) ) JpGraphError::RaiseL(15002);//('Argument to PiePlot::ExplodeSlice() must be an integer'); $this->explode_radius[$e]=$radius; } function ExplodeAll($radius=20) { $this->explode_all=true; $this->explode_r = $radius; } function Explode($aExplodeArr) { if( !is_array($aExplodeArr) ) { JpGraphError::RaiseL(15003);//("Argument to PiePlot::Explode() must be an array with integer distances."); } $this->explode_radius = $aExplodeArr; } function SetStartAngle($aStart) { if( $aStart < 0 || $aStart > 360 ) { JpGraphError::RaiseL(15004);//('Slice start angle must be between 0 and 360 degrees.'); } $this->startangle = 360-$aStart; $this->startangle *= M_PI/180; } function SetFont($family,$style=FS_NORMAL,$size=10) { JpGraphError::RaiseL(15005);//('PiePlot::SetFont() is deprecated. Use PiePlot->value->SetFont() instead.'); } // Size in percentage function SetSize($aSize) { if( ($aSize>0 && $aSize<=0.5) || ($aSize>10 && $aSize<1000) ) $this->radius = $aSize; else JpGraphError::RaiseL(15006);//("PiePlot::SetSize() Radius for pie must either be specified as a fraction [0, 0.5] of the size of the image or as an absolute size in pixels in the range [10, 1000]"); } function SetFontColor($aColor) { JpGraphError::RaiseL(15007);//('PiePlot::SetFontColor() is deprecated. Use PiePlot->value->SetColor() instead.'); } // Set label arrays function SetLegends($aLegend) { $this->legends = $aLegend; } // Set text labels for slices function SetLabels($aLabels,$aLblPosAdj="auto") { $this->labels = array_reverse($aLabels); $this->ilabelposadj=$aLblPosAdj; } function SetLabelPos($aLblPosAdj) { $this->ilabelposadj=$aLblPosAdj; } // Should we display actual value or percentage? function SetLabelType($t) { if( $t < 0 || $t > 2 ) JpGraphError::RaiseL(15008,$t);//("PiePlot::SetLabelType() Type for pie plots must be 0 or 1 (not $t)."); $this->labeltype=$t; } // Deprecated. function SetValueType($aType) { $this->SetLabelType($aType); } // Should the circle around a pie plot be displayed function ShowBorder($exterior=true,$interior=true) { $this->pie_border = $exterior; $this->pie_interior_border = $interior; } // Setup the legends function Legend($graph) { $colors = array_keys($graph->img->rgb->rgb_table); sort($colors); $ta=$this->themearr[$this->theme]; $n = count($this->data); if( $this->setslicecolors==null ) { $numcolors=count($ta); if( class_exists('PiePlot3D',false) && ($this instanceof PiePlot3D) ) { $ta = array_reverse(array_slice($ta,0,$n)); } } else { $this->setslicecolors = array_slice($this->setslicecolors,0,$n); $numcolors=count($this->setslicecolors); if( $graph->pieaa && ($this instanceof PiePlot) ) { $this->setslicecolors = array_reverse($this->setslicecolors); } } $sum=0; for($i=0; $i < $n; ++$i) $sum += $this->data[$i]; // Bail out with error if the sum is 0 if( $sum==0 ) JpGraphError::RaiseL(15009);//("Illegal pie plot. Sum of all data is zero for Pie!"); // Make sure we don't plot more values than data points // (in case the user added more legends than data points) $n = min(count($this->legends),count($this->data)); if( $this->legends != "" ) { $this->legends = array_reverse(array_slice($this->legends,0,$n)); } for( $i=$n-1; $i >= 0; --$i ) { $l = $this->legends[$i]; // Replace possible format with actual values if( count($this->csimalts) > $i ) { $fmt = $this->csimalts[$i]; } else { $fmt = "%d"; // Deafult Alt if no other has been specified } if( $this->labeltype==0 ) { $l = sprintf($l,100*$this->data[$i]/$sum); $alt = sprintf($fmt,$this->data[$i]); } elseif( $this->labeltype == 1) { $l = sprintf($l,$this->data[$i]); $alt = sprintf($fmt,$this->data[$i]); } else { $l = sprintf($l,$this->adjusted_data[$i]); $alt = sprintf($fmt,$this->adjusted_data[$i]); } if( $this->setslicecolors==null ) { $graph->legend->Add($l." ",$colors[$ta[$i%$numcolors]],"",0,$this->csimtargets[$i],$alt); } else { $graph->legend->Add($l,$this->setslicecolors[$i%$numcolors],"",0,$this->csimtargets[$i],$alt); } } } // Adjust the rounded percetage value so that the sum of // of the pie slices are always 100% // Using the Hare/Niemeyer method function AdjPercentage($aData,$aPrec=0) { $mul=100; if( $aPrec > 0 && $aPrec < 3 ) { if( $aPrec == 1 ) $mul=1000; else $mul=10000; } $tmp = array(); $result = array(); $quote_sum=0; $n = count($aData) ; for( $i=0, $sum=0; $i < $n; ++$i ) $sum+=$aData[$i]; foreach($aData as $index => $value) { $tmp_percentage=$value/$sum*$mul; $result[$index]=floor($tmp_percentage); $tmp[$index]=$tmp_percentage-$result[$index]; $quote_sum+=$result[$index]; } if( $quote_sum == $mul) { if( $mul > 100 ) { $tmp = $mul / 100; for( $i=0; $i < $n; ++$i ) { $result[$i] /= $tmp ; } } return $result; } arsort($tmp,SORT_NUMERIC); reset($tmp); for($i=0; $i < $mul-$quote_sum; $i++) { $result[key($tmp)]++; next($tmp); } if( $mul > 100 ) { $tmp = $mul / 100; for( $i=0; $i < $n; ++$i ) { $result[$i] /= $tmp ; } } return $result; } function Stroke($img,$aaoption=0) { // aaoption is used to handle antialias // aaoption == 0 a normal pie // aaoption == 1 just the body // aaoption == 2 just the values // Explode scaling. If anti anti alias we scale the image // twice and we also need to scale the exploding distance $expscale = $aaoption === 1 ? 2 : 1; if( $this->labeltype == 2 ) { // Adjust the data so that it will add up to 100% $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 { // We need to create an array of colors as long as the data // since we need to reverse it to get the colors in the right order $numcolors=count($this->setslicecolors); if( $n > $numcolors ) { $i = 2*$numcolors; while( $n > $i ) { $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); } } // Draw the slices $sum=0; for($i=0; $i < $n; ++$i) $sum += $this->data[$i]; // Bail out with error if the sum is 0 if( $sum==0 ) JpGraphError::RaiseL(15009);//("Sum of all data is 0 for Pie."); // Set up the pie-circle 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 && $this->posx > 0 ) $xc = round($this->posx*$img->width); else $xc = $this->posx ; if( $this->posy <= 1 && $this->posy > 0 ) $yc = round($this->posy*$img->height); else $yc = $this->posy ; $n = count($this->data); if( $this->explode_all ) for($i=0; $i < $n; ++$i) $this->explode_radius[$i]=$this->explode_r; if( $this->ishadowcolor != "" && $aaoption !== 2) { $accsum=0; $angle2 = $this->startangle; $img->SetColor($this->ishadowcolor);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -