jpgraph_spider.php

来自「eGroupWare is a multi-user, web-based gr」· PHP 代码 · 共 557 行 · 第 1/2 页

PHP
557
字号
	$img->SetLineWeight($this->weight);
	for($i=0; $i<$nbrticks; ++$i) {
	    for($j=0; $j<$nbrpnts; ++$j) {
		$pnts[$j*2]=$grid[$j][$i*2];
		$pnts[$j*2+1]=$grid[$j][$i*2+1];
	    }
	    for($k=0; $k<$nbrpnts; ++$k ){
		$l=($k+1)%$nbrpnts;
		if( $this->type == "solid" )
		    $img->Line($pnts[$k*2],$pnts[$k*2+1],$pnts[$l*2],$pnts[$l*2+1]);
		elseif( $this->type == "dotted" )
		    $img->DashedLine($pnts[$k*2],$pnts[$k*2+1],$pnts[$l*2],$pnts[$l*2+1],1,6);
		elseif( $this->type == "dashed" )
		    $img->DashedLine($pnts[$k*2],$pnts[$k*2+1],$pnts[$l*2],$pnts[$l*2+1],2,4);
		elseif( $this->type == "longdashed" )
		    $img->DashedLine($pnts[$k*2],$pnts[$k*2+1],$pnts[$l*2],$pnts[$l*2+1],8,6);
	    }
	    $pnts=array();
	}
    }
} // Class


//===================================================
// CLASS SpiderPlot
// Description: Plot a spiderplot
//===================================================
class SpiderPlot {
    var $data=array();
    var $fill=false, $fill_color=array(200,170,180);
    var $color=array(0,0,0);
    var $legend="";
    var $weight=1;
//---------------
// CONSTRUCTOR
    function SpiderPlot($data) {
	$this->data = $data;
    }

//---------------
// PUBLIC METHODS	
    function Min() {
	return Min($this->data);
    }
	
    function Max() {
	return Max($this->data);
    }
	
    function SetLegend($legend) {
	$this->legend=$legend;
    }
	
    function SetFill($f=true) {
	$this->fill = $f;
    }
	
    function SetLineWeight($w) {
	$this->weight=$w;
    }
		
    function SetColor($color,$fill_color=array(160,170,180)) {
	$this->color = $color;
	$this->fill_color = $fill_color;
    }
	
    function GetCSIMareas() {
	JpGraphError::Raise("JpGraph Error: Client side image maps not supported for SpiderPlots.");
    }
	
    function Stroke(&$img, $pos, &$scale, $startangle) {
	$nbrpnts = count($this->data);
	$astep=2*M_PI/$nbrpnts;		
	$a=$startangle;
		
	// Rotate each point to the correct axis-angle
	// TODO: Update for LogScale
	for($i=0; $i<$nbrpnts; ++$i) {
	    //$c=$this->data[$i];
	    $cs=$scale->RelTranslate($this->data[$i]);
	    $x=round($cs*cos($a)+$scale->scale_abs[0]);
	    $y=round($pos-$cs*sin($a));
	    /*
	      $c=log10($c);
	      $x=round(($c-$scale->scale[0])*$scale->scale_factor*cos($a)+$scale->scale_abs[0]);
	      $y=round($pos-($c-$scale->scale[0])*$scale->scale_factor*sin($a));		
	    */
	    $pnts[$i*2]=$x;
	    $pnts[$i*2+1]=$y;
	    $a += $astep;
	}
	if( $this->fill ) {
	    $img->SetColor($this->fill_color);
	    $img->FilledPolygon($pnts);
	}
	$img->SetLineWeight($this->weight);
	$img->SetColor($this->color);
	$img->Polygon($pnts);
    }
	
//---------------
// PRIVATE METHODS
    function GetCount() {
	return count($this->data);
    }
	
    function Legend(&$graph) {
	if( $this->legend=="" ) return;
	if( $this->fill )
	    $graph->legend->Add($this->legend,$this->fill_color);
	else
	    $graph->legend->Add($this->legend,$this->color);	
    }
	
} // Class

//===================================================
// CLASS SpiderGraph
// Description: Main container for a spider graph
//===================================================
class SpiderGraph extends Graph {
    var $posx;
    var $posy;
    var $len;		
    var $plots=null, $axis_title=null;
    var $grid,$axis=null;
//---------------
// CONSTRUCTOR
    function SpiderGraph($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->len=min($width,$height)*0.35;
	$this->SetColor(array(255,255,255));
	$this->SetTickDensity(TICKD_NORMAL);
	$this->SetScale("lin");
    }

//---------------
// PUBLIC METHODS
    function SupressTickMarks($f=true) {
	$this->axis->scale->ticks->SupressTickMarks($f);
    }
	
    function SetScale($axtype,$ymin=1,$ymax=1) {
	if( $axtype != "lin" && $axtype != "log" ) {
	    JpGraphError::Raise("Illegal scale for spiderplot ($axtype). Must be \"lin\" or \"log\"");
	}
	if( $axtype=="lin" ) {
	    $this->yscale = & new LinearScale(1,1);
	    $this->yscale->ticks = & new SpiderLinearTicks();
	    $this->yscale->ticks->SupressMinorTickMarks();
	}
	elseif( $axtype=="log" ) {
	    $this->yscale = & new LogScale(1,1);
	    $this->yscale->ticks = & new SpiderLogTicks();
	    //JpGraphError::Raise("JpGraph Error: Logarithmic spider plots are not yet supported");
	}
		
	$this->axis = & new SpiderAxis($this->img,$this->yscale);
	$this->grid = & new SpiderGrid();		
    }

    function SetPlotSize($s) {
	if( $s<0.1 || $s>1 )
	    JpGraphError::Raise("JpGraph Error: Spider Plot size must be between 0.1 and 1. (Your value=$s)");
	$this->len=min($this->img->width,$this->img->height)*$s/2;
    }

    function SetTickDensity($densy=TICKD_NORMAL) {
	$this->ytick_factor=25;		
	switch( $densy ) {
	    case TICKD_DENSE:
		$this->ytick_factor=12;			
	    break;
	    case TICKD_NORMAL:
		$this->ytick_factor=25;			
	    break;
	    case TICKD_SPARSE:
		$this->ytick_factor=40;			
	    break;
	    case TICKD_VERYSPARSE:
		$this->ytick_factor=70;			
	    break;		
	    default:
		JpGraphError::Raise("Unsupported Tick density: $densy");
	}
    }

    function SetCenter($px,$py=0.5) {
	assert($px > 0 && $py > 0 );
	$this->posx=$this->img->width*$px;
	$this->posy=$this->img->height*$py;
    }

    function SetColor($c) {
	$this->SetMarginColor($c);
    }
			
    function SetTitles($title) {
	$this->axis_title = $title;
    }

    function Add(&$splot) {
	$this->plots[]=$splot;
    }
	
    function GetPlotsYMinMax() {
	$min=$this->plots[0]->Min();
	$max=$this->plots[0]->Max();
	foreach( $this->plots as $p ) {
	    $max=max($max,$p->Max());
	    $min=min($min,$p->Min());
	}
	if( $min < 0 ) 
	    JpGraphError::Raise("JpGraph Error: Minimum data $min (Spider plots only makes sence to use when all data points > 0)");
	return array($min,$max);
    }	

    // Stroke the Spider graph
    function Stroke($aStrokeFileName="") {
	// Set Y-scale
	if( !$this->yscale->IsSpecified() && 
	count($this->plots)>0 ) {
	    list($min,$max) = $this->GetPlotsYMinMax();
	    $this->yscale->AutoScale($this->img,0,$max,$this->len/$this->ytick_factor);
	}
	// Set start position end length of scale (in absolute pixels)
	$this->yscale->SetConstants($this->posx,$this->len);
		
	// We need as many axis as there are data points
	$nbrpnts=$this->plots[0]->GetCount();
		
	// If we have no titles just number the axis 1,2,3,...
	if( $this->axis_title==null ) {
	    for($i=0; $i<$nbrpnts; ++$i ) 
		$this->axis_title[$i] = $i+1;
	}
	elseif(count($this->axis_title)<$nbrpnts) 
	    JpGraphError::Raise("JpGraph: Number of titles does not match number of points in plot.");
	for($i=0; $i<count($this->plots); ++$i )
	    if( $nbrpnts != $this->plots[$i]->GetCount() )
		JpGraphError::Raise("JpGraph: Each spider plot must have the same number of data points.");

	$this->StrokeFrame();
	$astep=2*M_PI/$nbrpnts;

	// Prepare legends
	for($i=0; $i<count($this->plots); ++$i)
	    $this->plots[$i]->Legend($this);
	$this->legend->Stroke($this->img);			
		
	// Plot points
	$a=M_PI/2;
	for($i=0; $i<count($this->plots); ++$i )
	    $this->plots[$i]->Stroke($this->img, $this->posy, $this->yscale, $a);
		
	// Draw axis and grid
	for( $i=0,$a=M_PI/2; $i<$nbrpnts; ++$i, $a+=$astep ) {
	    $this->axis->Stroke($this->posy,$a,$grid[$i],$this->axis_title[$i],$i==0);
	}	
	$this->grid->Stroke($this->img,$grid);
	$this->title->Center($this->img->left_margin,$this->img->width-$this->img->right_margin,5);
	$this->title->Stroke($this->img);
		
	// Stroke texts
	if( $this->texts != null )
	    foreach( $this->texts as $t) 
		$t->Stroke($this->img);
	
			
	// Finally output the image
	$this->cache->PutAndStream($this->img,$this->cache_name,$this->inline,$aStrokeFileName);	
    }
} // Class

/* EOF */
?>

⌨️ 快捷键说明

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