jpgraph_spider.php

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

PHP
557
字号
<?php
/*=======================================================================
// File:	JPGRAPH_SPIDER.PHP
// Description: Spider plot extension for JpGraph
// Created: 	2001-02-04
// Author:	Johan Persson (johanp@aditus.nu)
// Ver:		$Id: jpgraph_spider.php,v 1.5 2004/06/01 10:38:37 lkneschke Exp $
//
// License:	This code is released under GPL 2.0
// Copyright (C) 2001 Johan Persson
//========================================================================
*/

//===================================================
// CLASS FontProp
// Description: Utility class to enable the use
// of a "title" instance variable for the spider axis.
// This clas is only used to hold a font and a color
// property for the axis title.
//===================================================
class FontProp {
    var $font_family=FF_FONT1, $font_style=FS_NORMAL,$font_size=14,$font_color=array(0,0,0);	
    function SetFont($family,$style=FS_NORMAL,$size=14) {
	$this->font_family = $family;
	$this->font_style = $style;
	$this->font_size = $size;
    }
	
    function SetColor($c) {
	$this->font_color = $c;
    }
}

class SpiderLogTicks extends Ticks {
//---------------
// CONSTRUCTOR
    function SpiderLogTicks() {
    }
//---------------
// PUBLIC METHODS	

    // TODO: Add Argument grid
    function Stroke(&$aImg,&$grid,$aPos,$aAxisAngle,&$aScale,&$aMajPos,&$aMajLabel) {
	$start = $aScale->GetMinVal();
	$limit = $aScale->GetMaxVal();
	$nextMajor = 10*$start;
	$step = $nextMajor / 10.0;
	$count=1; 
				
	$ticklen_maj=5;
	$dx_maj=round(sin($aAxisAngle)*$ticklen_maj);
	$dy_maj=round(cos($aAxisAngle)*$ticklen_maj);
	$ticklen_min=3;
	$dx_min=round(sin($aAxisAngle)*$ticklen_min);
	$dy_min=round(cos($aAxisAngle)*$ticklen_min);
			
	$aMajPos=array();
	$aMajLabel=array();
			
	if( $this->supress_first )
	    $aMajLabel[]="";
	else
	    $aMajLabel[]=$start;	
	$yr=$aScale->RelTranslate($start);	
	$xt=round($yr*cos($aAxisAngle))+$aScale->scale_abs[0];
	$yt=$aPos-round($yr*sin($aAxisAngle));
	$aMajPos[]=$xt+2*$dx_maj;
	$aMajPos[]=$yt-$aImg->GetFontheight()/2;
	$grid[]=$xt;
	$grid[]=$yt;

	$aImg->SetLineWeight($this->weight);			
		
	for($y=$start; $y<=$limit; $y+=$step,++$count  ) {
	    $yr=$aScale->RelTranslate($y);	
	    $xt=round($yr*cos($aAxisAngle))+$aScale->scale_abs[0];
	    $yt=$aPos-round($yr*sin($aAxisAngle));
	    if( $count % 10 == 0 ) {
		$grid[]=$xt;
		$grid[]=$yt;
		$aMajPos[]=$xt+2*$dx_maj;
		$aMajPos[]=$yt-$aImg->GetFontheight()/2;							
		if( !$this->supress_tickmarks )	{		
		    if( $this->majcolor!="" ) $aImg->PushColor($this->majcolor);
		    $aImg->Line($xt+$dx_maj,$yt+$dy_maj,$xt-$dx_maj,$yt-$dy_maj);
		    if( $this->majcolor!="" ) $aImg->PopColor();
		}
		$aMajLabel[]=$nextMajor;	
		$nextMajor *= 10;
		$step *= 10;	
		$count=1; 				
	    }
	    else
		if( !$this->supress_minor_tickmarks )	{
		    if( $this->mincolor!="" ) $aImg->PushColor($this->mincolor);
		    $aImg->Line($xt+$dx_min,$yt+$dy_min,$xt-$dx_min,$yt-$dy_min);
		    if( $this->mincolor!="" ) $aImg->PopColor();
		}
	}		
    }		
}
	
class SpiderLinearTicks extends LinearTicks {
//---------------
// CONSTRUCTOR
    function SpiderLinearTicks() {
	// Empty
    }

//---------------
// PUBLIC METHODS	

    // TODO: Add argument grid
    function Stroke(&$aImg,&$grid,$aPos,$aAxisAngle,&$aScale,&$aMajPos,&$aMajLabel) {
	// Prepare to draw linear ticks
	$maj_step_abs = abs($aScale->scale_factor*$this->major_step);	
	$min_step_abs = abs($aScale->scale_factor*$this->minor_step);	
	$nbrmaj = floor(($aScale->world_abs_size)/$maj_step_abs);
	$nbrmin = floor(($aScale->world_abs_size)/$min_step_abs);
	$skip = round($nbrmin/$nbrmaj); // Don't draw minor ontop of major

	// Draw major ticks
	$ticklen2=4;
	$dx=round(sin($aAxisAngle)*$ticklen2);
	$dy=round(cos($aAxisAngle)*$ticklen2);
	$label=$aScale->scale[0]+$this->major_step;
		
	$aImg->SetLineWeight($this->weight);
		
	for($i=1; $i<=$nbrmaj; ++$i) {
	    $xt=round($i*$maj_step_abs*cos($aAxisAngle))+$aScale->scale_abs[0];
	    $yt=$aPos-round($i*$maj_step_abs*sin($aAxisAngle));
	    $aMajLabel[]=$label;
	    $label += $this->major_step;
	    $grid[]=$xt;
	    $grid[]=$yt;
	    $aMajPos[($i-1)*2]=$xt+2*$dx;
	    $aMajPos[($i-1)*2+1]=$yt-$aImg->GetFontheight()/2;				
	    if( !$this->supress_tickmarks ) {
		if( $this->majcolor!="" ) $aImg->PushColor($this->majcolor);
		$aImg->Line($xt+$dx,$yt+$dy,$xt-$dx,$yt-$dy);
		if( $this->majcolor!="" ) $aImg->PopColor();
	    }
	}

	// Draw minor ticks
	$ticklen2=3;
	$dx=round(sin($aAxisAngle)*$ticklen2);
	$dy=round(cos($aAxisAngle)*$ticklen2);
	if( !$this->supress_tickmarks && !$this->supress_minor_tickmarks)	{
	    if( $this->mincolor!="" ) $aImg->PushColor($this->mincolor);						
	    for($i=1; $i<=$nbrmin; ++$i) {
		if( ($i % $skip) == 0 ) continue;
		$xt=round($i*$min_step_abs*cos($aAxisAngle))+$aScale->scale_abs[0];
		$yt=$pos-round($i*$min_step_abs*sin($aAxisAngle));
		$aImg->Line($xt+$dx,$yt+$dy,$xt-$dx,$yt-$dy);
	    }
	    if( $this->mincolor!="" ) $aImg->PopColor();
	}
    }
}

	

//===================================================
// CLASS SpiderAxis
// Description: Implements axis for the spider graph
//===================================================
class SpiderAxis extends Axis {
    var $title_color="navy";
    var $title=null;
//---------------
// CONSTRUCTOR
    function SpiderAxis(&$img,&$aScale,$color=array(0,0,0)) {
	parent::Axis($img,$aScale,$color);
	$this->len=$img->plotheight;
	$this->font_size = FF_FONT1;
	$this->title = new FontProp();
	$this->color = array(0,0,0);
    }
//---------------
// PUBLIC METHODS	
    function SetTickLabels($l) {
	$this->ticks_label = $l;
    }
	
	
    // Stroke the axis 
    // $pos 			= Vertical position of axis
    // $aAxisAngle = Axis angle
    // $grid			= Returns an array with positions used to draw the grid
    //	$lf			= Label flag, TRUE if the axis should have labels
    function Stroke($pos,$aAxisAngle,&$grid,$title,$lf) {
	$this->img->SetColor($this->color);
		
	// Determine end points for the axis
	$x=round($this->scale->world_abs_size*cos($aAxisAngle)+$this->scale->scale_abs[0]);
	$y=round($pos-$this->scale->world_abs_size*sin($aAxisAngle));
		
	// Draw axis
	$this->img->SetColor($this->color);
	$this->img->SetLineWeight($this->weight);
	if( !$this->hide )
	    $this->img->Line($this->scale->scale_abs[0],$pos,$x,$y);
	
	$this->scale->ticks->Stroke($this->img,$grid,$pos,$aAxisAngle,$this->scale,$majpos,$majlabel);
		
	// Draw labels
	if( $lf && !$this->hide ) {
	    $this->img->SetFont($this->font_family,$this->font_style,$this->font_size);	
	    $this->img->SetTextAlign("left","top");
	    $this->img->SetColor($this->color);
			
	    // majpos contsins (x,y) coordinates for labels
	    for($i=0; $i<count($majpos)/2; ++$i) {
		if( $this->ticks_label != null )
		    $this->img->StrokeText($majpos[$i*2],$majpos[$i*2+1],$this->ticks_label[$i]);
		else
		    $this->img->StrokeText($majpos[$i*2],$majpos[$i*2+1],$majlabel[$i]);
	    }
	}
	$this->_StrokeAxisTitle($pos,$aAxisAngle,$title);
    }
//---------------
// PRIVATE METHODS	
	
    function _StrokeAxisTitle($pos,$aAxisAngle,$title) {
	// Draw title of this axis
	$this->img->SetFont($this->title->font_family,$this->title->font_style,$this->title->font_size);
	$this->img->SetColor($this->title->font_color);
	$marg=6;
	$xt=round(($this->scale->world_abs_size+$marg)*cos($aAxisAngle)+$this->scale->scale_abs[0]);
	$yt=round($pos-($this->scale->world_abs_size+$marg)*sin($aAxisAngle));

	// Position the axis title. 
	// dx, dy is the offset from the top left corner of the bounding box that sorrounds the text
	// that intersects with the extension of the corresponding axis. The code looks a little
	// bit messy but this is really the only way of having a reasonable position of the
	// axis titles.
	$h=$this->img->GetFontHeight();
	$w=$this->img->GetTextWidth($title);
	while( $aAxisAngle > 2*M_PI ) $aAxisAngle -= 2*M_PI;
	if( $aAxisAngle>=7*M_PI/4 || $aAxisAngle <= M_PI/4 ) $dx=0;
	if( $aAxisAngle>=M_PI/4 && $aAxisAngle <= 3*M_PI/4 ) $dx=($aAxisAngle-M_PI/4)*2/M_PI; 
	if( $aAxisAngle>=3*M_PI/4 && $aAxisAngle <= 5*M_PI/4 ) $dx=1;
	if( $aAxisAngle>=5*M_PI/4 && $aAxisAngle <= 7*M_PI/4 ) $dx=(1-($aAxisAngle-M_PI*5/4)*2/M_PI);
		
	if( $aAxisAngle>=7*M_PI/4 ) $dy=(($aAxisAngle-M_PI)-3*M_PI/4)*2/M_PI;
	if( $aAxisAngle<=M_PI/4 ) $dy=(1-$aAxisAngle*2/M_PI);
	if( $aAxisAngle>=M_PI/4 && $aAxisAngle <= 3*M_PI/4 ) $dy=1;
	if( $aAxisAngle>=3*M_PI/4 && $aAxisAngle <= 5*M_PI/4 ) $dy=(1-($aAxisAngle-3*M_PI/4)*2/M_PI);
	if( $aAxisAngle>=5*M_PI/4 && $aAxisAngle <= 7*M_PI/4 ) $dy=0;
		
	if( !$this->hide )
	    $this->img->StrokeText($xt-$dx*$w,$yt-$dy*$h,$title);

    }
		
	
} // Class


//===================================================
// CLASS SpiderGrid
// Description: Draws grid for the spider graph
//===================================================
class SpiderGrid extends Grid {
//------------
// CONSTRUCTOR
    function SpiderGrid() {
    }

//----------------
// PRIVATE METHODS	
    function Stroke(&$img,&$grid) {
	if( !$this->show ) return;
	$nbrticks = count($grid[0])/2;
	$nbrpnts = count($grid);
	$img->SetColor($this->grid_color);

⌨️ 快捷键说明

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