⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jpgraph_pie.php

📁 jsp程序开发系统
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?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,v 1.5 2003/10/10 03:37:56 wwf Exp $//// License:	This code is released under QPL// Copyright (C) 2001,2002 Johan Persson//========================================================================*/// Defines for PiePlot::SetLabelType()DEFINE("PIE_VALUE_ABS",1);DEFINE("PIE_VALUE_PER",0);DEFINE("PIE_VALUE_PERCENTAGE",0);//===================================================// CLASS PiePlot// Description: Draws a pie plot//===================================================class PiePlot {    var $posx=0.5,$posy=0.5;    var $radius=0.3;    var $explode_radius=array(),$explode_all=false,$explode_r=20;    var $labels=null, $legends=null;    var $csimtargets=null;  // Array of targets for CSIM    var $csimareas='';		// Generated CSIM text	    var $csimalts=null;		// ALT tags for corresponding target    var $data=null;    var $title;    var $startangle=0;    var $weight=1, $color="black";    var $legend_margin=6,$show_labels=true;    var $themearr = array(	"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));    var $theme="earth";    var $setslicecolors=array();    var $labeltype=0; // Default to percentage    var $pie_border=true,$pie_interior_border=true;    var $value;    var $ishadowcolor='',$ishadowdrop=4;    var $ilabelposadj=1;    var $legendcsimtargets = array();    var $legendcsimalts = array();	//---------------// 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%%');    }//---------------// PUBLIC METHODS	    function SetCenter($x,$y=0.5) {	$this->posx = $x;	$this->posy = $y;    }    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;	//add coordinates of the centre to the map	$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;	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]."\"";	if( !empty($this->csimalts[$i]) ) {	    $tmp=sprintf($this->csimalts[$i],$this->data[$i]);	    $this->csimareas .= " alt=\"$tmp\" title=\"$tmp\"";	}	$this->csimareas .= ">\n";    }	    function SetTheme($aTheme) {	if( in_array($aTheme,array_keys($this->themearr)) )	    $this->theme = $aTheme;	else	    JpGraphError::Raise("PiePLot::SetTheme() Unknown theme: $aTheme");    }	    function ExplodeSlice($e,$radius=20) {	$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::Raise("Argument to PiePlot::Explode() must be an array.");	}	$this->explode_radius = $aExplodeArr;    }    function SetStartAngle($aStart) {	if( $aStart < 0 || $aStart > 360 ) {	    JpGraphError::Raise('Slice start angle must be between 0 and 360 degrees.');	}	$this->startangle = 360-$aStart;    }	    function SetFont($family,$style=FS_NORMAL,$size=10) {		JpGraphError::Raise('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::Raise("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::Raise('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>1 ) 	    JpGraphError::Raise("PiePlot::SetLabelType() Type for pie plots must be 0 or 1 (not $t).");	$this->labeltype=$t;    }    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);   	else {	    $this->setslicecolors = array_slice($this->setslicecolors,0,$n);	    $numcolors=$n; 	    if( $graph->pieaa && get_class($this)==='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::Raise("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($this->legends);	for( $i=$n-1; $i >= 0; --$i ) {	    $l = $this->legends[$i];	    // Replace possible format with actual values	    if( $this->labeltype==0 ) {		$l = sprintf($l,100*$this->data[$i]/$sum);	    }	    else {		$l = sprintf($l,$this->data[$i]);	    }	    	    $alt = sprintf($this->csimalts[$i],$this->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);	}    }	    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;	$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 {	    $this->setslicecolors = array_reverse(array_slice($this->setslicecolors,0,$n));	    $numcolors=$n; 	}   		// 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::Raise("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 ;			$this->startangle = $this->startangle*M_PI/180;	$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);	    for($i=0; $sum>0 && $i < $n; ++$i) {		$d = $this->data[$i];		$angle1 = $angle2;		$accsum += $d;		$angle2 = $this->startangle+2*M_PI*$accsum/$sum;		if( empty($this->explode_radius[$i]) )		    $this->explode_radius[$i]=0;		$la = 2*M_PI - (abs($angle2-$angle1)/2.0+$angle1);		$xcm = $xc + $this->explode_radius[$i]*cos($la)*$expscale;		$ycm = $yc - $this->explode_radius[$i]*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);	for($i=0; $sum>0 && $i < $n; ++$i) {	    $d = $this->data[$i];	    $angle1 = $angle2;	    $accsum += $d;	    $angle2 = $this->startangle+2*M_PI*$accsum/$sum;	    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 : "";	    $this->la[$i] = 2*M_PI - (abs($angle2-$angle1)/2.0+$angle1);	    if( empty($this->explode_radius[$i]) )		$this->explode_radius[$i]=0;	    $xcm = $xc + $this->explode_radius[$i]*cos($this->la[$i])*$expscale;	    $ycm = $yc - $this->explode_radius[$i]*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( $this->value->show && $aaoption !== 1 ) {	    // Format the titles for each slice	    for( $i=0; $i < $n; ++$i) {		if( $this->labeltype==0 )		    if( $sum != 0 )			$l = 100.0*$this->data[$i]/$sum;		    else			$l = 0.0;		else		    $l = $this->data[$i]*1.0;		if( isset($this->labels[$i]) && is_string($this->labels[$i]) )		    $this->labels[$i]=sprintf($this->labels[$i],$l);		else		    $this->labels[$i]=$l;	    }	    $this->StrokeAllLabels($img,$xc,$yc,$radius);	}	// Adjust title position	if( $aaoption !== 1 ) {	    $this->title->Pos($xc,			  $yc-$this->title->GetFontHeight($img)-$radius-$this->title->margin,			  "center","bottom");	    $this->title->Stroke($img);	}    }//---------------// PRIVATE METHODS	    function StrokeAllLabels($img,$xc,$yc,$radius) {	$n = count($this->labels);	for($i=0; $i < $n; ++$i) {	    $this->StrokeLabel($this->labels[$i],$img,$xc,$yc,$this->la[$i],			       $radius + $this->explode_radius[$i]); 	}    }    // Position the labels of each slice    function StrokeLabel($label,$img,$xc,$yc,$a,$r) {	// Default value

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -